Android、iOS和Java通用的AES128加密解密示例代码

时间:2021-05-20

前言

移动端越来越火了,我们在开发过程中,总会碰到要和移动端打交道的场景,比如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 , [plainText bytes], dataLength, buffer, bufferSize, &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 , [cipherData bytes], dataLength, buffer, bufferSize, &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;}

总结

以上就是这篇文章的全部内容了,希望本文的内容对各位开发者们能有所帮助,如果有疑问大家可以留言交流。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章