This project contains the implementation of (iOS Objective C, iOS Swift, Android, Java, Javascript, NodeJS, Web)
- iOS
- Android
- Javascript/NodeJS/Web
-
Cross platform support. Encryption-Decryption works across iOS, Android and Node.js.
-
Support for Random IV (initialization vector) for encryption and decryption. Randomization is crucial for encryption schemes to achieve semantic security, a property whereby repeated usage of the scheme under the same key does not allow an attacker to infer relationships between segments of the encrypted message.
-
Support for SHA-256 for hashing the key. Never use plain text as encryption key. Always hash the plain text key and then use for encryption. AES permits the use of 256-bit keys. Breaking a symmetric 256-bit key by brute force requires 2^128 times more computational power than a 128-bit key. A device that could check a billion billion (10^18) AES keys per second would in theory require about 3×10^51 years to exhaust the 256-bit key space.
Complex logics such as generating IV and sha256 the key are done within the library.
Pass in the plainText as String, Pass in the key as String, Pass in the IV as String. Both the encrypted and decrypted data are also String. key is converted to 256-bit within the library for Android and iOS
Add a bridging header. Apple documentation
#import "CryptLib.h"
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)")
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);
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();
}
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);
encryptedString rKzNsa7Qzk9TExJ6aHg49tGDiritTUJ08RMPm48S0o4=
decryptedString this is my plain text
Refer the source code
Thanks to Cross-platform-AES-encryption