org.cryptonode.jncryptor
Class AES256JNCryptor

java.lang.Object
  extended by org.cryptonode.jncryptor.AES256JNCryptor
All Implemented Interfaces:
JNCryptor

public class AES256JNCryptor
extends Object
implements JNCryptor

This JNCryptor instance produces data in version 3 format. It can read data in any format since version 2.

 | version | options | encryption salt | HMAC salt |   IV   | ... ciphertext ... |     HMAC    |
 |    0    |    1    |       2->9      |   10->17  | 18->33 | <-      ...     -> | (n-32) -> n |
 

The encryption key is derived using the PKBDF2 function, using a random eight-byte encryption salt, the supplied password and 10,000 iterations. The iteration count can be changed using the setPBKDFIterations(int) method. The HMAC key is derived in a similar fashion, using its own random eight-byte HMAC salt. Both salt values are stored in the ciphertext output (as shown above).

The ciphertext is AES-256-CBC encrypted, using a randomly generated IV and the encryption key (described above), with PKCS #5 padding.

The HMAC is calculated across all the data (except the HMAC itself, of course), generated using the HMAC key described above and the SHA-256 PRF.

This class is thread-safe. Multiple threads may share one instance of this class, or each thread may have its own instance.

See https://github .com/rnapier/RNCryptor/wiki/Data-Format, from which most of the information above was shamelessly copied.

Since:
0.5

Constructor Summary
AES256JNCryptor()
          Creates a new AES256JNCryptor instance.
AES256JNCryptor(int iterations)
          Creates a new AES256JNCryptor instance that uses a specific number of PBKDF iterations.
 
