Search in sources :

Example 1 with ParametersWithIV

use of org.spongycastle.crypto.params.ParametersWithIV in project aion by aionnetwork.

the class ECKeySecp256k1 method decryptAES.

/**
 * Decrypt cipher by AES in SIC(also know as CTR) mode
 *
 * @param cipher
 *            -proper cipher
 * @return decrypted cipher, equal length to the cipher.
 * @deprecated should not use EC private scalar value as an AES key
 */
public byte[] decryptAES(byte[] cipher) {
    if (privKey == null) {
        throw new MissingPrivateKeyException();
    }
    if (!(privKey instanceof BCECPrivateKey)) {
        throw new UnsupportedOperationException("Cannot use the private key as an AES key");
    }
    AESFastEngine engine = new AESFastEngine();
    SICBlockCipher ctrEngine = new SICBlockCipher(engine);
    KeyParameter key = new KeyParameter(BigIntegers.asUnsignedByteArray(((BCECPrivateKey) privKey).getD()));
    ParametersWithIV params = new ParametersWithIV(key, new byte[16]);
    ctrEngine.init(false, params);
    int i = 0;
    byte[] out = new byte[cipher.length];
    while (i < cipher.length) {
        ctrEngine.processBlock(cipher, i, out, i);
        i += engine.getBlockSize();
        if (cipher.length - i < engine.getBlockSize()) {
            break;
        }
    }
    // process left bytes
    if (cipher.length - i > 0) {
        byte[] tmpBlock = new byte[16];
        System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i);
        ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0);
        System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
    }
    return out;
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) KeyParameter(org.spongycastle.crypto.params.KeyParameter) SICBlockCipher(org.spongycastle.crypto.modes.SICBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) ECPoint(org.spongycastle.math.ec.ECPoint) BCECPrivateKey(org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)

Example 2 with ParametersWithIV

use of org.spongycastle.crypto.params.ParametersWithIV in project rskj by rsksmart.

the class KeyCrypterAes method encrypt.

/**
 * Password based encryption using AES - CBC 256 bits.
 */
@Override
public EncryptedData encrypt(byte[] plainBytes, KeyParameter key) {
    checkNotNull(plainBytes);
    checkNotNull(key);
    try {
        // Generate iv - each encryption call has a different iv.
        byte[] iv = new byte[BLOCK_LENGTH];
        secureRandom.nextBytes(iv);
        ParametersWithIV keyWithIv = new ParametersWithIV(key, iv);
        // Encrypt using AES.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(true, keyWithIv);
        byte[] encryptedBytes = new byte[cipher.getOutputSize(plainBytes.length)];
        final int length1 = cipher.processBytes(plainBytes, 0, plainBytes.length, encryptedBytes, 0);
        final int length2 = cipher.doFinal(encryptedBytes, length1);
        return new EncryptedData(iv, Arrays.copyOf(encryptedBytes, length1 + length2));
    } catch (Exception e) {
        throw new KeyCrypterException("Could not encrypt bytes.", e);
    }
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine)

Example 3 with ParametersWithIV

use of org.spongycastle.crypto.params.ParametersWithIV in project bitcoin-wallet by bitcoin-wallet.

the class Crypto method encryptRaw.

/**
 * Password based encryption using AES - CBC 256 bits.
 *
 * @param plainBytes
 *            The bytes to encrypt
 * @param password
 *            The password to use for encryption
 * @return SALT_LENGTH bytes of salt followed by the encrypted bytes.
 * @throws IOException
 */
private static byte[] encryptRaw(final byte[] plainTextAsBytes, final char[] password) throws IOException {
    try {
        // Generate salt - each encryption call has a different salt.
        final byte[] salt = new byte[SALT_LENGTH];
        secureRandom.nextBytes(salt);
        final ParametersWithIV key = (ParametersWithIV) getAESPasswordKey(password, salt);
        // The following code uses an AES cipher to encrypt the message.
        final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(true, key);
        final byte[] encryptedBytes = new byte[cipher.getOutputSize(plainTextAsBytes.length)];
        final int processLen = cipher.processBytes(plainTextAsBytes, 0, plainTextAsBytes.length, encryptedBytes, 0);
        final int doFinalLen = cipher.doFinal(encryptedBytes, processLen);
        // The result bytes are the SALT_LENGTH bytes followed by the encrypted bytes.
        return concat(salt, Arrays.copyOf(encryptedBytes, processLen + doFinalLen));
    } catch (final InvalidCipherTextException | DataLengthException x) {
        throw new IOException("Could not encrypt bytes", x);
    }
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) InvalidCipherTextException(org.spongycastle.crypto.InvalidCipherTextException) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) DataLengthException(org.spongycastle.crypto.DataLengthException) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) IOException(java.io.IOException)

