當前位置: 首頁>>代碼示例>>Java>>正文


Java CBCBlockCipher類代碼示例

本文整理匯總了Java中org.spongycastle.crypto.modes.CBCBlockCipher的典型用法代碼示例。如果您正苦於以下問題:Java CBCBlockCipher類的具體用法?Java CBCBlockCipher怎麽用?Java CBCBlockCipher使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


CBCBlockCipher類屬於org.spongycastle.crypto.modes包,在下文中一共展示了CBCBlockCipher類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: encrypt

import org.spongycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
/**
 * 加密
 *
 * @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);
    }
}
 
開發者ID:nuls-io,項目名稱:nuls,代碼行數:33,代碼來源:AESEncrypt.java

示例2: decrypt

import org.spongycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
/**
 * 解密
 *
 * @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);
    }
}
 
開發者ID:nuls-io,項目名稱:nuls,代碼行數:30,代碼來源:AESEncrypt.java

示例3: encrypt

import org.spongycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
/**
 * 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);
    }
}
 
開發者ID:rsksmart,項目名稱:rskj,代碼行數:28,代碼來源:KeyCrypterAes.java

示例4: decrypt

import org.spongycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
/**
 * Decrypt bytes previously encrypted with this class.
 *
 * @param dataToDecrypt    The data to decrypt
 * @param key              The AES key to use for decryption
 * @return                 The decrypted bytes
 * @throws                 KeyCrypterException if bytes could not be decrypted
 */
@Override
public byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter key) {
    checkNotNull(dataToDecrypt);
    checkNotNull(key);

    try {
        ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(key.getKey()), dataToDecrypt.initialisationVector);

        // Decrypt the message.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, keyWithIv);

        byte[] cipherBytes = dataToDecrypt.encryptedBytes;
        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 KeyCrypterException("Could not decrypt bytes", e);
    }
}
 
開發者ID:rsksmart,項目名稱:rskj,代碼行數:31,代碼來源:KeyCrypterAes.java

示例5: encrypt

import org.spongycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
/**
 * Password based encryption using AES - CBC 256 bits.
 */
@Override
public EncryptedData encrypt(byte[] plainBytes, KeyParameter aesKey) throws KeyCrypterException {
    checkNotNull(plainBytes);
    checkNotNull(aesKey);

    try {
        // Generate iv - each encryption call has a different iv.
        byte[] iv = new byte[BLOCK_LENGTH];
        secureRandom.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 KeyCrypterException("Could not encrypt bytes.", e);
    }
}
 
開發者ID:creativechain,項目名稱:creacoinj,代碼行數:28,代碼來源:KeyCrypterScrypt.java

示例6: decrypt

import org.spongycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
/**
 * Decrypt bytes previously encrypted with this class.
 *
 * @param dataToDecrypt    The data to decrypt
 * @param aesKey           The AES key to use for decryption
 * @return                 The decrypted bytes
 * @throws                 KeyCrypterException if bytes could not be decrypted
 */
@Override
public byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter aesKey) throws KeyCrypterException {
    checkNotNull(dataToDecrypt);
    checkNotNull(aesKey);

    try {
        ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(aesKey.getKey()), dataToDecrypt.initialisationVector);

        // Decrypt the message.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, keyWithIv);

        byte[] cipherBytes = dataToDecrypt.encryptedBytes;
        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 KeyCrypterException("Could not decrypt bytes", e);
    }
}
 
開發者ID:creativechain,項目名稱:creacoinj,代碼行數:31,代碼來源:KeyCrypterScrypt.java

示例7: initCiphers

import org.spongycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
private void initCiphers(final byte[] key, final byte[] iv) {

		// get the keyBytes
		this.keyBytes = new byte[key.length];
		System.arraycopy(key, 0, this.keyBytes, 0, key.length);

		this.keyP = new KeyParameter(this.keyBytes);

		// get the IV
		this.IV = new byte[blockSize];
		System.arraycopy(iv, 0, this.IV, 0, this.IV.length);

		// create the ciphers
		// AES block cipher in CBC mode with ISO7816d4 padding
		this.encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
				new AESFastEngine()), new ISO7816d4Padding());

		this.decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
				new AESFastEngine()), new ISO7816d4Padding());

		// create the IV parameter
		final ParametersWithIV parameterIV = new ParametersWithIV(this.keyP, this.IV);

		this.encryptCipher.init(true, parameterIV);
		this.decryptCipher.init(false, parameterIV);
	}
 
開發者ID:MiFirma,項目名稱:mi-firma-android,代碼行數:27,代碼來源:AmAESCrypto.java

