首頁 > Java > java教程 > Android、iOS和Java通用的AES128加密解密範例程式碼

Android、iOS和Java通用的AES128加密解密範例程式碼

高洛峰
發布: 2017-01-24 11:38:33
原創
2000 人瀏覽過

前言

行動端越來越火了,我們在開發過程中,總是會碰到要和行動端打交道的場景,像是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, 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;
}
登入後複製

   

rr

IOS解密

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;
}
登入後複製

   

IOS解密

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;
}
登入後複製

   

IOS

Android解密

rrreee

   

總結

以上就是這篇文章的全部內容了,希望本文的內容對各位開發者們能有所幫助,如果有疑問大家可以留言交流。

更多Android、iOS和Java通用的AES128加密解密範例程式碼相關文章請關注PHP中文網! 🎜
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板