mil.dod.th.ose.remote.integration/src/mil/dod/th/ose/remote/integration/EncryptionUtil.java
public static TerraHarvestPayload decryptECDHMessage(TerraHarvestMessage message) throws NoSuchAlgorithmException, 
        NoSuchProviderException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, 
        InvalidKeyException, InvalidAlgorithmParameterException, InvalidProtocolBufferException
    {
        //Retrieve the cipher to be used for decrypting messages.
        final Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
        
        //get the encrypted payload
        byte[] payload = message.getTerraHarvestPayload().toByteArray();
        EncryptInfo responseEncryptInfo = message.getEncryptInfo();
        
        //system ids
        int responseLocalSystemId = message.getDestId();
        int responseRemoteSystemId = message.getSourceId();
        
        //parse the initialization vector from The TerraHarvestMessage
        IvParameterSpec responseIvBytes_New = 
                new IvParameterSpec(responseEncryptInfo.getInitializationVector().toByteArray());
        
        //we will verify the signature using sender public key and the ephemeral key received with
        //the message and the ephemeral signature itself
        DSADigestSigner dsaSigner1 = new DSADigestSigner(new ECDSASigner(), new SHA256Digest()); 
        dsaSigner1.init(false, getRemotePublicKey(responseRemoteSystemId));
        byte [] ephemeralKeyTemp = responseEncryptInfo.getEphemeralKey().toByteArray();
        dsaSigner1.update(ephemeralKeyTemp, 0, ephemeralKeyTemp.length);
        
        //process signature
        boolean sigOK = dsaSigner1.verifySignature(responseEncryptInfo.getEphemeralKeySignature().toByteArray());
        assertThat(sigOK, is(true));
        
        //first get the ephemeral public key from the message and source private key
        ECPoint qPoint = EncryptionUtility.CURVEPARAMS.getCurve().decodePoint(ephemeralKeyTemp);
        ECPublicKeyParameters ephemeralPublicKeyParam = new ECPublicKeyParameters(qPoint, 
                EncryptionUtility.CURVEPARAMS);
        
        //create a shared key 
        byte [] sharedKeyBytesDecrypt = EncryptionUtility.sharedEcdhKeyCreate(getPrivateStaticKey(),
                ephemeralPublicKeyParam, responseRemoteSystemId, responseLocalSystemId);
       
        //key
        Key responsekey = new SecretKeySpec(sharedKeyBytesDecrypt, "AES");
        
        //Create a CIpher object to perform the decryption
        cipher.init(Cipher.DECRYPT_MODE, responsekey, responseIvBytes_New);
        byte[] decrypt = cipher.doFinal(payload);

        //parse the decrypted bytes
        TerraHarvestPayload payloadDecrypt = TerraHarvestPayload.parseFrom(decrypt);
        
        return payloadDecrypt;
    }
/**
     * Decrypt a message encrypted with ECDH.
     */
Decrypt a message encrypted with ECDH.
Decrypt a message encrypted with ECDH .
public static TerraHarvestPayload decryptECDHMessage(TerraHarvestMessage message) throws NoSuchAlgorithmException, 
        NoSuchProviderException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, 
        InvalidKeyException, InvalidAlgorithmParameterException, InvalidProtocolBufferException
    {
        final Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
        byte[] payload = message.getTerraHarvestPayload().toByteArray();
        EncryptInfo responseEncryptInfo = message.getEncryptInfo();
        int responseLocalSystemId = message.getDestId();
        int responseRemoteSystemId = message.getSourceId();
        IvParameterSpec responseIvBytes_New = 
                new IvParameterSpec(responseEncryptInfo.getInitializationVector().toByteArray());
        DSADigestSigner dsaSigner1 = new DSADigestSigner(new ECDSASigner(), new SHA256Digest()); 
        dsaSigner1.init(false, getRemotePublicKey(responseRemoteSystemId));
        byte [] ephemeralKeyTemp = responseEncryptInfo.getEphemeralKey().toByteArray();
        dsaSigner1.update(ephemeralKeyTemp, 0, ephemeralKeyTemp.length);
        boolean sigOK = dsaSigner1.verifySignature(responseEncryptInfo.getEphemeralKeySignature().toByteArray());
        assertThat(sigOK, is(true));
        ECPoint qPoint = EncryptionUtility.CURVEPARAMS.getCurve().decodePoint(ephemeralKeyTemp);
        ECPublicKeyParameters ephemeralPublicKeyParam = new ECPublicKeyParameters(qPoint, 
                EncryptionUtility.CURVEPARAMS);
        byte [] sharedKeyBytesDecrypt = EncryptionUtility.sharedEcdhKeyCreate(getPrivateStaticKey(),
                ephemeralPublicKeyParam, responseRemoteSystemId, responseLocalSystemId);
        Key responsekey = new SecretKeySpec(sharedKeyBytesDecrypt, "AES");
        cipher.init(Cipher.DECRYPT_MODE, responsekey, responseIvBytes_New);
        byte[] decrypt = cipher.doFinal(payload);
        TerraHarvestPayload payloadDecrypt = TerraHarvestPayload.parseFrom(decrypt);
        return payloadDecrypt;
    }
