List of usage examples for org.bouncycastle.crypto.modes GCMBlockCipher GCMBlockCipher
public GCMBlockCipher(BlockCipher c)
From source file:com.nimbusds.jose.crypto.impl.LegacyAESGCM.java
License:Apache License
/** * Creates a new AES/GCM/NoPadding cipher. * * @param secretKey The AES key. Must not be {@code null}. * @param forEncryption If {@code true} creates an encryption cipher, * else creates a decryption cipher. * @param iv The initialisation vector (IV). Must not be * {@code null}./*from w w w . j a va2 s .c o m*/ * @param authData The authenticated data. Must not be * {@code null}. * * @return The AES/GCM/NoPadding cipher. */ private static GCMBlockCipher createAESGCMCipher(final SecretKey secretKey, final boolean forEncryption, final byte[] iv, final byte[] authData) { // Initialise AES cipher BlockCipher cipher = createAESCipher(secretKey, forEncryption); // Create GCM cipher with AES GCMBlockCipher gcm = new GCMBlockCipher(cipher); AEADParameters aeadParams = new AEADParameters(new KeyParameter(secretKey.getEncoded()), AUTH_TAG_BIT_LENGTH, iv, authData); gcm.init(forEncryption, aeadParams); return gcm; }
From source file:com.skplanet.jose.jwa.crypto.CryptoUtils.java
License:Open Source License
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;//ww w . ja v a 2 s. c o m }
From source file:com.skplanet.jose.jwa.crypto.CryptoUtils.java
License:Open Source License
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;//www. j a v a 2 s . c o m }
From source file:dbn.crypto.Crypto.java
public static byte[] aesGCMEncrypt(byte[] plaintext, byte[] key) { try {/*from w w w . j a va 2 s . c o m*/ byte[] iv = new byte[16]; secureRandom.get().nextBytes(iv); GCMBlockCipher aes = new GCMBlockCipher(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); } }
From source file:dbn.crypto.Crypto.java
public static byte[] aesGCMDecrypt(byte[] ivCiphertext, byte[] key) { try {/* ww w . j av a 2s .c o m*/ if (ivCiphertext.length < 16) { throw new InvalidCipherTextException("invalid ivCiphertext length"); } byte[] iv = Arrays.copyOfRange(ivCiphertext, 0, 16); byte[] ciphertext = Arrays.copyOfRange(ivCiphertext, 16, ivCiphertext.length); GCMBlockCipher aes = new GCMBlockCipher(new AESEngine()); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv); aes.init(false, ivAndKey); byte[] output = new byte[aes.getOutputSize(ciphertext.length)]; int plaintextLength = aes.processBytes(ciphertext, 0, ciphertext.length, output, 0); plaintextLength += aes.doFinal(output, plaintextLength); byte[] result = new byte[plaintextLength]; System.arraycopy(output, 0, result, 0, result.length); return result; } catch (InvalidCipherTextException e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:dorkbox.util.crypto.AesTest.java
License:Apache License
@Test public void AesGcm() throws IOException { byte[] bytes = "hello, my name is inigo montoya".getBytes(); SecureRandom rand = new SecureRandom(entropySeed.getBytes()); GCMBlockCipher aesEngine = new GCMBlockCipher(new AESFastEngine()); byte[] key = new byte[32]; byte[] iv = new byte[16]; // note: the IV needs to be VERY unique! rand.nextBytes(key); // 256bit key (32 bytes) rand.nextBytes(iv); // 128bit block size (16 bytes) byte[] encryptAES = CryptoAES.encrypt(aesEngine, key, iv, bytes, logger); byte[] decryptAES = CryptoAES.decrypt(aesEngine, key, iv, encryptAES, logger); if (Arrays.equals(bytes, encryptAES)) { fail("bytes should not be equal"); }/* ww w .j ava 2s . c om*/ if (!Arrays.equals(bytes, decryptAES)) { fail("bytes not equal"); } }
From source file:dorkbox.util.crypto.AesTest.java
License:Apache License
@Test public void AesGcmStream() throws IOException { byte[] originalBytes = "hello, my name is inigo montoya".getBytes(); ByteArrayInputStream inputStream = new ByteArrayInputStream(originalBytes); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); SecureRandom rand = new SecureRandom(entropySeed.getBytes()); GCMBlockCipher aesEngine = new GCMBlockCipher(new AESFastEngine()); byte[] key = new byte[32]; byte[] iv = new byte[16]; // note: the IV needs to be VERY unique! rand.nextBytes(key); // 256bit key rand.nextBytes(iv); // 128bit block size boolean success = CryptoAES.encryptStream(aesEngine, key, iv, inputStream, outputStream, logger); if (!success) { fail("crypto was not successful"); }/*from w w w . ja v a2 s. c om*/ byte[] encryptBytes = outputStream.toByteArray(); inputStream = new ByteArrayInputStream(outputStream.toByteArray()); outputStream = new ByteArrayOutputStream(); success = CryptoAES.decryptStream(aesEngine, key, iv, inputStream, outputStream, logger); if (!success) { fail("crypto was not successful"); } byte[] decryptBytes = outputStream.toByteArray(); if (Arrays.equals(originalBytes, encryptBytes)) { fail("bytes should not be equal"); } if (!Arrays.equals(originalBytes, decryptBytes)) { fail("bytes not equal"); } }
From source file:dorkbox.util.crypto.AesTest.java
License:Apache License
@Test public void AesWithIVGcm() throws IOException { byte[] bytes = "hello, my name is inigo montoya".getBytes(); SecureRandom rand = new SecureRandom(entropySeed.getBytes()); GCMBlockCipher aesEngine = new GCMBlockCipher(new AESFastEngine()); byte[] key = new byte[32]; // 256bit key byte[] iv = new byte[aesEngine.getUnderlyingCipher().getBlockSize()]; // note: the IV needs to be VERY unique! rand.nextBytes(key);/* w ww.j a va 2 s. c om*/ rand.nextBytes(iv); byte[] encryptAES = CryptoAES.encryptWithIV(aesEngine, key, iv, bytes, logger); byte[] decryptAES = CryptoAES.decryptWithIV(aesEngine, key, encryptAES, logger); if (Arrays.equals(bytes, encryptAES)) { fail("bytes should not be equal"); } if (!Arrays.equals(bytes, decryptAES)) { fail("bytes not equal"); } }
From source file:dorkbox.util.crypto.AesTest.java
License:Apache License
@Test public void AesWithIVGcmStream() throws IOException { byte[] originalBytes = "hello, my name is inigo montoya".getBytes(); ByteArrayInputStream inputStream = new ByteArrayInputStream(originalBytes); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); SecureRandom rand = new SecureRandom(entropySeed.getBytes()); GCMBlockCipher aesEngine = new GCMBlockCipher(new AESFastEngine()); byte[] key = new byte[32]; byte[] iv = new byte[16]; // note: the IV needs to be VERY unique! rand.nextBytes(key); // 256bit key rand.nextBytes(iv); // 128bit block size boolean success = CryptoAES.encryptStreamWithIV(aesEngine, key, iv, inputStream, outputStream, logger); if (!success) { fail("crypto was not successful"); }/*w ww . j a v a 2 s . c om*/ byte[] encryptBytes = outputStream.toByteArray(); inputStream = new ByteArrayInputStream(outputStream.toByteArray()); outputStream = new ByteArrayOutputStream(); success = CryptoAES.decryptStreamWithIV(aesEngine, key, inputStream, outputStream, logger); if (!success) { fail("crypto was not successful"); } byte[] decryptBytes = outputStream.toByteArray(); if (Arrays.equals(originalBytes, encryptBytes)) { fail("bytes should not be equal"); } if (!Arrays.equals(originalBytes, decryptBytes)) { fail("bytes not equal"); } }
From source file:dorkbox.util.crypto.PerformanceTest.java
License:Apache License
PerformanceTest(boolean isWarmup) { final byte[] bytes = new byte[64 * 1024]; byte[] encrypted = null; final byte[] aesKey = new byte[32]; final byte[] aesIV = new byte[12]; final Random random = new SecureRandom(entropySeed.getBytes()); random.nextBytes(bytes);//www . j a v a 2s .co m random.nextBytes(aesKey); random.nextBytes(aesIV); int length = bytes.length; if (!isWarmup) { System.out.println("Benchmarking AES-256 GCM encryption"); } long javaEncryptInputBytes = 0; long javaEncryptStartTime = System.currentTimeMillis(); // convert to bouncycastle GCMBlockCipher aesEngine = new GCMBlockCipher(new AESFastEngine()); CipherParameters aesIVAndKey = new ParametersWithIV(new KeyParameter(aesKey), aesIV); long encryptInitTime = 0L; long encryptUpdate1Time = 0L; long encryptDoFinalTime = 0L; while (System.currentTimeMillis() - javaEncryptStartTime < 10000) { random.nextBytes(aesIV); long n1 = System.nanoTime(); aesEngine.reset(); aesEngine.init(true, aesIVAndKey); if (encrypted == null) { int minSize = aesEngine.getOutputSize(length); encrypted = new byte[minSize]; } long n2 = System.nanoTime(); int actualLength = aesEngine.processBytes(bytes, 0, length, encrypted, 0); long n3 = System.nanoTime(); try { actualLength += aesEngine.doFinal(encrypted, actualLength); } catch (Exception e) { logger.error("Unable to perform AES cipher.", e); } if (encrypted.length != actualLength) { byte[] result = new byte[actualLength]; System.arraycopy(encrypted, 0, result, 0, result.length); encrypted = result; } long n4 = System.nanoTime(); javaEncryptInputBytes += actualLength; encryptInitTime = n2 - n1; encryptUpdate1Time = n3 - n2; encryptDoFinalTime = n4 - n3; } long javaEncryptEndTime = System.currentTimeMillis(); if (!isWarmup) { System.out.println("Time init (ns): " + encryptInitTime); System.out.println("Time update (ns): " + encryptUpdate1Time); System.out.println("Time do final (ns): " + encryptDoFinalTime); System.out.println("Java calculated at " + (javaEncryptInputBytes / 1024 / 1024 / ((javaEncryptEndTime - javaEncryptStartTime) / 1000)) + " MB/s"); System.out.println("Benchmarking AES-256 GCM decryption"); } long javaDecryptInputBytes = 0; long javaDecryptStartTime = System.currentTimeMillis(); length = encrypted.length; long decryptInitTime = 0L; long decryptUpdate1Time = 0L; long decryptDoFinalTime = 0L; while (System.currentTimeMillis() - javaDecryptStartTime < 10000) { long n1 = System.nanoTime(); aesEngine.reset(); aesEngine.init(false, aesIVAndKey); long n2 = System.nanoTime(); int actualLength = aesEngine.processBytes(encrypted, 0, length, bytes, 0); long n3 = System.nanoTime(); try { actualLength += aesEngine.doFinal(bytes, actualLength); } catch (Exception e) { logger.debug("Unable to perform AES cipher.", e); } long n4 = System.nanoTime(); javaDecryptInputBytes += actualLength; decryptInitTime += n2 - n1; decryptUpdate1Time += n3 - n2; decryptDoFinalTime += n4 - n3; } long javaDecryptEndTime = System.currentTimeMillis(); if (!isWarmup) { System.out.println("Time init (ns): " + decryptInitTime); System.out.println("Time update 1 (ns): " + decryptUpdate1Time); System.out.println("Time do final (ns): " + decryptDoFinalTime); System.out.println("Total bytes processed: " + javaDecryptInputBytes); System.out.println("Java calculated at " + (javaDecryptInputBytes / 1024 / 1024 / ((javaDecryptEndTime - javaDecryptStartTime) / 1000)) + " MB/s"); } }