示例8: initCiphers

import org.spongycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
private void initCiphers(final byte[] key, final byte[] iv) {
	// get the keyBytes
	this.keyBytes = new byte[key.length];
	System.arraycopy(key, 0, this.keyBytes, 0, key.length);

	// get the IV
	this.IV = new byte[blockSize];
	System.arraycopy(iv, 0, this.IV, 0, iv.length);

	this.keyP = new KeyParameter(this.keyBytes);

	this.encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
			new DESedeEngine()), new ISO7816d4Padding());
	this.decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
			new DESedeEngine()), new ISO7816d4Padding());

	// create the IV parameter
	final ParametersWithIV parameterIV = new ParametersWithIV(this.keyP, this.IV);

	this.encryptCipher.init(true, parameterIV);
	this.decryptCipher.init(false, parameterIV);
}
 
開發者ID:MiFirma,項目名稱:mi-firma-android,代碼行數:23,代碼來源:AmDESCrypto.java

示例9: aesEncrypt

import org.spongycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
public static byte[] aesEncrypt(byte[] plaintext, byte[] myPrivateKey, byte[] theirPublicKey, byte[] nonce) {
    try {
        byte[] dhSharedSecret = new byte[32];
        Curve25519.curve(dhSharedSecret, myPrivateKey, theirPublicKey);
        for (int i = 0; i < 32; i++) {
            dhSharedSecret[i] ^= nonce[i];
        }
        byte[] key = sha256().digest(dhSharedSecret);
        byte[] iv = new byte[16];
        secureRandom.get().nextBytes(iv);
        PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(
                new AESEngine()));
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
        aes.init(true, ivAndKey);
        byte[] output = new byte[aes.getOutputSize(plaintext.length)];
        int ciphertextLength = aes.processBytes(plaintext, 0, plaintext.length, output, 0);
        ciphertextLength += aes.doFinal(output, ciphertextLength);
        byte[] result = new byte[iv.length + ciphertextLength];
        System.arraycopy(iv, 0, result, 0, iv.length);
        System.arraycopy(output, 0, result, iv.length, ciphertextLength);
        return result;
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
開發者ID:filipnyquist,項目名稱:lbry-android,代碼行數:26,代碼來源:Crypto.java

示例10: encrypt

import org.spongycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
protected byte[] encrypt(byte[] data) {
    // 16 bytes is the IV size for AES256
    try {
        SecretKey key = loadKey();

        // Random IV
        SecureRandom rng = new SecureRandom();
        byte[] ivBytes = new byte[16];                                                                  // 16 bytes is the IV size for AES256
        rng.nextBytes(ivBytes);

        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(true, new ParametersWithIV(new KeyParameter(key.getEncoded()), ivBytes));

        byte[] encryptedData = cipherData(cipher, data);
        byte[] encryptedDataWithIV = new byte[encryptedData.length + ivBytes.length];                   // Make room for IV
        System.arraycopy(ivBytes, 0, encryptedDataWithIV, 0, ivBytes.length);                           // Add IV
        System.arraycopy(encryptedData, 0, encryptedDataWithIV, ivBytes.length, encryptedData.length);  // Then the encrypted data
        return encryptedDataWithIV;
    }
    catch(InvalidCipherTextException e) {
        Log.e(TAG, "Can't encrypt data", e);
    }
    return null;
}
 
開發者ID:kalemontes,項目名稱:OIDCAndroidLib,代碼行數:25,代碼來源:SensitiveDataPreApi23.java

示例11: decrypt

import org.spongycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
protected byte[] decrypt(byte[] data) {

        try {
            SecretKey key = loadKey();

            byte[] ivBytes = new byte[16];                                                                  // 16 bytes is the IV size for AES256
            System.arraycopy(data, 0, ivBytes, 0, ivBytes.length);                                          // Get IV from data
            byte[] dataWithoutIV = new byte[data.length - ivBytes.length];                                  // Remove the room made for the IV
            System.arraycopy(data, ivBytes.length, dataWithoutIV, 0, dataWithoutIV.length);                 // Then the encrypted data

            PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
            cipher.init(false, new ParametersWithIV(new KeyParameter(key.getEncoded()), ivBytes));

            return cipherData(cipher, dataWithoutIV);
        }
        catch(InvalidCipherTextException e) {
            Log.e(TAG, "Can't decrypt data", e);
        }
        return null;
    }
 
開發者ID:kalemontes,項目名稱:OIDCAndroidLib,代碼行數:21,代碼來源:SensitiveDataPreApi23.java

示例12: decrypt

import org.spongycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
/**
 * Decrypt bytes previously encrypted with this class.
 *
 * @param privateKeyToDecode    The private key to decrypt
 * @param aesKey           The AES key to use for decryption
 * @return                 The decrypted bytes
 * @throws                 KeyCrypterException if bytes could not be decoded to a valid key
 */
@Override
public byte[] decrypt(EncryptedData privateKeyToDecode, KeyParameter aesKey) throws KeyCrypterException {
    checkNotNull(privateKeyToDecode);
    checkNotNull(aesKey);

    try {
        ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(aesKey.getKey()), privateKeyToDecode.initialisationVector);

        // Decrypt the message.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, keyWithIv);

        byte[] cipherBytes = privateKeyToDecode.encryptedBytes;
        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 KeyCrypterException("Could not decrypt bytes", e);
    }
}
 
