Skip to content

piyapan039285/Multi-platform-AES-Encryption

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multi-platform-AES-Encryption

Ready-To-Use AES-256 bit encryption library which supports across platforms. For example, you can encrypt message using Java and decrypt it using Swift. View Demo

Encryption method is AES with CBC mode (random IV in each encryption), PCKS7 padding (aka. PCKS5), and HMAC-256 authentication.

Language Supported

  • C# (.NET Framework 4.5)
  • Java 8 (can be used with Android), Java 11
  • Objective-C
  • Swift 3, 4, 5
  • NodeJS
  • Python 3.7
  • Javascript (Tested on Chrome 59, Firefox 54, IE 10-11)

Input

  • plain_text (string) : string MUST be able to be converted into UTF-8 encoding.
  • key (string) : string in hexadecimal format (64 characters length). Space in string is allowed.

Output

  • encrypted_string (string) : string in hexadecimal format.

Note : decryption method will ouput null if you pass wrong key.



Sample Usage

C#

Installation

  1. If you use windows 7, 8 you need to install .NET Framework 4.5 from this link
  2. Create .NET Framework 4.5 Project in Visual Studio.
  3. copy \CSharp\Encryption.cs to your C# project.
  4. build project.

Example

See more example at file \CSharp\AESUnitTest\UnitTest1.cs

string text = "  Hello World  ";
string key = "44 52 d7 16 87 b6 bc 2c 93 89 c3 34 9f dc 17 fb 3d fb ba 62 24 af fb 76 76 e1 33 79 26 cd d6 02";

string encryptedText = AES.Encryption.encryptData(text, key);
string decryptedText = AES.Encryption.decryptData(encryptedText, key);

Java

Installation

  1. copy file to project
    • Java 8: \Java_or_Android\app\src\main\java\com\piyapan039285\java_or_android\Encryption.java
    • Java 11: /Java11/src/main/java/com/piyapan039285/Encryption.java
  2. build project.

Example
See more example at this file

  • java 8: \Java_or_Android\app\src\test\java\com\piyapan039285\java_or_android\AESUnitTest.java
  • Java 11: /Java11/src/test/java/com/piyapan039285/AESUnitTest.java
String text = "  Hello World  ";
String key = "44 52 d7 16 87 b6 bc 2c 93 89 c3 34 9f dc 17 fb 3d fb ba 62 24 af fb 76 76 e1 33 79 26 cd d6 02";

String encryptedText = Encryption.encryptData(text, key);
String decryptedText = Encryption.decryptData(encryptedText, key);

Note: For Java 8, If you want to run unit test, you have to download "unlimited strength file" (link below). Then, install file to ${java.home}/jre/lib/security/. In Android Studio, you can find JDK path from Files -> Project Structure -> SDK Location.

Objective C

Installation

  1. copy \ObjectiveC\ObjectiveC\Encryptor.m, \ObjectiveC\ObjectiveC\Encryptor.h to your project.
  2. build project.

Example
See more example at \ObjectiveC\EncryptionTests\EncryptionTests.m

NSString *text = @"  Hello World  ";
NSString *key = @"44 52 d7 16 87 b6 bc 2c 93 89 c3 34 9f dc 17 fb 3d fb ba 62 24 af fb 76 76 e1 33 79 26 cd d6 02";

NSString *encryptedText = [Encryptor encryptedData:text WithHexKey:key];
NSString *decryptedText = [Encryptor decryptedData:encryptedText WithHexKey:key];

Swift

Installation

  1. copy file to project
    • swift 3: Swift\Swift3.1\Swift3.1\Encryption.swift
    • swift 4: Swift\Swift4\Encryption_Swift\Encryption.swift
    • swift 5: Swift\Swift5\Encryption_Swift\Encryption.swift
  2. add Security.framework library and add #import <CommonCrypto/CommonCryptor.h>, #import <CommonCrypto/CommonHMAC.h> to the bridging header as shown in image below (More info : here)




Example

See more example at this file

  • Swift 3: Swift\Swift3.1\EncryptionTests\EncryptionTests.swift
  • Swift 4: Swift\Swift4\EncryptionTests\EncryptionTests.swift
  • Swift 5: Swift\Swift5\EncryptionTests\Encryption_SwiftTests.swift
let text:String = "  Hello World  ";
let key:String = "44 52 d7 16 87 b6 bc 2c 93 89 c3 34 9f dc 17 fb 3d fb ba 62 24 af fb 76 76 e1 33 79 26 cd d6 02";

let encryptedText:String? = Encryption.encryptData(plainText: text, hexKey: key)
let decryptedText:String? = Encryption.decryptData(hexStr: encryptedText!, hexKey: key)

NodeJS

Installation

  1. copy \NodeJS\encryption.js to your project.
  2. build project.

Example

See more example at \NodeJS\unitTest.js

var encryptor = require("./encryption");

var text = "  Hello World  ";
var key = "44 52 d7 16 87 b6 bc 2c 93 89 c3 34 9f dc 17 fb 3d fb ba 62 24 af fb 76 76 e1 33 79 26 cd d6 02";

var encryptedText = encryptor.encryptData(text, key);
var decryptedText = encryptor.decryptData(encryptedText, key);

Python

Installation

  1. add pycrypto==2.6.1 to requirements.txt file. Then, run pip install -r requirements.txt
  2. copy /Python/MultiPlatformAESEncryptionPython3.7/encryption.py to your project.

Example
See more example at /Python/MultiPlatformAESEncryptionPython3.7/unit_test.py

from encryption import encrypt_data, decrypt_data

text = "  Hello World  "
key = "44 52 d7 16 87 b6 bc 2c 93 89 c3 34 9f dc 17 fb 3d fb ba 62 24 af fb 76 76 e1 33 79 26 cd d6 02"

encrypted_text = encrypt_data(text, key)
decrypted_text = decrypt_data(encrypted_text, key)

Javascript

Installation

  1. copy javascript\encryption.js, javascript\forge.min.js to your project.
  2. add script section in your html file.
 <script src="forge.min.js"></script>
 <script src="encryption.js"></script>

Example
See more example at javascript\unitTest.html

<html>
<head>
  <script src="forge.min.js"></script>
  <script src="encryption.js"></script>
  
  <script>
	var text = "  Hello World  ";
	var key = "44 52 d7 16 87 b6 bc 2c 93 89 c3 34 9f dc 17 fb 3d fb ba 62 24 af fb 76 76 e1 33 79 26 cd d6 02";

	var encryptedText = encryptor.encryptData(text, key);
	var decryptedText = encryptor.decryptData(encryptedText, key);
  </script>  
</head>
<body>
</body>
</html>

About

Ready-To-Use AES-256 bit encryption library in various languages

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published