All Projects → martinwithaar → Encryptor4j

martinwithaar / Encryptor4j

Licence: mit
Strong encryption for Java simplified

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Encryptor4j

Jsrsasign
The 'jsrsasign' (RSA-Sign JavaScript Library) is an opensource free cryptography library supporting RSA/RSAPSS/ECDSA/DSA signing/validation, ASN.1, PKCS#1/5/8 private/public key, X.509 certificate, CRL, OCSP, CMS SignedData, TimeStamp, CAdES JSON Web Signature/Token in pure JavaScript.
Stars: ✭ 2,760 (+2900%)
Mutual labels:  encryption, aes, rsa, decryption
Hybrid Crypto Js
RSA+AES hybrid encryption implementation for JavaScript. Works with Node.js, React Native and modern browsers.
Stars: ✭ 87 (-5.43%)
Mutual labels:  encryption, aes, rsa, decryption
Hat.sh
encrypt and decrypt files in your browser. Fast, Secure client-side File Encryption and Decryption using the web crypto api
Stars: ✭ 886 (+863.04%)
Mutual labels:  encryption, aes, decryption
Padding Oracle Attacker
🔓 CLI tool and library to execute padding oracle attacks easily, with support for concurrent network requests and an elegant UI.
Stars: ✭ 136 (+47.83%)
Mutual labels:  encryption, aes, decryption
Encrypt
🔒 A set of high-level APIs over PointyCastle for two-way cryptography.
Stars: ✭ 199 (+116.3%)
Mutual labels:  encryption, aes, rsa
Py7zr
7zip in python3 with ZStandard, PPMd, LZMA2, LZMA1, Delta, BCJ, BZip2, and Deflate compressions, and AES encryption.
Stars: ✭ 110 (+19.57%)
Mutual labels:  encryption, aes, decryption
Cross Platform Aes
Simple cross-platform encryption and decryption using AES
Stars: ✭ 127 (+38.04%)
Mutual labels:  encryption, aes, decryption
Laravel Database Encryption
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Stars: ✭ 238 (+158.7%)
Mutual labels:  encryption, aes, decryption
Python-File-Encryptor
Encrypt and Decrypt files using Python (AES CBC MODE)
Stars: ✭ 51 (-44.57%)
Mutual labels:  encryption, aes, decryption
EasyAES
AES encrypt/decrypt, Android, iOS, php compatible(兼容php, Android, iOS平台)
Stars: ✭ 79 (-14.13%)
Mutual labels:  encryption, aes, decryption
Gonnacry
A Linux Ransomware
Stars: ✭ 341 (+270.65%)
Mutual labels:  encryption, aes, decryption
Aes Rsa Java
AES+RSA结合应用java示例
Stars: ✭ 295 (+220.65%)
Mutual labels:  encryption, aes, rsa
Xjar
Spring Boot JAR 安全加密运行工具,支持的原生JAR。
Stars: ✭ 692 (+652.17%)
Mutual labels:  encryption, aes, decryption
Java Crypto Utils
Java Cryptographic, Encoding and Hash Utilities
Stars: ✭ 15 (-83.7%)
Mutual labels:  encryption, decryption
Enigma
Enigma cipher tool
Stars: ✭ 13 (-85.87%)
Mutual labels:  encryption, decryption
Cryptr
A simple shell utility for encrypting and decrypting files using OpenSSL.
Stars: ✭ 81 (-11.96%)
Mutual labels:  encryption, decryption
Rsa Javascript
rsa-javascript
Stars: ✭ 13 (-85.87%)
Mutual labels:  encryption, rsa
Dbtransitencryption
Secure data before sending it over the internet
Stars: ✭ 20 (-78.26%)
Mutual labels:  aes, rsa
Iocane
An odorless, tasteless NodeJS crypto library that dissolves instantly in liquid
Stars: ✭ 35 (-61.96%)
Mutual labels:  encryption, decryption
Mirage Crypto
Cryptographic primitives for MirageOS
Stars: ✭ 39 (-57.61%)
Mutual labels:  aes, rsa

Maven Central Android Arsenal

Encryptor4j

Strong encryption for Java simplified

Get it

Add this line to your build.gradle's dependencies:

