Class KeyCrypterScrypt

  • All Implemented Interfaces:
    KeyCrypter

    public class KeyCrypterScrypt
    extends java.lang.Object
    implements KeyCrypter

    This class encrypts and decrypts byte arrays and strings using scrypt as the key derivation function and AES for the encryption.

    You can use this class to:

    1) Using a user password, create an AES key that can encrypt and decrypt your private keys. To convert the password to the AES key, scrypt is used. This is an algorithm resistant to brute force attacks. You can use the ScryptParameters to tune how difficult you want this to be generation to be.

    2) Using the AES Key generated above, you then can encrypt and decrypt any bytes using the AES symmetric cipher. Eight bytes of salt is used to prevent dictionary attacks.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BLOCK_LENGTH
      The size of an AES block in bytes.
      static int KEY_LENGTH
      Key length in bytes.
      static int SALT_LENGTH
      The length of the salt used.
    • Constructor Summary

      Constructors 
      Constructor Description
      KeyCrypterScrypt()
      Encryption/Decryption using default parameters and a random salt.
      KeyCrypterScrypt​(int iterations)
      Encryption/Decryption using custom number of iterations parameters and a random salt.
      KeyCrypterScrypt​(org.bitcoinj.protobuf.wallet.Protos.ScryptParameters scryptParameters)
      Encryption/ Decryption using specified Scrypt parameters.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] decrypt​(EncryptedData dataToDecrypt, AesKey aesKey)
      Decrypt bytes previously encrypted with this class.
      AesKey deriveKey​(java.lang.CharSequence password)
      Generate AES key.
      EncryptedData encrypt​(byte[] plainBytes, AesKey aesKey)
      Password based encryption using AES - CBC 256 bits.
      boolean equals​(java.lang.Object o)  
      org.bitcoinj.protobuf.wallet.Protos.ScryptParameters getScryptParameters()  
      org.bitcoinj.protobuf.wallet.Protos.Wallet.EncryptionType getUnderstoodEncryptionType()
      Return the EncryptionType enum value which denotes the type of encryption/ decryption that this KeyCrypter can understand.
      int hashCode()  
      static byte[] randomSalt()
      Returns SALT_LENGTH (8) bytes of random data
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • BLOCK_LENGTH

        public static final int BLOCK_LENGTH
        The size of an AES block in bytes. This is also the length of the initialisation vector.
        See Also:
        Constant Field Values
      • SALT_LENGTH

        public static final int SALT_LENGTH
        The length of the salt used.
        See Also:
        Constant Field Values
    • Constructor Detail

      • KeyCrypterScrypt

        public KeyCrypterScrypt()
        Encryption/Decryption using default parameters and a random salt.
      • KeyCrypterScrypt

        public KeyCrypterScrypt​(int iterations)
        Encryption/Decryption using custom number of iterations parameters and a random salt. As of August 2016, a useful value for mobile devices is 4096 (derivation takes about 1 second).
        Parameters:
        iterations - number of scrypt iterations
      • KeyCrypterScrypt

        public KeyCrypterScrypt​(org.bitcoinj.protobuf.wallet.Protos.ScryptParameters scryptParameters)
        Encryption/ Decryption using specified Scrypt parameters.
        Parameters:
        scryptParameters - ScryptParameters to use
        Throws:
        java.lang.NullPointerException - if the scryptParameters or any of its N, R or P is null.
    • Method Detail

      • randomSalt

        public static byte[] randomSalt()
        Returns SALT_LENGTH (8) bytes of random data
      • deriveKey

        public AesKey deriveKey​(java.lang.CharSequence password)
                         throws KeyCrypterException
        Generate AES key. This is a very slow operation compared to encrypt/ decrypt so it is normally worth caching the result.
        Specified by:
        deriveKey in interface KeyCrypter
        Parameters:
        password - The password to use in key generation
        Returns:
        The AesKey containing the created AES key
        Throws:
        KeyCrypterException
      • encrypt

        public EncryptedData encrypt​(byte[] plainBytes,
                                     AesKey aesKey)
                              throws KeyCrypterException
        Password based encryption using AES - CBC 256 bits.
        Specified by:
        encrypt in interface KeyCrypter
        Returns:
        encryptedPrivateKey An encryptedPrivateKey containing the encrypted bytes and an initialisation vector.
        Throws:
        KeyCrypterException - if encryption was unsuccessful
      • decrypt

        public byte[] decrypt​(EncryptedData dataToDecrypt,
                              AesKey aesKey)
                       throws KeyCrypterException
        Decrypt bytes previously encrypted with this class.
        Specified by:
        decrypt in interface KeyCrypter
        Parameters:
        dataToDecrypt - The data to decrypt
        aesKey - The AES key to use for decryption
        Returns:
        The decrypted bytes
        Throws:
        KeyCrypterException - if bytes could not be decrypted
      • getScryptParameters

        public org.bitcoinj.protobuf.wallet.Protos.ScryptParameters getScryptParameters()
      • getUnderstoodEncryptionType

        public org.bitcoinj.protobuf.wallet.Protos.Wallet.EncryptionType getUnderstoodEncryptionType()
        Return the EncryptionType enum value which denotes the type of encryption/ decryption that this KeyCrypter can understand.
        Specified by:
        getUnderstoodEncryptionType in interface KeyCrypter
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object