前言
移動(dòng)端越來(lái)越火了,我們?cè)陂_(kāi)發(fā)過(guò)程中,總會(huì)碰到要和移動(dòng)端打交道的場(chǎng)景,比如android和iOS的打交道。為了讓數(shù)據(jù)交互更安全,我們需要對(duì)數(shù)據(jù)進(jìn)行加密傳輸。
這篇文章給大家分享AES的加密和解密、Android和ios通用的AES加密算法、大家可以直接集成到自己的項(xiàng)目、服務(wù)器接口如果是用Java寫(xiě)的話(huà)、整個(gè)框架都完美了、如果是.NET編寫(xiě)的后臺(tái)接口的話(huà)、得改造一下哦
IOS加密
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/*加密方法*/ (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解密
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/*解密方法*/ (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加密
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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解密
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位開(kāi)發(fā)者們能有所幫助,如果有疑問(wèn)大家可以留言交流。