本文整理汇总了Java中org.bouncycastle.crypto.modes.GCMBlockCipher类的典型用法代码示例。如果您正苦于以下问题:Java GCMBlockCipher类的具体用法?Java GCMBlockCipher怎么用?Java GCMBlockCipher使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
GCMBlockCipher类属于org.bouncycastle.crypto.modes包,在下文中一共展示了GCMBlockCipher类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: CipherWriteStreamValidation
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入依赖的package包/类
public CipherWriteStreamValidation(byte[] secretBytes, byte[] salt) {
this.salt = salt.clone();
secretBytes = secretBytes.clone();
if (secretBytes.length != KEY_SIZE_BYTES) {
secretBytes = Hashing.sha256().hashBytes(secretBytes).asBytes();
}
try {
KeyParameter key = new KeyParameter(secretBytes);
AEADParameters params = new AEADParameters(key, MAC_SIZE_BITS, this.salt);
this.encryptor = new GCMBlockCipher(new AESFastEngine());
this.encryptor.init(true, params);
this.decryptor = new GCMBlockCipher(new AESFastEngine());
this.decryptor.init(false, params);
} catch (Exception e) {
throw new RuntimeException("could not create cipher for AES256", e);
} finally {
Arrays.fill(secretBytes, (byte) 0);
}
}
示例2: performTest
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入依赖的package包/类
public void performTest()
{
for (int i = 0; i < TEST_VECTORS.length; i++)
{
TestCase testCase = TEST_VECTORS[i];
Mac mac = new GMac(new GCMBlockCipher(new AESFastEngine()), testCase.getTag().length * 8);
CipherParameters key = new KeyParameter(testCase.getKey());
mac.init(new ParametersWithIV(key, testCase.getIv()));
testSingleByte(mac, testCase);
testMultibyte(mac, testCase);
}
// Invalid mac size
testInvalidMacSize(97);
testInvalidMacSize(136);
testInvalidMacSize(24);
}
示例3: testInvalidMacSize
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入依赖的package包/类
private void testInvalidMacSize(int size)
{
try
{
GMac mac = new GMac(new GCMBlockCipher(new AESFastEngine()), size);
mac.init(new ParametersWithIV(null, new byte[16]));
fail("Expected failure for illegal mac size " + size);
}
catch (IllegalArgumentException e)
{
if (!e.getMessage().startsWith("Invalid value for MAC size"))
{
fail("Illegal mac size failed with unexpected message");
}
}
}
示例4: 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);
}
示例5: SAES256v01
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入依赖的package包/类
public SAES256v01(byte[] secretBytes, byte[] salt) {
this.salt = salt.clone();
secretBytes = secretBytes.clone();
if (secretBytes.length != KEY_SIZE_BYTES) {
secretBytes = Hashing.sha256().hashBytes(secretBytes).asBytes();
}
try {
KeyParameter key = new KeyParameter(secretBytes);
AEADParameters params = new AEADParameters(key, MAC_SIZE_BITS, this.salt);
this.encryptor = new GCMBlockCipher(new AESFastEngine());
this.encryptor.init(true, params);
this.decryptor = new GCMBlockCipher(new AESFastEngine());
this.decryptor.init(false, params);
} catch (Exception e) {
throw new RuntimeException("could not create cipher for AES256", e);
} finally {
Arrays.fill(secretBytes, (byte) 0);
}
}
示例6: 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);
}
}
示例7: 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);
}
示例8: 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);
}
}
示例9: 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;
}
示例10: performTest
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入依赖的package包/类
public void performTest()
{
for (int i = 0; i < TEST_VECTORS.length; i++)
{
TestCase testCase = TEST_VECTORS[i];
Mac mac = new GMac(new GCMBlockCipher(new AESFastEngine()), testCase.getTag().length * 8);
CipherParameters key = new KeyParameter(testCase.getKey());
mac.init(new ParametersWithIV(key, testCase.getIv()));
testSingleByte(mac, testCase);
testMultibyte(mac, testCase);
}
// Invalid mac size
testInvalidMacSize(97);
testInvalidMacSize(136);
testInvalidMacSize(88);
testInvalidMacSize(64);
}
示例11: 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);
}
示例12: 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);
}
示例13: testModes
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入依赖的package包/类
private void testModes(BlockCipher cipher1, BlockCipher cipher2, int keySize)
throws Exception
{
final KeyParameter key = new KeyParameter(new byte[keySize]);
final int blockSize = getBlockSize(cipher1);
final CipherParameters withIv = new ParametersWithIV(key, new byte[blockSize]);
if (blockSize > 1)
{
testMode(new PaddedBufferedBlockCipher(cipher1, new PKCS7Padding()), key);
testMode(new PaddedBufferedBlockCipher(new CBCBlockCipher(cipher1), new PKCS7Padding()), withIv);
testMode(new BufferedBlockCipher(new OFBBlockCipher(cipher1, blockSize)), withIv);
testMode(new BufferedBlockCipher(new CFBBlockCipher(cipher1, blockSize)), withIv);
testMode(new BufferedBlockCipher(new SICBlockCipher(cipher1)), withIv);
}
// CTS requires at least one block
if (blockSize <= 16 && streamSize >= blockSize)
{
testMode(new CTSBlockCipher(cipher1), key);
}
if (blockSize <= 16 && streamSize >= blockSize)
{
testMode(new NISTCTSBlockCipher(NISTCTSBlockCipher.CS1, cipher1), key);
testMode(new NISTCTSBlockCipher(NISTCTSBlockCipher.CS2, cipher1), key);
testMode(new NISTCTSBlockCipher(NISTCTSBlockCipher.CS3, cipher1), key);
}
if (blockSize == 8 || blockSize == 16)
{
testMode(new EAXBlockCipher(cipher1), withIv);
}
if (blockSize == 16)
{
testMode(new CCMBlockCipher(cipher1), new ParametersWithIV(key, new byte[7]));
testMode(new GCMBlockCipher(cipher1), withIv);
testMode(new OCBBlockCipher(cipher1, cipher2), new ParametersWithIV(key, new byte[15]));
}
}
示例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];
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");
}
}
示例15: getMac
import org.bouncycastle.crypto.modes.GCMBlockCipher; //导入依赖的package包/类
@Override
protected Mac getMac(String mode) {
if (!currentMode.equals(mode) || mac == null) {
BlockCipher engine = BlockCiphers.getBlockCipherEngine(mode);
mac = new org.bouncycastle.crypto.macs.GMac(new GCMBlockCipher(engine));
currentMode = mode;
}
return mac;
}