public static TerraHarvestPayload decryptECDHMessage ( TerraHarvestMessage message ) throws NoSuchAlgorithmException , NoSuchProviderException , NoSuchPaddingException , IllegalBlockSizeException , BadPaddingException , InvalidKeyException , InvalidAlgorithmParameterException , InvalidProtocolBufferException { final Cipher cipher = Cipher . getInstance ( "AES/GCM/NoPadding" , "BC" ) ; byte [ ] payload = message . getTerraHarvestPayload ( ) . toByteArray ( ) ; EncryptInfo responseEncryptInfo = message . getEncryptInfo ( ) ; int responseLocalSystemId = message . getDestId ( ) ; int responseRemoteSystemId = message . getSourceId ( ) ; IvParameterSpec responseIvBytes_New = new IvParameterSpec ( responseEncryptInfo . getInitializationVector ( ) . toByteArray ( ) ) ; DSADigestSigner dsaSigner1 = new DSADigestSigner ( new ECDSASigner ( ) , new SHA256Digest ( ) ) ; dsaSigner1 . init ( false , getRemotePublicKey ( responseRemoteSystemId ) ) ; byte [ ] ephemeralKeyTemp = responseEncryptInfo . getEphemeralKey ( ) . toByteArray ( ) ; dsaSigner1 . update ( ephemeralKeyTemp , 0 , ephemeralKeyTemp . length ) ; boolean sigOK = dsaSigner1 . verifySignature ( responseEncryptInfo . getEphemeralKeySignature ( ) . toByteArray ( ) ) ; assertThat ( sigOK , is ( true ) ) ; ECPoint qPoint = EncryptionUtility . CURVEPARAMS . getCurve ( ) . decodePoint ( ephemeralKeyTemp ) ; ECPublicKeyParameters ephemeralPublicKeyParam = new ECPublicKeyParameters ( qPoint , EncryptionUtility . CURVEPARAMS ) ; byte [ ] sharedKeyBytesDecrypt = EncryptionUtility . sharedEcdhKeyCreate ( getPrivateStaticKey ( ) , ephemeralPublicKeyParam , responseRemoteSystemId , responseLocalSystemId ) ; Key responsekey = new SecretKeySpec ( sharedKeyBytesDecrypt , "AES" ) ; cipher . init ( Cipher . DECRYPT_MODE , responsekey , responseIvBytes_New ) ; byte [ ] decrypt = cipher . doFinal ( payload ) ; TerraHarvestPayload payloadDecrypt = TerraHarvestPayload . parseFrom ( decrypt ) ; return payloadDecrypt ; }
Decrypt a message encrypted with ECDH.
Decrypt a message encrypted with ECDH .
//Retrieve the cipher to be used for decrypting messages. //get the encrypted payload //system ids //parse the initialization vector from The TerraHarvestMessage //we will verify the signature using sender public key and the ephemeral key received with //the message and the ephemeral signature itself //process signature //first get the ephemeral public key from the message and source private key //create a shared key  //key //Create a CIpher object to perform the decryption //parse the decrypted bytes