Search in sources :

Example 6 with AESFastEngine

use of org.spongycastle.crypto.engines.AESFastEngine in project rskj by rsksmart.

the class FrameCodec method makeMacCipher.

private AESFastEngine makeMacCipher() {
    // Stateless AES encryption
    AESFastEngine macc = new AESFastEngine();
    macc.init(true, new KeyParameter(mac));
    return macc;
}
Also used : KeyParameter(org.spongycastle.crypto.params.KeyParameter) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine)

Example 7 with AESFastEngine

use of org.spongycastle.crypto.engines.AESFastEngine 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 8 with AESFastEngine

use of org.spongycastle.crypto.engines.AESFastEngine 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 9 with AESFastEngine

use of org.spongycastle.crypto.engines.AESFastEngine 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)

Example 10 with AESFastEngine

use of org.spongycastle.crypto.engines.AESFastEngine in project libsignal-service-java by signalapp.

the class ProfileCipher method decryptName.

public byte[] decryptName(byte[] input) throws InvalidCiphertextException {
    try {
        if (input.length < 12 + 16 + 1) {
            throw new InvalidCiphertextException("Too short: " + input.length);
        }
        byte[] nonce = new byte[12];
        System.arraycopy(input, 0, nonce, 0, nonce.length);
        GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
        cipher.init(false, new AEADParameters(new KeyParameter(key), 128, nonce));
        byte[] paddedPlaintextOne = new byte[cipher.getUpdateOutputSize(input.length - 12)];
        cipher.processBytes(input, 12, input.length - 12, paddedPlaintextOne, 0);
        byte[] paddedPlaintextTwo = new byte[cipher.getOutputSize(0)];
        cipher.doFinal(paddedPlaintextTwo, 0);
        byte[] paddedPlaintext = ByteUtil.combine(paddedPlaintextOne, paddedPlaintextTwo);
        int plaintextLength = 0;
        for (int i = paddedPlaintext.length - 1; i >= 0; i--) {
            if (paddedPlaintext[i] != (byte) 0x00) {
                plaintextLength = i + 1;
                break;
            }
        }
        byte[] plaintext = new byte[plaintextLength];
        System.arraycopy(paddedPlaintext, 0, plaintext, 0, plaintextLength);
        return plaintext;
    } catch (InvalidCipherTextException e) {
        throw new InvalidCiphertextException(e);
    }
}
Also used : InvalidCipherTextException(org.spongycastle.crypto.InvalidCipherTextException) AEADParameters(org.spongycastle.crypto.params.AEADParameters) KeyParameter(org.spongycastle.crypto.params.KeyParameter) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) GCMBlockCipher(org.spongycastle.crypto.modes.GCMBlockCipher)

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