開發者ID:HashEngineering,項目名稱:namecoinj,代碼行數:31,代碼來源:KeyCrypterScrypt.java

示例13: encrypt

import org.spongycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
/**
 * Password based encryption using AES - CBC 256 bits.
 */
@Override
public EncryptedPrivateKey encrypt(byte[] plainBytes, KeyParameter aesKey) throws KeyCrypterException {
    checkNotNull(plainBytes);
    checkNotNull(aesKey);

    try {
        // Generate iv - each encryption call has a different iv.
        byte[] iv = new byte[BLOCK_LENGTH];
        secureRandom.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 EncryptedPrivateKey(iv, Arrays.copyOf(encryptedBytes, length1 + length2));
    } catch (Exception e) {
        throw new KeyCrypterException("Could not encrypt bytes.", e);
    }
}
 
開發者ID:coinspark,項目名稱:sparkbit-bitcoinj,代碼行數:28,代碼來源:KeyCrypterScrypt.java

示例14: decrypt

import org.spongycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
/**
 * Decrypt bytes previously encrypted with this class.
 *
 * @param privateKeyToDecode    The private key to decrypt
 * @param aesKey           The AES key to use for decryption
 * @return                 The decrypted bytes
 * @throws                 KeyCrypterException if bytes could not be decoded to a valid key
 */
@Override
public byte[] decrypt(EncryptedPrivateKey privateKeyToDecode, KeyParameter aesKey) throws KeyCrypterException {
    checkNotNull(privateKeyToDecode);
    checkNotNull(aesKey);

    try {
        ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(aesKey.getKey()), privateKeyToDecode.getInitialisationVector());

        // Decrypt the message.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, keyWithIv);

        byte[] cipherBytes = privateKeyToDecode.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 KeyCrypterException("Could not decrypt bytes", e);
    }
}
 
開發者ID:coinspark,項目名稱:sparkbit-bitcoinj,代碼行數:31,代碼來源:KeyCrypterScrypt.java

示例15: encrypt

import org.spongycastle.crypto.modes.CBCBlockCipher; //導入依賴的package包/類
/**
 * Password based encryption using AES - CBC 256 bits.
 * 
 * @param plainTextAsBytes
 *            The bytes to encrypt
 * @param password
 *            The password to use for encryption
 * @return SALT_LENGTH bytes of salt followed by the encrypted bytes.
 * @throws KeyCrypterException
 */
public byte[] encrypt(byte[] plainTextAsBytes, CharSequence password) throws KeyCrypterException {
    try {
        // Generate salt - each encryption call has a different salt.
        byte[] salt = new byte[SALT_LENGTH];
        secureRandom.nextBytes(salt);
 
        ParametersWithIV key = (ParametersWithIV) getAESPasswordKey(password, salt);

        // The following code uses an AES cipher to encrypt the message.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(true, key);
        byte[] encryptedBytes = new byte[cipher.getOutputSize(plainTextAsBytes.length)];
        final int processLength = cipher.processBytes(plainTextAsBytes, 0, plainTextAsBytes.length, encryptedBytes, 0);
        final int doFinalLength = cipher.doFinal(encryptedBytes, processLength);

        // The result bytes are the SALT_LENGTH bytes followed by the encrypted bytes.
        return concat(salt, Arrays.copyOf(encryptedBytes, processLength + doFinalLength));
    } catch (Exception e) {
        throw new KeyCrypterException("Could not encrypt bytes '" + Utils.bytesToHexString(plainTextAsBytes) + "'", e);
    }
}
 
開發者ID:coinspark,項目名稱:sparkbit,代碼行數:32,代碼來源:KeyCrypterOpenSSL.java


注:本文中的org.spongycastle.crypto.modes.CBCBlockCipher類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。