Android、iOS和Java通用的AES128加密解密示例代码
前言
移动端越来越火了,我们在开发过程中,总会碰到要和移动端打交道的场景,比如android和iOS的打交道。为了让数据交互更安全,我们需要对数据进行加密传输。
这篇文章给大家分享AES的加密和解密、Android和ios通用的AES加密算法、大家可以直接集成到自己的项目、服务器接口如果是用Java写的话、整个框架都完美了、如果是.NET编写的后台接口的话、得改造一下哦
IOS加密
/*加密方法*/
(NSString*)AES256EncryptWithPlainText:(NSString*)plain{
NSData*plainText=[plaindataUsingEncoding:NSUTF8StringEncoding];
//´key´shouldbe32bytesforAES256,willbenull-paddedotherwise
charkeyPtr[kCCKeySizeAES2561];//roomforterminator(unused)
bzero(keyPtr,sizeof(keyPtr));//fillwithzeroes(forpadding)
NSUIntegerdataLength=[plainTextlength];
size_tbufferSize=dataLengthkCCBlockSizeAES128;
void*buffer=malloc(bufferSize);
bzero(buffer,sizeof(buffer));
size_tnumBytesEncrypted=0;
CCCryptorStatuscryptStatus=CCCrypt(kCCEncrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding,
[[NSDataAESKeyForPassword:PASSWORD]bytes],kCCKeySizeAES256,
ivBuff/*initializationvector(optional)*/,
[plainTextbytes],dataLength,/*input*/
buffer,bufferSize,/*output*/
&numBytesEncrypted);
if(cryptStatus==kCCSuccess){
NSData*encryptData=[NSDatadataWithBytesNoCopy:bufferlength:numBytesEncrypted];
return[encryptDatabase64Encoding];
}
free(buffer);//freethebuffer;
returnnil;
}
IOS解密
/*解密方法*/
(NSString*)AES256DecryptWithCiphertext:(NSString*)ciphertexts{
NSData*cipherData=[NSDatadataWithBase64EncodedString:ciphertexts];
//´key´shouldbe32bytesforAES256,willbenull-paddedotherwise
charkeyPtr[kCCKeySizeAES2561];//roomforterminator(unused)
bzero(keyPtr,sizeof(keyPtr));//fillwithzeroes(forpadding)
NSUIntegerdataLength=[cipherDatalength];
size_tbufferSize=dataLengthkCCBlockSizeAES128;
void*buffer=malloc(bufferSize);
size_tnumBytesDecrypted=0;
CCCryptorStatuscryptStatus=CCCrypt(kCCDecrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding,
[[NSDataAESKeyForPassword:PASSWORD]bytes],kCCKeySizeAES256,
ivBuff,/*initializationvector(optional)*/
[cipherDatabytes],dataLength,/*input*/
buffer,bufferSize,/*output*/
&numBytesDecrypted);
if(cryptStatus==kCCSuccess){
NSData*encryptData=[NSDatadataWithBytesNoCopy:bufferlength:numBytesDecrypted];
return[[[NSStringalloc]initWithData:encryptDataencoding:NSUTF8StringEncoding]init];
}
free(buffer);//freethebuffer;
returnnil;
}
Android加密
privatebyte[]encrypt(Stringcmp,SecretKeysk,IvParameterSpecIV,
byte[]msg){
try{
Cipherc=Cipher.getInstance(cmp);
c.init(Cipher.ENCRYPT_MODE,sk,IV);
returnc.doFinal(msg);
}catch(NoSuchAlgorithmExceptionnsae){
Log.e("AESdemo","nociphergetinstancesupportfor"cmp);
}catch(NoSuchPaddingExceptionnspe){
Log.e("AESdemo","nociphergetinstancesupportforpadding"cmp);
}catch(InvalidKeyExceptione){
Log.e("AESdemo","invalidkeyexception");
}catch(InvalidAlgorithmParameterExceptione){
Log.e("AESdemo","invalidalgorithmparameterexception");
}catch(IllegalBlockSizeExceptione){
Log.e("AESdemo","illegalblocksizeexception");
}catch(BadPaddingExceptione){
Log.e("AESdemo","badpaddingexception");
}
returnnull;
}
Android解密
privatebyte[]decrypt(Stringcmp,SecretKeysk,IvParameterSpecIV,
byte[]ciphertext){
try{
Cipherc=Cipher.getInstance(cmp);
c.init(Cipher.DECRYPT_MODE,sk,IV);
returnc.doFinal(ciphertext);
}catch(NoSuchAlgorithmExceptionnsae){
Log.e("AESdemo","nociphergetinstancesupportfor"cmp);
}catch(NoSuchPaddingExceptionnspe){
Log.e("AESdemo","nociphergetinstancesupportforpadding"cmp);
}catch(InvalidKeyExceptione){
Log.e("AESdemo","invalidkeyexception");
}catch(InvalidAlgorithmParameterExceptione){
Log.e("AESdemo","invalidalgorithmparameterexception");
}catch(IllegalBlockSizeExceptione){
Log.e("AESdemo","illegalblocksizeexception");
}catch(BadPaddingExceptione){
Log.e("AESdemo","badpaddingexception");
e.printStackTrace();
}
returnnull;
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对各位开发者们能有所帮助,如果有疑问大家可以留言交流。