Method Summary
 byte[] decryptData(byte[] ciphertext, char[] password)
          Decrypts data with the supplied password.
 byte[] decryptData(byte[] ciphertext, SecretKey decryptionKey, SecretKey hmacKey)
          Decrypts data with the supplied keys.
 byte[] encryptData(byte[] plaintext, char[] password)
          Encrypts data with the supplied password.
 byte[] encryptData(byte[] plaintext, char[] password, byte[] encryptionSalt, byte[] hmacSalt, byte[] iv)
          Encrypts data with the supplied password, salt values and IV.
 byte[] encryptData(byte[] plaintext, PasswordKey encryptionKey, PasswordKey hmacKey)
          Encrypts data using pre-computed keys, producing data in the password output format (i.e.
 byte[] encryptData(byte[] plaintext, SecretKey encryptionKey, SecretKey hmacKey)
          Encrypts data with the supplied keys.
 PasswordKey getPasswordKey(char[] password)
          Generates a key from a password and a random salt.
 int getPBKDFIterations()
          Gets the number of iterations used by this JNCryptor.
 int getVersionNumber()
          Returns the version number of the data format produced by this JNCryptor.
 SecretKey keyForPassword(char[] password, byte[] salt)
          Generates a key given a password and salt using a PBKDF.
 void setPBKDFIterations(int iterations)
          Changes the number of iterations used by this JNCryptor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AES256JNCryptor

public AES256JNCryptor()
Creates a new AES256JNCryptor instance. Uses the default number of PBKDF iterations.


AES256JNCryptor

public AES256JNCryptor(int iterations)
Creates a new AES256JNCryptor instance that uses a specific number of PBKDF iterations.

Parameters:
iterations - the number of PBKDF iterations to perform
Method Detail

keyForPassword

public SecretKey keyForPassword(char[] password,
                                byte[] salt)
                         throws CryptorException
Description copied from interface: JNCryptor
Generates a key given a password and salt using a PBKDF.

Specified by:
keyForPassword in interface JNCryptor
Parameters:
password - password to use for PBKDF. Cannot be empty or null.
salt - salt for password, cannot be null
Returns:
the key
Throws:
CryptorException

getPBKDFIterations

public int getPBKDFIterations()
Description copied from interface: JNCryptor
Gets the number of iterations used by this JNCryptor.

Specified by:
getPBKDFIterations in interface JNCryptor
Returns:
the number of PBKDF2 iterations

setPBKDFIterations

public void setPBKDFIterations(int iterations)
Description copied from interface: JNCryptor
Changes the number of iterations used by this JNCryptor.

Specified by:
setPBKDFIterations in interface JNCryptor

decryptData

public byte[] decryptData(byte[] ciphertext,
                          char[] password)
                   throws CryptorException
Description copied from interface: JNCryptor
Decrypts data with the supplied password.

Specified by:
decryptData in interface JNCryptor
Parameters:
ciphertext - data to decrypt. Must be in the format described at https://github.com/RNCryptor/RNCryptor-Spec/blob/master/RNCryptor -Spec-v3.md
password - password to use for the decryption. Cannot be empty or null.
Returns:
the plain text
Throws:
InvalidHMACException
CryptorException

encryptData

public byte[] encryptData(byte[] plaintext,
                          char[] password,
                          byte[] encryptionSalt,
                          byte[] hmacSalt,
                          byte[] iv)
                   throws CryptorException
Description copied from interface: JNCryptor
Encrypts data with the supplied password, salt values and IV.

Specified by:
encryptData in interface JNCryptor
Parameters:
plaintext - the plaintext
password - the password (cannot be null or empty)
encryptionSalt - eight bytes of random salt value
hmacSalt - eight bytes of random salt value
iv - sixteen byte AES IV
Returns:
the ciphertext, in the format described at https://github.com/RNCryptor/RNCryptor-Spec/blob/master/RNCryptor- Spec-v3.md
Throws:
CryptorException - if an error occurred

encryptData

public byte[] encryptData(byte[] plaintext,
                          char[] password)
                   throws CryptorException
Description copied from interface: JNCryptor
Encrypts data with the supplied password.

Specified by:
encryptData in interface JNCryptor
Parameters:
plaintext - the data to encrypt
password - password to use for the encryption. Cannot be empty or null.
Returns:
the ciphertext, in the format described at https://github.com/RNCryptor/RNCryptor-Spec/blob/master/RNCryptor- Spec-v3.md
Throws:
CryptorException

getVersionNumber

public int getVersionNumber()
Description copied from interface: JNCryptor
Returns the version number of the data format produced by this JNCryptor.

Specified by:
getVersionNumber in interface JNCryptor
Returns:
the version number

decryptData

public byte[] decryptData(byte[] ciphertext,
                          SecretKey decryptionKey,
                          SecretKey hmacKey)
                   throws CryptorException,
                          InvalidHMACException
Description copied from interface: JNCryptor
Decrypts data with the supplied keys.

Specified by:
decryptData in interface JNCryptor
Parameters:
ciphertext - data to decrypt. Must be in the format described at https://github.com/RNCryptor/RNCryptor-Spec/blob/master/RNCryptor -Spec-v3.md
decryptionKey - the key to decrypt with
hmacKey - the key to verify the HMAC with
Returns:
the plain text
Throws:
InvalidHMACException
CryptorException

encryptData

public byte[] encryptData(byte[] plaintext,
                          SecretKey encryptionKey,
                          SecretKey hmacKey)
                   throws CryptorException
Description copied from interface: JNCryptor
Encrypts data with the supplied keys.

Specified by:
encryptData in interface JNCryptor
Parameters:
plaintext - the data to encrypt
encryptionKey - key to use for encryption
hmacKey - key to use for computing the HMAC
Returns:
the ciphertext, in the format described at https://github.com/RNCryptor/RNCryptor-Spec/blob/master/RNCryptor- Spec-v3.md
Throws:
CryptorException

getPasswordKey

public PasswordKey getPasswordKey(char[] password)
                           throws CryptorException
Description copied from interface: JNCryptor
Generates a key from a password and a random salt.

Specified by:
getPasswordKey in interface JNCryptor
Parameters:
password - password to use for PBKDF. Cannot be empty or null.
Returns:
an object containing the key and the salt
Throws:
CryptorException

encryptData

public byte[] encryptData(byte[] plaintext,
                          PasswordKey encryptionKey,
                          PasswordKey hmacKey)
                   throws CryptorException
Description copied from interface: JNCryptor
Encrypts data using pre-computed keys, producing data in the password output format (i.e. including salt values).

Specified by:
encryptData in interface JNCryptor
Parameters:
plaintext - the plaintext to encrypt
encryptionKey - the pre-computed encryption key
hmacKey - the pre-computer HMAC key
Returns:
the ciphertext, in the format described at https://github.com/RNCryptor/RNCryptor-Spec/blob/master/RNCryptor- Spec-v3.md
Throws:
CryptorException


Copyright © 2014. All Rights Reserved.