Example 4 with ParametersWithIV

use of org.spongycastle.crypto.params.ParametersWithIV in project bitcoin-wallet by bitcoin-wallet.

the class Crypto method getAESPasswordKey.

/**
 * Get password and generate key and iv.
 *
 * @param password
 *            The password to use in key generation
 * @param salt
 *            The salt to use in key generation
 * @return The CipherParameters containing the created key
 */
private static CipherParameters getAESPasswordKey(final char[] password, final byte[] salt) {
    final PBEParametersGenerator generator = new OpenSSLPBEParametersGenerator();
    generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(password), salt, NUMBER_OF_ITERATIONS);
    final ParametersWithIV key = (ParametersWithIV) generator.generateDerivedParameters(KEY_LENGTH, IV_LENGTH);
    return key;
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) PBEParametersGenerator(org.spongycastle.crypto.PBEParametersGenerator) OpenSSLPBEParametersGenerator(org.spongycastle.crypto.generators.OpenSSLPBEParametersGenerator) OpenSSLPBEParametersGenerator(org.spongycastle.crypto.generators.OpenSSLPBEParametersGenerator)

Example 5 with ParametersWithIV

use of org.spongycastle.crypto.params.ParametersWithIV in project java-tron by tronprotocol.

the class ECKey method decryptAES.

/**
 * Decrypt cipher by AES in SIC(also know as CTR) mode
 *
 * @param cipher -proper cipher
 * @return decrypted cipher, equal length to the cipher.
 * @deprecated should not use EC private scalar value as an AES key
 */
public byte[] decryptAES(byte[] cipher) {
    if (privKey == null) {
        throw new MissingPrivateKeyException();
    }
    if (!(privKey instanceof BCECPrivateKey)) {
        throw new UnsupportedOperationException("Cannot use the private " + "key as an AES key");
    }
    AESFastEngine engine = new AESFastEngine();
    SICBlockCipher ctrEngine = new SICBlockCipher(engine);
    KeyParameter key = new KeyParameter(BigIntegers.asUnsignedByteArray(((BCECPrivateKey) privKey).getD()));
    ParametersWithIV params = new ParametersWithIV(key, new byte[16]);
    ctrEngine.init(false, params);
    int i = 0;
    byte[] out = new byte[cipher.length];
    while (i < cipher.length) {
        ctrEngine.processBlock(cipher, i, out, i);
        i += engine.getBlockSize();
        if (cipher.length - i < engine.getBlockSize()) {
            break;
        }
    }
    // process left bytes
    if (cipher.length - i > 0) {
        byte[] tmpBlock = new byte[16];
        System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i);
        ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0);
        System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
    }
    return out;
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) KeyParameter(org.spongycastle.crypto.params.KeyParameter) SICBlockCipher(org.spongycastle.crypto.modes.SICBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) ECPoint(org.spongycastle.math.ec.ECPoint) BCECPrivateKey(org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)

Aggregations

ParametersWithIV (org.spongycastle.crypto.params.ParametersWithIV)17 KeyParameter (org.spongycastle.crypto.params.KeyParameter)12 AESFastEngine (org.spongycastle.crypto.engines.AESFastEngine)11 BufferedBlockCipher (org.spongycastle.crypto.BufferedBlockCipher)8 CBCBlockCipher (org.spongycastle.crypto.modes.CBCBlockCipher)8 PaddedBufferedBlockCipher (org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher)7 SICBlockCipher (org.spongycastle.crypto.modes.SICBlockCipher)6 IOException (java.io.IOException)4 BCECPrivateKey (org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)3 ECPoint (org.spongycastle.math.ec.ECPoint)3 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 DataLengthException (org.spongycastle.crypto.DataLengthException)2 InvalidCipherTextException (org.spongycastle.crypto.InvalidCipherTextException)2 StreamCipher (org.spongycastle.crypto.StreamCipher)2 PBEParametersGenerator (org.spongycastle.crypto.PBEParametersGenerator)1 AESEngine (org.spongycastle.crypto.engines.AESEngine)1 ChaCha7539Engine (org.spongycastle.crypto.engines.ChaCha7539Engine)1 Salsa20Engine (org.spongycastle.crypto.engines.Salsa20Engine)1