Search in sources :

Example 11 with AESFastEngine

use of org.spongycastle.crypto.engines.AESFastEngine in project bitcoin-wallet by bitcoin-wallet.

the class Crypto method decryptRaw.

/**
 * Decrypt bytes previously encrypted with this class.
 *
 * @param bytesToDecode
 *            The bytes to decrypt
 * @param passwordbThe
 *            password to use for decryption
 * @return The decrypted bytes
 * @throws IOException
 */
private static byte[] decryptRaw(final byte[] bytesToDecode, final char[] password) throws IOException {
    try {
        // separate the salt and bytes to decrypt
        final byte[] salt = new byte[SALT_LENGTH];
        System.arraycopy(bytesToDecode, 0, salt, 0, SALT_LENGTH);
        final byte[] cipherBytes = new byte[bytesToDecode.length - SALT_LENGTH];
        System.arraycopy(bytesToDecode, SALT_LENGTH, cipherBytes, 0, bytesToDecode.length - SALT_LENGTH);
        final ParametersWithIV key = (ParametersWithIV) getAESPasswordKey(password, salt);
        // decrypt the message
        final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, key);
        final byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
        final int processLen = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
        final int doFinalLen = cipher.doFinal(decryptedBytes, processLen);
        return Arrays.copyOf(decryptedBytes, processLen + doFinalLen);
    } catch (final InvalidCipherTextException | DataLengthException x) {
        throw new IOException("Could not decrypt 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 12 with AESFastEngine

use of org.spongycastle.crypto.engines.AESFastEngine in project nuls by nuls-io.

the class AESEncrypt method encrypt.

/**
 * 加密
 *
 * @param plainBytes
 * @param iv
 * @param aesKey
 * @return EncryptedData
 */
public static EncryptedData encrypt(byte[] plainBytes, byte[] iv, KeyParameter aesKey) throws NulsRuntimeException {
    Utils.checkNotNull(plainBytes);
    Utils.checkNotNull(aesKey);
    try {
        if (iv == null) {
            iv = new byte[16];
            SECURE_RANDOM.nextBytes(iv);
        }
        ParametersWithIV keyWithIv = new ParametersWithIV(aesKey, 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 NulsRuntimeException(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) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 13 with AESFastEngine

use of org.spongycastle.crypto.engines.AESFastEngine in project nuls by nuls-io.

the class AESEncrypt method decrypt.

/**
 * 解密
 *
 * @param dataToDecrypt
 * @param aesKey
 * @return byte[]
 * @throws NulsRuntimeException
 */
public static byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter aesKey) throws NulsRuntimeException {
    Utils.checkNotNull(dataToDecrypt);
    Utils.checkNotNull(aesKey);
    try {
        ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(aesKey.getKey()), dataToDecrypt.getInitialisationVector());
        // Decrypt the message.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, keyWithIv);
        byte[] cipherBytes = dataToDecrypt.getEncryptedBytes();
        byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
        final int length1 = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
        final int length2 = cipher.doFinal(decryptedBytes, length1);
        return Arrays.copyOf(decryptedBytes, length1 + length2);
    } catch (Exception e) {
        throw new NulsRuntimeException(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) KeyParameter(org.spongycastle.crypto.params.KeyParameter) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 14 with AESFastEngine

use of org.spongycastle.crypto.engines.AESFastEngine in project Zom-Android by zom.

the class OtrCryptoEngineImpl method aesDecrypt.

public byte[] aesDecrypt(byte[] key, byte[] ctr, byte[] b) throws OtrCryptoException {
    AESFastEngine aesDec = new AESFastEngine();
    SICBlockCipher sicAesDec = new SICBlockCipher(aesDec);
    BufferedBlockCipher bufSicAesDec = new BufferedBlockCipher(sicAesDec);
    // Create initial counter value 0.
    if (ctr == null)
        ctr = ZERO_CTR;
    bufSicAesDec.init(false, new ParametersWithIV(new KeyParameter(key), ctr));
    byte[] aesOutLwDec = new byte[b.length];
    int done = bufSicAesDec.processBytes(b, 0, b.length, aesOutLwDec, 0);
    try {
        bufSicAesDec.doFinal(aesOutLwDec, done);
    } catch (Exception e) {
        throw new OtrCryptoException(e);
    }
    return aesOutLwDec;
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) KeyParameter(org.spongycastle.crypto.params.KeyParameter) SICBlockCipher(org.spongycastle.crypto.modes.SICBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 15 with AESFastEngine

use of org.spongycastle.crypto.engines.AESFastEngine in project Zom-Android by zom.

the class OtrCryptoEngineImpl method aesEncrypt.

public byte[] aesEncrypt(byte[] key, byte[] ctr, byte[] b) throws OtrCryptoException {
    AESFastEngine aesEnc = new AESFastEngine();
    SICBlockCipher sicAesEnc = new SICBlockCipher(aesEnc);
    BufferedBlockCipher bufSicAesEnc = new BufferedBlockCipher(sicAesEnc);
    // Create initial counter value 0.
    if (ctr == null)
        ctr = ZERO_CTR;
    bufSicAesEnc.init(true, new ParametersWithIV(new KeyParameter(key), ctr));
    byte[] aesOutLwEnc = new byte[b.length];
    int done = bufSicAesEnc.processBytes(b, 0, b.length, aesOutLwEnc, 0);
    try {
        bufSicAesEnc.doFinal(aesOutLwEnc, done);
    } catch (Exception e) {
        throw new OtrCryptoException(e);
    }
    return aesOutLwEnc;
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) KeyParameter(org.spongycastle.crypto.params.KeyParameter) SICBlockCipher(org.spongycastle.crypto.modes.SICBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

AESFastEngine (org.spongycastle.crypto.engines.AESFastEngine)22 SICBlockCipher (org.spongycastle.crypto.modes.SICBlockCipher)13 BufferedBlockCipher (org.spongycastle.crypto.BufferedBlockCipher)12 ParametersWithIV (org.spongycastle.crypto.params.ParametersWithIV)11 KeyParameter (org.spongycastle.crypto.params.KeyParameter)10 CBCBlockCipher (org.spongycastle.crypto.modes.CBCBlockCipher)6 PaddedBufferedBlockCipher (org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher)6 ECDHBasicAgreement (org.spongycastle.crypto.agreement.ECDHBasicAgreement)5 SHA256Digest (org.spongycastle.crypto.digests.SHA256Digest)5 HMac (org.spongycastle.crypto.macs.HMac)5 IOException (java.io.IOException)4 Test (org.junit.Test)4 InvalidCipherTextException (org.spongycastle.crypto.InvalidCipherTextException)4 ECPoint (org.spongycastle.math.ec.ECPoint)4 ConcatKDFBytesGenerator (org.ethereum.ConcatKDFBytesGenerator)3 BCECPrivateKey (org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)3 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 SecureRandom (java.security.SecureRandom)2