AES跨平台加密解密 第三方库:Cross-Platform-AES

版权声明:本文为博主原创文章,但是可以随意转载。 https://blog.csdn.net/zhang5690800/article/details/79263934

向大家推荐一个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★ 让更多人能看到这个不错的小项目 : )

猜你喜欢

转载自blog.csdn.net/zhang5690800/article/details/79263934