List of usage examples for org.bouncycastle.crypto.engines AESEngine AESEngine
public AESEngine()
From source file:org.syncany.tests.crypto.AesGcmWithBcInputStreamTest.java
License:Open Source License
@Test public void testE_BouncyCastleCipherInputStreamWithAesGcmLongPlaintext() throws InvalidKeyException, InvalidAlgorithmParameterException, IOException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException { // Encrypt (not interesting in this example) byte[] randomKey = createRandomArray(16); byte[] randomIv = createRandomArray(16); byte[] originalPlaintext = createRandomArray(4080); // <<<< 4080 bytes fails, 4079 bytes works! byte[] originalCiphertext = encryptWithAesGcm(originalPlaintext, randomKey, randomIv); // Decrypt with BouncyCastle implementation of CipherInputStream AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine()); cipher.init(false, new AEADParameters(new KeyParameter(randomKey), 128, randomIv)); try {//from w w w. ja v a 2s . co m readFromStream(new org.bouncycastle.crypto.io.CipherInputStream( new ByteArrayInputStream(originalCiphertext), cipher)); // ^^^^^^^^^^^^^^^ INTERESTING PART ^^^^^^^^^^^^^^^^ // // In this example, the BouncyCastle implementation of the CipherInputStream throws an ArrayIndexOutOfBoundsException. // The only difference to the example above is that the plaintext is now 4080 bytes long! For 4079 bytes plaintexts, // everything works just fine. System.out.println( "Test E: org.bouncycastle.crypto.io.CipherInputStream: OK, throws no exception"); } catch (IOException e) { fail("Test E: org.bouncycastle.crypto.io.CipherInputStream: NOT OK throws: " + e.getMessage()); } }
From source file:org.tramaci.onionmail.Stdio.java
License:Open Source License
public static byte[] AESEnc2m(byte[][] key, byte[][] iv, byte[] data) throws Exception { byte[][] blo = Stdio.DivBlock(data, 16, false); int cx = blo.length; int kc = key.length; for (int kx = 0; kx < kc; kx++) { CBCBlockCipher aes = new CBCBlockCipher(new AESEngine()); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key[kx]), iv[kx]); aes.init(true, ivAndKey);/*from w ww. j a v a 2 s .com*/ for (int ax = 0; ax < cx; ax++) aes.processBlock(blo[ax], 0, blo[ax], 0); } data = Stdio.MulBlock(blo, 16); blo = null; return data; }
From source file:org.tramaci.onionmail.Stdio.java
License:Open Source License
public static byte[] AESDec2m(byte[][] key, byte[][] iv, byte[] data) throws Exception { byte[][] blo = Stdio.DivBlock(data, 16, false); int cx = blo.length; int kc = key.length - 1; for (int kx = kc; kx > -1; kx--) { CBCBlockCipher aes = new CBCBlockCipher(new AESEngine()); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key[kx]), iv[kx]); aes.init(false, ivAndKey);//ww w .ja va2 s . c om for (int ax = 0; ax < cx; ax++) aes.processBlock(blo[ax], 0, blo[ax], 0); } data = Stdio.MulBlock(blo, 16); blo = null; return data; }
From source file:org.tramaci.onionmail.Stdio.java
License:Open Source License
public static byte[] AESEnc2(byte[] key, byte[] iv, byte[] data) throws Exception { byte[][] blo = Stdio.DivBlock(data, 16, false); CBCBlockCipher aes = new CBCBlockCipher(new AESEngine()); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv); aes.init(true, ivAndKey);//from ww w.j ava 2 s. com int cx = blo.length; for (int ax = 0; ax < cx; ax++) aes.processBlock(blo[ax], 0, blo[ax], 0); data = Stdio.MulBlock(blo, 16); blo = null; return data; }
From source file:org.tramaci.onionmail.Stdio.java
License:Open Source License
public static byte[] AESDec2(byte[] key, byte[] iv, byte[] data) throws Exception { byte[][] blo = Stdio.DivBlock(data, 16, false); CBCBlockCipher aes = new CBCBlockCipher(new AESEngine()); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv); aes.init(false, ivAndKey);//from w ww . ja v a 2 s . c o m int cx = blo.length; for (int ax = 0; ax < cx; ax++) aes.processBlock(blo[ax], 0, blo[ax], 0); data = Stdio.MulBlock(blo, 16); blo = null; return data; }
From source file:org.tramaci.onionmail.Stdio.java
License:Open Source License
public static byte[] AES2Enc(byte[] key, byte[] iv, byte[] data) throws Exception { PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv); aes.init(true, ivAndKey);/*from w ww . j av a 2 s.com*/ return AES2cipher(aes, data); }
From source file:org.tramaci.onionmail.Stdio.java
License:Open Source License
public static byte[] AES2Dec(byte[] key, byte[] iv, byte[] data) throws Exception { try {//from w ww . j av a 2 s . co m PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv); aes.init(false, ivAndKey); return AES2cipher(aes, data); } catch (Exception E) { throw new Exception("!Invalid KEY for data" + E.getMessage()); } }
From source file:org.votingsystem.signature.util.Encryptor.java
License:Open Source License
public static String encryptAES(String messageToEncrypt, AESParams aesParams) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException, InvalidCipherTextException { PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); KeyParameter keyParam = new KeyParameter(aesParams.getKey().getEncoded()); ParametersWithIV params = new ParametersWithIV(keyParam, aesParams.getIV().getIV()); pbbc.init(true, params); //to decrypt put param to false byte[] input = messageToEncrypt.getBytes("UTF-8"); byte[] output = new byte[pbbc.getOutputSize(input.length)]; int bytesWrittenOut = pbbc.processBytes(input, 0, input.length, output, 0); pbbc.doFinal(output, bytesWrittenOut); return new String(org.bouncycastle.util.encoders.Base64.encode(output)); }
From source file:org.votingsystem.signature.util.Encryptor.java
License:Open Source License
public static String decryptAES(String messageToDecrypt, AESParams aesParams) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException, InvalidCipherTextException { PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); KeyParameter keyParam = new KeyParameter(aesParams.getKey().getEncoded()); CipherParameters params = new ParametersWithIV(keyParam, aesParams.getIV().getIV()); pbbc.init(false, params); //to encrypt put param to true byte[] input = org.bouncycastle.util.encoders.Base64.decode(messageToDecrypt.getBytes("UTF-8")); byte[] output = new byte[pbbc.getOutputSize(input.length)]; int bytesWrittenOut = pbbc.processBytes(input, 0, input.length, output, 0); pbbc.doFinal(output, bytesWrittenOut); int i = output.length - 1; //remove padding while (i >= 0 && output[i] == 0) { --i;/* w w w . j av a2s . c o m*/ } return new String(Arrays.copyOf(output, i + 1), "UTF-8"); }
From source file:org.xdi.oxauth.model.jwe.JweDecrypterImpl.java
License:MIT License
@Override public String decryptCipherText(String encodedCipherText, byte[] contentMasterKey, byte[] initializationVector, byte[] authenticationTag, byte[] additionalAuthenticatedData) throws InvalidJweException { if (getBlockEncryptionAlgorithm() == null) { throw new InvalidJweException("The block encryption algorithm is null"); }/*from ww w. j a v a 2 s .c o m*/ if (contentMasterKey == null) { throw new InvalidJweException("The content master key (CMK) is null"); } if (initializationVector == null) { throw new InvalidJweException("The initialization vector is null"); } if (authenticationTag == null) { throw new InvalidJweException("The authentication tag is null"); } if (additionalAuthenticatedData == null) { throw new InvalidJweException("The additional authentication data is null"); } try { if (getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A128GCM || getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A256GCM) { final int MAC_SIZE_BITS = 128; byte[] cipherText = Base64Util.base64urldecode(encodedCipherText); KeyParameter key = new KeyParameter(contentMasterKey); AEADParameters aeadParameters = new AEADParameters(key, MAC_SIZE_BITS, initializationVector, additionalAuthenticatedData); SecretKeySpec sks = new SecretKeySpec(contentMasterKey, "AES"); BlockCipher blockCipher = new AESEngine(); CipherParameters params = new KeyParameter(sks.getEncoded()); blockCipher.init(false, params); GCMBlockCipher aGCMBlockCipher = new GCMBlockCipher(blockCipher); aGCMBlockCipher.init(false, aeadParameters); byte[] input = new byte[cipherText.length + authenticationTag.length]; System.arraycopy(cipherText, 0, input, 0, cipherText.length); System.arraycopy(authenticationTag, 0, input, cipherText.length, authenticationTag.length); int len = aGCMBlockCipher.getOutputSize(input.length); byte[] out = new byte[len]; int outOff = aGCMBlockCipher.processBytes(input, 0, input.length, out, 0); aGCMBlockCipher.doFinal(out, outOff); String plaintext = new String(out, Charset.forName(Util.UTF8_STRING_ENCODING)); return plaintext; } else if (getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A128CBC_PLUS_HS256 || getBlockEncryptionAlgorithm() == BlockEncryptionAlgorithm.A256CBC_PLUS_HS512) { byte[] cipherText = Base64Util.base64urldecode(encodedCipherText); byte[] cek = KeyDerivationFunction.generateCek(contentMasterKey, getBlockEncryptionAlgorithm()); Cipher cipher = Cipher.getInstance(getBlockEncryptionAlgorithm().getAlgorithm()); IvParameterSpec ivParameter = new IvParameterSpec(initializationVector); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(cek, "AES"), ivParameter); byte[] decodedPlainTextBytes = cipher.doFinal(cipherText); String decodedPlainText = new String(decodedPlainTextBytes, Charset.forName(Util.UTF8_STRING_ENCODING)); // Integrity check String securedInputValue = new String(additionalAuthenticatedData, Charset.forName(Util.UTF8_STRING_ENCODING)) + "." + encodedCipherText; byte[] cik = KeyDerivationFunction.generateCik(contentMasterKey, getBlockEncryptionAlgorithm()); SecretKey secretKey = new SecretKeySpec(cik, getBlockEncryptionAlgorithm().getIntegrityValueAlgorithm()); Mac mac = Mac.getInstance(getBlockEncryptionAlgorithm().getIntegrityValueAlgorithm()); mac.init(secretKey); byte[] integrityValue = mac.doFinal(securedInputValue.getBytes(Util.UTF8_STRING_ENCODING)); if (!Arrays.equals(integrityValue, authenticationTag)) { throw new InvalidJweException("The authentication tag is not valid"); } return decodedPlainText; } else { throw new InvalidJweException("The block encryption algorithm is not supported"); } } catch (InvalidCipherTextException e) { throw new InvalidJweException(e); } catch (NoSuchPaddingException e) { throw new InvalidJweException(e); } catch (BadPaddingException e) { throw new InvalidJweException(e); } catch (InvalidAlgorithmParameterException e) { throw new InvalidJweException(e); } catch (NoSuchAlgorithmException e) { throw new InvalidJweException(e); } catch (IllegalBlockSizeException e) { throw new InvalidJweException(e); } catch (UnsupportedEncodingException e) { throw new InvalidJweException(e); } catch (NoSuchProviderException e) { throw new InvalidJweException(e); } catch (InvalidKeyException e) { throw new InvalidJweException(e); } catch (InvalidParameterException e) { throw new InvalidJweException(e); } }