前言
移动端越来越火了,我们在开发过程中,总会碰到要和移动端打交道的场景,比如androID和iOS的打交道。为了让数据交互更安全,我们需要对数据进行加密传输。
这篇文章给大家分享AES的加密和解密、AndroID和ios通用的AES加密算法、大家可以直接集成到自己的项目、服务器接口如果是用Java写的话、整个框架都完美了、如果是.NET编写的后台接口的话、得改造一下哦
IOS加密
/*加密方法*/ (Nsstring *)AES256EncryptWithPlainText:(Nsstring *)plain { NSData *plainText = [plain dataUsingEnCoding:NSUTF8StringEnCoding]; // ´key´ should be 32 bytes for AES256,will be null-padded otherwise char keyPtr[kCCKeySizeAES256 1]; // room for terminator (unused) bzero(keyPtr,sizeof(keyPtr)); // fill with zeroes (for padding) NSUInteger dataLength = [plainText length]; size_t bufferSize = dataLength kCCBlockSizeAES128; voID *buffer = malloc(bufferSize); bzero(buffer,sizeof(buffer)); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,kCCAlgorithmAES128,kCcoptionPKCS7padding,[[NSData AESKeyForPassword:PASSWORD] bytes],kCCKeySizeAES256,ivBuff /* initialization vector (optional) */,[plainText bytes],dataLength,/* input */ buffer,bufferSize,/* output */ &numBytesEncrypted); if (cryptStatus == kCCSuccess) { NSData *encryptData = [NSData dataWithBytesNocopy:buffer length:numBytesEncrypted]; return [encryptData base64EnCoding]; } free(buffer); //free the buffer; return nil;}
IOS解密
/*解密方法*/ (Nsstring *)AES256DecryptWithCiphertext:(Nsstring *)ciphertexts{ NSData *cipherData = [NSData dataWithBase64EncodedString:ciphertexts]; // ´key´ should be 32 bytes for AES256,sizeof(keyPtr)); // fill with zeroes (for padding) NSUInteger dataLength = [cipherData length]; size_t bufferSize = dataLength kCCBlockSizeAES128; voID *buffer = malloc(bufferSize); size_t numBytesDecrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,ivBuff,/* initialization vector (optional) */ [cipherData bytes],/* output */ &numBytesDecrypted); if (cryptStatus == kCCSuccess) { NSData *encryptData = [NSData dataWithBytesNocopy:buffer length:numBytesDecrypted]; return [[[Nsstring alloc] initWithData:encryptData enCoding:NSUTF8StringEnCoding] init]; } free(buffer); //free the buffer; return nil;}
AndroID加密
private byte[] encrypt(String cmp,SecretKey sk,IvParameterSpec IV,byte[] msg) { try { Cipher c = Cipher.getInstance(cmp); c.init(Cipher.ENCRYPT_MODE,sk,IV); return c.doFinal(msg); } catch (NoSuchAlgorithmException nSAE) { Log.e("AESdemo","no cipher getinstance support for " cmp); } catch (NoSuchpaddingException nspe) { Log.e("AESdemo","no cipher getinstance support for padding " cmp); } catch (InvalIDKeyException e) { Log.e("AESdemo","invalID key exception"); } catch (InvalIDAlgorithmParameterException e) { Log.e("AESdemo","invalID algorithm parameter exception"); } catch (IllegalBlockSizeException e) { Log.e("AESdemo","illegal block size exception"); } catch (BadpaddingException e) { Log.e("AESdemo","bad padding exception"); } return null;}
AndroID解密
private byte[] decrypt(String cmp,byte[] ciphertext) { try { Cipher c = Cipher.getInstance(cmp); c.init(Cipher.DECRYPT_MODE,IV); return c.doFinal(ciphertext); } catch (NoSuchAlgorithmException nSAE) { Log.e("AESdemo","bad padding exception"); e.printstacktrace(); } return null;}
总结
以上就是这篇文章的全部内容了,希望本文的内容对各位开发者们能有所帮助,如果有疑问大家可以留言交流。
总结以上是内存溢出为你收集整理的Android、iOS和Java通用的AES128加密解密示例代码全部内容,希望文章能够帮你解决Android、iOS和Java通用的AES128加密解密示例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)