本文整理汇总了Java中org.spongycastle.crypto.params.ParametersWithIV类的典型用法代码示例。如果您正苦于以下问题:Java ParametersWithIV类的具体用法?Java ParametersWithIV怎么用?Java ParametersWithIV使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ParametersWithIV类属于org.spongycastle.crypto.params包,在下文中一共展示了ParametersWithIV类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encrypt
import org.spongycastle.crypto.params.ParametersWithIV; //导入依赖的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);
}
}
示例2: decrypt
import org.spongycastle.crypto.params.ParametersWithIV; //导入依赖的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);
}
}
示例3: encrypt
import org.spongycastle.crypto.params.ParametersWithIV; //导入依赖的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);
}
}
示例4: decrypt
import org.spongycastle.crypto.params.ParametersWithIV; //导入依赖的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);
}
}
示例5: encrypt
import org.spongycastle.crypto.params.ParametersWithIV; //导入依赖的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);
}
}
示例6: decrypt
import org.spongycastle.crypto.params.ParametersWithIV; //导入依赖的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);
}
}
示例7: initCiphers
import org.spongycastle.crypto.params.ParametersWithIV; //导入依赖的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);
}
示例8: initCiphers
import org.spongycastle.crypto.params.ParametersWithIV; //导入依赖的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);
}
示例9: aesEncrypt
import org.spongycastle.crypto.params.ParametersWithIV; //导入依赖的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);
}
}
示例10: encrypt
import org.spongycastle.crypto.params.ParametersWithIV; //导入依赖的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;
}
示例11: decrypt
import org.spongycastle.crypto.params.ParametersWithIV; //导入依赖的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;
}
示例12: decrypt
import org.spongycastle.crypto.params.ParametersWithIV; //导入依赖的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);
}
}
示例13: aesDecrypt
import org.spongycastle.crypto.params.ParametersWithIV; //导入依赖的package包/类
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;
}
示例14: aesEncrypt
import org.spongycastle.crypto.params.ParametersWithIV; //导入依赖的package包/类
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;
}
示例15: encrypt
import org.spongycastle.crypto.params.ParametersWithIV; //导入依赖的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);
}
}