本文整理汇总了Java中org.bouncycastle.crypto.modes.GCMBlockCipher.init方法的典型用法代码示例。如果您正苦于以下问题:Java GCMBlockCipher.init方法的具体用法?Java GCMBlockCipher.init怎么用?Java GCMBlockCipher.init使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.crypto.modes.GCMBlockCipher
的用法示例。
在下文中一共展示了GCMBlockCipher.init方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: runTestCase
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入方法依赖的package包/类
private void runTestCase(
GCMMultiplier encM,
GCMMultiplier decM,
String testName,
byte[] K,
byte[] IV,
byte[] A,
byte[] SA,
byte[] P,
byte[] C,
byte[] T)
throws InvalidCipherTextException
{
AEADParameters parameters = new AEADParameters(new KeyParameter(K), T.length * 8, IV, A);
GCMBlockCipher encCipher = initCipher(encM, true, parameters);
GCMBlockCipher decCipher = initCipher(decM, false, parameters);
checkTestCase(encCipher, decCipher, testName, SA, P, C, T);
checkTestCase(encCipher, decCipher, testName + " (reused)", SA, P, C, T);
// Key reuse
AEADParameters keyReuseParams = AEADTestUtil.reuseKey(parameters);
encCipher.init(true, keyReuseParams);
decCipher.init(false, keyReuseParams);
checkTestCase(encCipher, decCipher, testName + " (key reuse)", SA, P, C, T);
}
示例2: genKeyId
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入方法依赖的package包/类
private AesKeyId genKeyId() {
try {
final GCMBlockCipher gcm = new GCMBlockCipher(new AESFastEngine());
gcm.init(true, new AEADParameters(
new KeyParameter(key), 128, KEYID_AD, KEYID_AD));
final byte[] ciphertext = new byte[gcm.getOutputSize(ZERO_BYTES.length)];
final int resp = gcm.processBytes(ZERO_BYTES, 0, ZERO_BYTES.length,
ciphertext, 0);
gcm.doFinal(ciphertext, resp);
return new AesKeyId(ciphertext);
} catch (final InvalidCipherTextException e) {
// Should be impossible when we're encrypting!
throw new RuntimeException(
"Unexpected behaviour in crypto libraries", e);
}
}
示例3: decrypt
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入方法依赖的package包/类
public SequenceItem decrypt(final ConverterCatalog r, final AesPacket packet)
throws InvalidInputException {
final GCMBlockCipher gcm = new GCMBlockCipher(new AESFastEngine());
gcm.init(false, new AEADParameters(
new KeyParameter(key), 128, packet.nonce, ZERO_BYTES));
final byte[] newtext = new byte[
gcm.getOutputSize(packet.ciphertext.length)];
final int pp = gcm.processBytes(packet.ciphertext, 0,
packet.ciphertext.length, newtext, 0);
try {
gcm.doFinal(newtext, pp);
} catch (final InvalidCipherTextException e) {
throw new CryptographyException(e);
}
return ConvertUtils.fromBytes(r, SequenceItem.class, newtext);
}
示例4: decrypt
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入方法依赖的package包/类
public static byte[] decrypt(byte[] key, byte[] data) {
// TODO utilize GCMAES#decrypt method
try {
if (data.length < NONCE_LENGTH + TAG_LENGTH) {
throw new IllegalArgumentException("data packet too short");
}
int cipherTextLength = data.length - NONCE_LENGTH - TAG_LENGTH;
byte[] nonce = Arrays.copyOf(data, NONCE_LENGTH);
GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
AEADParameters parameters = new AEADParameters(new KeyParameter(key), TAG_LENGTH * 8, nonce);
cipher.init(false, parameters);
byte[] out = new byte[cipher.getOutputSize(cipherTextLength + TAG_LENGTH)];
int pos = cipher.processBytes(data, NONCE_LENGTH, data.length - NONCE_LENGTH, out, 0);
pos += cipher.doFinal(out, pos);
return Arrays.copyOf(out, pos);
} catch (IllegalStateException | InvalidCipherTextException ex) {
throw new IllegalArgumentException(ex);
}
}
示例5: aesGcmEncrypt
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入方法依赖的package包/类
public static byte[] aesGcmEncrypt(Transformation transformation, byte[] raw, byte[] secret, int atLength, byte[] iv, byte[] aad) throws Exception {
BlockCipher blockCipher = new AESEngine();
blockCipher.init(true, new KeyParameter(new SecretKeySpec(secret, "AES").getEncoded()));
GCMBlockCipher aGCMBlockCipher = new GCMBlockCipher(blockCipher);
aGCMBlockCipher.init(true, new AEADParameters(new KeyParameter(secret), atLength, iv, aad));
int len = aGCMBlockCipher.getOutputSize(raw.length);
byte[] out = new byte[len];
int outOff = aGCMBlockCipher.processBytes(raw, 0, raw.length, out, 0);
aGCMBlockCipher.doFinal(out, outOff);
return out;
}
示例6: runTestCase
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入方法依赖的package包/类
private void runTestCase(
GCMMultiplier encM,
GCMMultiplier decM,
String testName,
byte[] K,
byte[] IV,
byte[] A,
byte[] SA,
byte[] P,
byte[] C,
byte[] T)
throws InvalidCipherTextException
{
AEADParameters parameters = new AEADParameters(new KeyParameter(K), T.length * 8, IV, A);
GCMBlockCipher encCipher = initCipher(encM, true, parameters);
GCMBlockCipher decCipher = initCipher(decM, false, parameters);
checkTestCase(encCipher, decCipher, testName, SA, P, C, T);
checkTestCase(encCipher, decCipher, testName + " (reused)", SA, P, C, T);
// Key reuse
AEADParameters keyReuseParams = new AEADParameters(null, parameters.getMacSize(), parameters.getNonce(), parameters.getAssociatedText());
encCipher.init(true, keyReuseParams);
decCipher.init(false, keyReuseParams);
checkTestCase(encCipher, decCipher, testName + " (key reuse)", SA, P, C, T);
checkTestCase(encCipher, decCipher, testName + " (key reuse)", SA, P, C, T);
}
示例7: init
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入方法依赖的package包/类
protected void init(boolean encryption, PlainFileKey fileKey) throws IllegalArgumentException {
byte[] key = CryptoUtils.stringToByteArray(fileKey.getKey());
byte[] iv = CryptoUtils.stringToByteArray(fileKey.getIv());
AEADParameters parameters = new AEADParameters(new KeyParameter(key), 8 * TAG_SIZE, iv);
realCipher = new GCMBlockCipher(new AESFastEngine());
realCipher.init(encryption, parameters);
}
示例8: outputSizeTests
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入方法依赖的package包/类
private void outputSizeTests()
{
byte[] K = new byte[16];
byte[] A = null;
byte[] IV = new byte[16];
AEADParameters parameters = new AEADParameters(new KeyParameter(K), 16 * 8, IV, A);
GCMBlockCipher cipher = initCipher(null, true, parameters);
if (cipher.getUpdateOutputSize(0) != 0)
{
fail("incorrect getUpdateOutputSize for initial 0 bytes encryption");
}
if (cipher.getOutputSize(0) != 16)
{
fail("incorrect getOutputSize for initial 0 bytes encryption");
}
cipher.init(false, parameters);
if (cipher.getUpdateOutputSize(0) != 0)
{
fail("incorrect getUpdateOutputSize for initial 0 bytes decryption");
}
// NOTE: 0 bytes would be truncated data, but we want it to fail in the doFinal, not here
if (cipher.getOutputSize(0) != 0)
{
fail("fragile getOutputSize for initial 0 bytes decryption");
}
if (cipher.getOutputSize(16) != 0)
{
fail("incorrect getOutputSize for initial MAC-size bytes decryption");
}
}
示例9: encrypt
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入方法依赖的package包/类
public AesPacket encrypt(final SequenceItem message) {
final byte[] nonce = Ec.randomBytes(12);
final GCMBlockCipher gcm = new GCMBlockCipher(new AESFastEngine());
gcm.init(true, new AEADParameters(
new KeyParameter(key), 128, nonce, ZERO_BYTES));
final byte[] plaintext =
ConvertUtils.toBytes(message);
final byte[] ciphertext = new byte[gcm.getOutputSize(plaintext.length)];
final int resp = gcm.processBytes(plaintext, 0, plaintext.length,
ciphertext, 0);
try {
gcm.doFinal(ciphertext, resp);
} catch (final InvalidCipherTextException e) {
// Should be impossible when we're encrypting!
throw new RuntimeException(
"Unexpected behaviour in crypto libraries", e);
}
return new AesPacket(getKeyId(), nonce, ciphertext);
}
示例10: decrypt
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入方法依赖的package包/类
/**
* Returns decrypted data.
*
* @param key
* @param nonce nonce/ IV
* @param header
* @param encryptedData
* @param tag
* @param optional optional AADBytes (post header)
* @return decrypted data
* @throws IllegalArgumentException on decryption exceptions
* @throws NullPointerException on null arguments
*/
public static byte[] decrypt(
byte[] key,
byte[] nonce,
byte[] header,
byte[] encryptedData,
byte[] tag,
Optional<byte[]> optional) {
try {
GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
AEADParameters parameters = new AEADParameters(new KeyParameter(key), tag.length * 8, nonce, header);
cipher.init(false, parameters);
if (optional.isPresent()) {
byte[] aadBytes = optional.get();
cipher.processAADBytes(aadBytes, 0, aadBytes.length);
}
byte[] out = new byte[cipher.getOutputSize(encryptedData.length + tag.length)];
int pos = cipher.processBytes(encryptedData, 0, encryptedData.length, out, 0);
pos += cipher.processBytes(tag, 0, tag.length, out, pos);
pos += cipher.doFinal(out, pos);
return Arrays.copyOf(out, pos);
} catch (IllegalStateException | InvalidCipherTextException | RuntimeCryptoException ex) {
throw new IllegalStateException("GCM decrypt error", ex);
}
}
示例11: newCipherOutputStream
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入方法依赖的package包/类
public CipherOutputStream newCipherOutputStream(OutputStream os, byte[] password) throws IOException {
byte[] salt = randomBytes(saltLength);
byte[] nonce = randomBytes(nonceLength);
os.write(salt);
os.write(nonce);
byte[] dk = kdf.apply(password, salt);
GCMBlockCipher cipher = new GCMBlockCipher(new AESEngine());
AEADParameters parameters = new AEADParameters(new KeyParameter(dk), tagLength * 8, nonce);
cipher.init(true, parameters);
return new CipherOutputStream(os, cipher);
}
示例12: newCipherInputStream
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入方法依赖的package包/类
public CipherInputStream newCipherInputStream(InputStream is, byte[] password) throws IOException {
byte[] salt = IOUtils.readFully(is, saltLength);
byte[] nonce = IOUtils.readFully(is, nonceLength);
byte[] dk = kdf.apply(password, salt);
GCMBlockCipher cipher = new GCMBlockCipher(new AESEngine());
AEADParameters parameters = new AEADParameters(new KeyParameter(dk), tagLength * 8, nonce);
cipher.init(false, parameters);
return new CipherInputStream(is, cipher);
}
示例13: aesGcmDecrypt
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入方法依赖的package包/类
public static byte[] aesGcmDecrypt(Transformation transformation, byte[] encryptedData, byte[] secret, int atLength, byte[] iv, byte[] aad) throws Exception {
BlockCipher blockCipher = new AESEngine();
blockCipher.init(false, new KeyParameter(new SecretKeySpec(secret, "AES").getEncoded()));
GCMBlockCipher aGCMBlockCipher = new GCMBlockCipher(blockCipher);
aGCMBlockCipher.init(false, new AEADParameters(new KeyParameter(secret), atLength, iv, aad));
int len = aGCMBlockCipher.getOutputSize(encryptedData.length);
byte[] out = new byte[len];
int outOff = aGCMBlockCipher.processBytes(encryptedData, 0, encryptedData.length, out, 0);
aGCMBlockCipher.doFinal(out, outOff);
return out;
}
示例14: outputSizeTests
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入方法依赖的package包/类
private void outputSizeTests()
{
byte[] K = new byte[16];
byte[] A = null;
byte[] IV = new byte[16];
GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine(), new BasicGCMMultiplier());
AEADParameters parameters = new AEADParameters(new KeyParameter(K), 16 * 8, IV, A);
cipher.init(true, parameters);
if (cipher.getUpdateOutputSize(0) != 0)
{
fail("incorrect getUpdateOutputSize for initial 0 bytes encryption");
}
if (cipher.getOutputSize(0) != 16)
{
fail("incorrect getOutputSize for initial 0 bytes encryption");
}
cipher.init(false, parameters);
if (cipher.getUpdateOutputSize(0) != 0)
{
fail("incorrect getUpdateOutputSize for initial 0 bytes decryption");
}
// NOTE: 0 bytes would be truncated data, but we want it to fail in the doFinal, not here
if (cipher.getOutputSize(0) != 0)
{
fail("fragile getOutputSize for initial 0 bytes decryption");
}
if (cipher.getOutputSize(16) != 0)
{
fail("incorrect getOutputSize for initial MAC-size bytes decryption");
}
}
示例15: initCipher
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入方法依赖的package包/类
private GCMBlockCipher initCipher(GCMMultiplier m, boolean forEncryption, AEADParameters parameters)
{
GCMBlockCipher c = new GCMBlockCipher(createAESEngine(), m);
c.init(forEncryption, parameters);
return c;
}