Android、iOS和Java通用的AES128加密解密示例代碼
前言
移動端越來越火了,我們在開發(fā)過程中,總會碰到要和移動端打交道的場景,比如android和iOS的打交道。為了讓數(shù)據(jù)交互更安全,我們需要對數(shù)據(jù)進(jìn)行加密傳輸。
這篇文章給大家分享AES的加密和解密、Android和ios通用的AES加密算法、大家可以直接集成到自己的項(xiàng)目、服務(wù)器接口如果是用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, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256 1]; // room for terminator (unused)
bzero(keyPtr, 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, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
[[NSData AESKeyForPassword:PASSWORD] bytes], kCCKeySizeAES256,
ivBuff ,/* initialization vector (optional) */
[cipherData bytes], dataLength, /* input */
buffer, bufferSize, /* 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, SecretKey sk, IvParameterSpec IV,
byte[] ciphertext) {
try {
Cipher c = Cipher.getInstance(cmp);
c.init(Cipher.DECRYPT_MODE, sk, IV);
return c.doFinal(ciphertext);
} 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");
e.printStackTrace();
}
return null;
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對各位開發(fā)者們能有所幫助,如果有疑問大家可以留言交流。
相關(guān)文章
mybatis實(shí)現(xiàn)遍歷Map的key和value
這篇文章主要介紹了mybatis實(shí)現(xiàn)遍歷Map的key和value方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Java實(shí)戰(zhàn)項(xiàng)目之斗地主和斗牛游戲的實(shí)現(xiàn)
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實(shí)現(xiàn)一個斗地主和一個斗牛游戲,大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11
Java實(shí)現(xiàn)評論回復(fù)功能的完整步驟
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)評論回復(fù)功能的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