compile 'org.encryptor4j:encryptor4j:0.1.2'

Overview

Encryptor4j consists of a set of wrapper and utility classes that make leveraging cryptography in your application alot easier. It enables developers to encrypt and decrypt with little room for error, using few lines of code and supports all popular encryption algorithms such as AES, DES, RSA etc.

Encryption

Encryptor is a small wrapper class for Java that greatly reduces boilerplate when implementing cryptography in an application. It handles initialization vectors (IVs) transparently by securely generating and prepending them to the encrypted message. The IV is then read and used for decryption at a later time.

The Encryptor class is thread-safe and can thus be used by multiple threads.

Message encryption

Encrypting a message using AES in CBC mode is as simple as this:

String message = "This string has been encrypted & decrypted using AES in Cipher Block Chaining mode";
SecretKey secretKey = KeyFactory.AES.randomKey();
Encryptor encryptor = new Encryptor(secretKey, "AES/CBC/PKCS5Padding", 16);
byte[] encrypted = encryptor.encrypt(message.getBytes());

Using AES in GCM mode needs an additional tLen value in the constructor:

String message = "This string has been encrypted & decrypted using AES in Galois Counter Mode";
SecretKey secretKey = KeyFactory.AES.randomKey();
Encryptor encryptor = new Encryptor(secretKey, "AES/GCM/NoPadding", 16, 128);
byte[] encrypted = encryptor.encrypt(message.getBytes());

Decrypting a message is done like this:

// Assuming the same secret key is used
Encryptor encryptor = new Encryptor(secretKey, "AES/GCM/NoPadding", 16, 128);
byte[] decrypted = encryptor.decrypt(encrypted);
System.out.println(new String(decrypted));

Streaming encryption

Streaming encryption can be done with relative ease: Just wrap any existing InputStream or OutputStream using an Encryptor instance. The Encryptor will prepend the IV transparently and use the cipher that it has been constructed with.

The following example reads a file and writes it to another file using streaming encryption:

SecretKey secretKey = KeyFactory.AES.randomKey();
Encryptor encryptor = new Encryptor(secretKey, "AES/CTR/NoPadding", 16);

InputStream is = null;
OutputStream os = null;
try {
  is = new FileInputStream("original.jpg");
  os = encryptor.wrapOutputStream(new FileOutputStream("encrypted.jpg"));
  byte[] buffer = new byte[4096];
  int nRead;
  while((nRead = is.read(buffer)) != -1) {
    os.write(buffer, 0, nRead);
  }
  os.flush();
} finally {
  if(is != null) {
    is.close();
  }
  if(os != null) {
    os.close();
  }
}

Decryption:

// Assuming the same secret key is used
Encryptor encryptor = new Encryptor(secretKey, "AES/CTR/NoPadding", 16);

InputStream is = null;
OutputStream os = null;

try {
  is = encryptor.wrapInputStream(new FileInputStream("encrypted.jpg"));
  os = new FileOutputStream("decrypted.jpg");
  byte[] buffer = new byte[4096];
  int nRead;
  while((nRead = is.read(buffer)) != -1) {
    os.write(buffer, 0, nRead);
  }
  os.flush();
} finally {
  if(is != null) {
    is.close();
  }
  if(os != null) {
    os.close();
  }
}

Encryption utilities

Encryptor4j comes with utility classes for performing common encryption tasks.

File encryption

The FileEncryptor class encrypts and decrypts files and can be invoked from the command line.

Encryption:

File srcFile = new File("original.zip");
File destFile = new File("original.zip.encrypted");
String password = "mysupersecretpassword";
FileEncryptor fe = new FileEncryptor(password);
fe.encrypt(srcFile, destFile);

Decryption:

File srcFile = new File("original.zip.encrypted");
File destFile = new File("decrypted.zip");
String password = "mysupersecretpassword";
FileEncryptor fe = new FileEncryptor(password);
fe.decrypt(srcFile, destFile);

By default FileEncryptor uses AES in CTR mode with the maximum key length permitted (up to 256-bit).

Text encryption

The TextEncryptor class encrypts and decrypts text using Base64 encoding for the encrypted message and can be invoked from the command line.

Encryption:

