前言
行動端越來越火了,我們在開發過程中,總是會碰到要和行動端打交道的場景,像是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中文網! 🎜
本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3 Linux新版
SublimeText3 Linux最新版