List of usage examples for org.bouncycastle.crypto.modes GCMBlockCipher GCMBlockCipher
public GCMBlockCipher(BlockCipher c, GCMMultiplier m)
From source file:COSE.EncryptCommon.java
private void AES_GCM_Decrypt(AlgorithmID alg, byte[] rgbKey) throws CoseException, InvalidCipherTextException { GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine(), new BasicGCMMultiplier()); CBORObject cn = FindAttribute(HeaderKeys.IV); if (cn == null) throw new CoseException("Missing IV during decryption"); if (cn.getType() != CBORType.ByteString) throw new CoseException("IV is incorrectly formed"); if (cn.GetByteString().length != 96 / 8) throw new CoseException("IV size is incorrect"); if (rgbKey.length != alg.getKeySize() / 8) throw new CoseException("Missing IV during decryption"); KeyParameter contentKey = new KeyParameter(rgbKey); AEADParameters parameters = new AEADParameters(contentKey, 128, cn.GetByteString(), getAADBytes()); cipher.init(false, parameters);/*from w w w .j a va2s . co m*/ byte[] C = new byte[cipher.getOutputSize(rgbEncrypt.length)]; int length = cipher.processBytes(rgbEncrypt, 0, rgbEncrypt.length, C, 0); length += cipher.doFinal(C, length); rgbContent = C; }
From source file:COSE.EncryptCommon.java
private void AES_GCM_Encrypt(AlgorithmID alg, byte[] rgbKey) throws CoseException, IllegalStateException, InvalidCipherTextException { GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine(), new BasicGCMMultiplier()); if (rgbKey.length != alg.getKeySize() / 8) throw new CoseException("Key Size is incorrect"); KeyParameter contentKey = new KeyParameter(rgbKey); CBORObject cn = FindAttribute(HeaderKeys.IV); byte[] IV;//from ww w. j av a2 s .c om if (cn == null) { IV = new byte[96 / 8]; random.nextBytes(IV); AddUnprotected(HeaderKeys.IV, CBORObject.FromObject(IV)); } else { if (cn.getType() != CBORType.ByteString) throw new CoseException("IV is incorrectly formed"); if (cn.GetByteString().length != 96 / 8) throw new CoseException("IV size is incorrect"); IV = cn.GetByteString(); } AEADParameters parameters = new AEADParameters(contentKey, 128, IV, getAADBytes()); cipher.init(true, parameters); byte[] C = new byte[cipher.getOutputSize(rgbContent.length)]; int length = cipher.processBytes(rgbContent, 0, rgbContent.length, C, 0); length += cipher.doFinal(C, length); rgbEncrypt = C; }
From source file:org.openmuc.jdlms.internal.security.CipheringGcm.java
License:Open Source License
public static byte[] encrypt(byte[] plaintext, int off, int len, byte[] systemTitle, int frameCounter, byte[] encryptionKey, byte[] authenticationKey, byte tag) throws IOException { byte[] frameCounterBytes = intToByteArray(frameCounter); byte[] iv = concat(systemTitle, frameCounterBytes); byte[] associatedData = new byte[authenticationKey.length + 1]; associatedData[0] = SECURITY_CONTROL_BYTES_AUTH_CIPH; System.arraycopy(authenticationKey, 0, associatedData, 1, authenticationKey.length); AEADParameters parameters = new AEADParameters(new KeyParameter(encryptionKey), 96, iv, associatedData); GCMBlockCipher encCipher = new GCMBlockCipher(new AESFastEngine(), new BasicGCMMultiplier()); encCipher.init(true, parameters);/* w w w . j a v a2s .co m*/ byte[] enc = new byte[encCipher.getOutputSize(len)]; int length = encCipher.processBytes(plaintext, off, len, enc, 0); try { length += encCipher.doFinal(enc, length); } catch (IllegalStateException e) { throw new IOException("Unable to cipher/encrypt xDLMS pdu", e); } catch (InvalidCipherTextException e) { throw new IOException("Unable to cipher/encrypt xDLMS pdu", e); } byte[] result = new byte[enc.length + 7]; result[0] = tag; result[1] = (byte) (enc.length + 5); result[2] = SECURITY_CONTROL_BYTES_AUTH_CIPH; System.arraycopy(frameCounterBytes, 0, result, 3, 4); System.arraycopy(enc, 0, result, 7, enc.length); return result; }
From source file:org.openmuc.jdlms.internal.security.CipheringGcm.java
License:Open Source License
public static byte[] decrypt(byte[] ciphertext, byte[] systemTitle, byte[] encryptionKey, byte[] authenticationKey) throws IOException { byte[] iv = new byte[12]; System.arraycopy(systemTitle, 0, iv, 0, systemTitle.length); // copy frame counter System.arraycopy(ciphertext, 1, iv, 8, 4); byte[] associatedData = new byte[authenticationKey.length + 1]; associatedData[0] = SECURITY_CONTROL_BYTES_AUTH_CIPH; System.arraycopy(authenticationKey, 0, associatedData, 1, authenticationKey.length); AEADParameters parameters = new AEADParameters(new KeyParameter(encryptionKey), 96, iv, associatedData); GCMBlockCipher decCipher = new GCMBlockCipher(new AESFastEngine(), new BasicGCMMultiplier()); decCipher.init(false, parameters);// www . j a va 2 s . com byte[] dec = new byte[decCipher.getOutputSize(ciphertext.length - 5)]; int len = decCipher.processBytes(ciphertext, 5, ciphertext.length - 5, dec, 0); try { len += decCipher.doFinal(dec, len); } catch (IllegalStateException e) { throw new IOException("Unable to decipher/decrypt xDLMS pdu", e); } catch (InvalidCipherTextException e) { throw new IOException("Unable to decipher/decrypt xDLMS pdu", e); } return dec; }