String text = "This is a secret message";
String password = "mysupersecretpassword";
TextEncryptor te = new TextEncryptor(password);
String encrypted = te.encrypt(text);

Decryption:

String encrypted = "5Zz0WGJ5XK1YDxs7O5VX7nhBaNeFWnvz/RBxmfawammmkNZhWeTJkMIQ/RWIPDmx";
String password = "mysupersecretpassword";
TextEncryptor te = new TextEncryptor(password);
String text = te.decrypt(encrypted);

By default TextEncryptor uses AES in CBC mode with the maximum key length permitted (up to 256-bit).

Key agreement

Key agreement protocols such as Diffie-Hellman and Elliptic Curve Diffie-Hellman have gained significant popularity in the past years. Encryptor4j comes with wrapper classes that further simplify these processes.

A simple DH key agreement can be written like this:

// Create primes p & g
// Tip: You don't need to regenerate p; Use a fixed value in your application
int bits = 2048;
BigInteger p = BigInteger.probablePrime(bits, new SecureRandom());
BigInteger g = new BigInteger("2");

// Create two peers
KeyAgreementPeer peerA = new DHPeer(p, g);
KeyAgreementPeer peerB = new DHPeer(p, g);

// Exchange public keys and compute shared secret
byte[] sharedSecretA = peerA.computeSharedSecret(peerB.getPublicKey());
byte[] sharedSecretB = peerB.computeSharedSecret(peerA.getPublicKey());

an ECDH key agreement like this:

String algorithm = "brainpoolp256r1";

// Create two peers
KeyAgreementPeer peerA = new ECDHPeer(algorithm);
KeyAgreementPeer peerB = new ECDHPeer(algorithm);

// Exchange public keys and compute shared secret
byte[] sharedSecretA = peerA.computeSharedSecret(peerB.getPublicKey());
byte[] sharedSecretB = peerB.computeSharedSecret(peerA.getPublicKey());

Key agreement protocols like this can be used to set up encrypted messaging or streaming as shown above. Setup an Encryptor instance with the freshly obtained shared secret:

byte[] sharedSecret = peer.computeSharedSecret(publicKey);
SecretKey secretKey = new SecretKeySpec(sharedSecret, "AES");
Encryptor encryptor = new Encryptor(secretKey, "AES/CTR/NoPadding", 16);

Custom IV handling

When using block modes that need an IV the Encryptor instance handles the generation and prepending automatically. This behavior can be overridden by using an explicit IV or by not prepending the IV when a custom means of transmission is required.

Encrypt data using an explicit IV:

Encryptor encryptor = new Encryptor(secretKey, "AES/CBC/PKCS5Padding", 16);
byte[] iv = new byte[] { 107, 67, 98, -81, 54, -31, -110, -63, 24, 76, -12, -48, -55, 14, 15, 19 };
byte[] encrypted = encryptor.encrypt(data, null, iv);

Disable prepending of the IV by calling:

encryptor.setPrependIV(false);

You will have to store the IV yourself in order to properly decrypt a ciphertext.

Disable IV generation

Sometimes the Cipher implementation used by the VM does not permit passing the IV via the algorithm parameters because it will generate it itself (such as with some versions of Android). The automatic IV generation functionality of the Encryptor class should then be disabled like so:

encryptor.setGenerateIV(false);

AEAD and Additional Authentication Data (AAD)

When using an AEAD (authenticated encryption with associated data) blockmode such as GCM it is possible to include additional associated data when encrypting or decrypting:

byte[] aad = new byte[] { 74, 17, -123, -27, 21, 46, 57, 18, 111, -102, 98, -84, 8, -68, -23, 72 };
byte[] encrypted = encryptor.encrypt(message.getBytes(), aad);

This can be used to enhance the authentication strength of a cryptographic protocol if properly designed.

Best practices

Encryption does not guarantee security out-of-the-box. A certain degree of understanding is required to choose the correct algorithm, block cipher mode, key size etc.

Here are some tips:

Javadoc

Consult the project's Javadoc at http://martinwithaar.github.io/Encryptor4j/

Unlimited Strength Jurisdiction Policy

In order to use 256-bit keys with AES encryption you must download and install custom policy files made available by Oracle:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].