1. 程式人生 > >AES跨平臺加密解密 第三方庫:Cross-Platform-AES

AES跨平臺加密解密 第三方庫:Cross-Platform-AES

向大家推薦一個AES跨平臺加解密開源庫 Cross-Platform-AES。該庫使用方法很簡單,根據語言直接將對應的檔案新增進自己專案即可,作者也給出了不同語言呼叫的例子。總之是個挺簡單的直接就能用的小專案。 我自己在Swift 4、Android上執行成功,兩個平臺的加解密也都一致。這裡把他推薦給大家。也希望大家能給作者一個> star★ : )

平臺支援

  1. iOS (iOS Objective C, iOS Swift)
  2. Android
  3. Javascript/NodeJS/Web

加密模式

採用AES演算法,CBC模式,PKCS5Padding祕鑰補齊模式,預設祕鑰長度32位元組,預設偏移量iv16位元組。以上配置決定了加密解密結果,不要輕易修改,如修改請保證所有平臺的配置一致,否則會導致結果不一致,大神請忽略此提示。
這裡寫圖片描述

iOS / Swift 3

import "CryptLib.h" // 此句在Swift專案橋檔案中新增

let plainText = "this is my plain text"
let key = "simplekey"
let iv = "1234123412341234"

let cryptoLib = CryptLib();

let encryptedString = cryptoLib.encryptPlainText(with: plainText, key: key, iv: iv)
print("encryptedString \(encryptedString! as String)"
) let decryptedString = cryptoLib.decryptCipherText(with: encryptedString, key: key, iv: iv) print("decryptedString \(decryptedString! as String)")

iOS / Objective-C

NSString *plainText = @"this is my plain text";
NSString *key = @"simplekey";
NSString *iv = @"1234123412341234";

CryptLib *cryptoLib = [[CryptLib alloc] init];

NSString
*encryptedString = [cryptoLib encryptPlainTextWith:plainText key:key iv:iv]; NSLog(@"encryptedString %@", encryptedString); NSString *decryptedString = [cryptoLib decryptCipherTextWith:encryptedString key:key iv:iv]; NSLog(@"decryptedString %@", decryptedString);

Android

try {
    String plainText = "this is my plain text";
    String key = "simplekey";
    String iv = "1234123412341234";

    CryptLib cryptLib = new CryptLib();

    String encryptedString = cryptLib.encryptSimple(plainText, key, iv);
    System.out.println("encryptedString " + encryptedString);

    String decryptedString = cryptLib.decryptSimple(encryptedString, key, iv);
    System.out.println("decryptedString " + decryptedString);

} catch (Exception e) {
    e.printStackTrace();
}

Javascript / NodeJS / Web

Download the library

npm install cryptlib --save
var plainText = "this is my plain text";
var key = "simplekey";
var iv = "1234123412341234";

var cryptoLib = require('cryptlib');

shaKey = cryptoLib.getHashSha256(key, 32); // This line is not needed on Android or iOS. Its already built into CryptLib.m and CryptLib.java

var encryptedString = cryptoLib.encrypt(plainText, shaKey, iv);
console.log('encryptedString %s', encryptedString);

var decryptedString = cryptoLib.decrypt(encryptedString, shaKey, iv);
console.log('decryptedString %s', decryptedString);

Output

encryptedString rKzNsa7Qzk9TExJ6aHg49tGDiritTUJ08RMPm48S0o4=    // 加密結果
decryptedString this is my plain text                           // 解密結果

專案地址:https://github.com/skavinvarnan/Cross-Platform-AES

希望大家能給作者一個 star★ 讓更多人能看到這個不錯的小專案 : )