mil.dod.th.ose.remote.integration/src/mil/dod/th/ose/remote/integration/EncryptionUtil.java
public static TerraHarvestMessage encryptMessageWithECDH(TerraHarvestMessage thMessage) 
        throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, 
        NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException
    {
        //Retrieve the cipher to be used for encrypting messages.
        final Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
        
        //need to set the encryption information so put the message to a builder
        TerraHarvestMessage.Builder builder = thMessage.toBuilder();
        builder.setEncryptType(EncryptType.AES_ECDH_ECDSA);
        
        //destination and source ids
        int remoteSystemId = TerraHarvestMessageHelper.getControllerId();
        int localSystemId = TerraHarvestMessageHelper.getSourceId();
        
        //asymmetrical key generator
        AsymmetricCipherKeyPair ephemeralKeyPair = EncryptionUtility.createKeyPair();
        byte[] ephemeralPublicKey = ((ECPublicKeyParameters)ephemeralKeyPair.getPublic()).getQ().getEncoded(false);
        
        //digital signer will 'digest' or take parts of the ephemeral key and this systems
        //private key and create a signature that will be used when the message is received to verify the sender
        DSADigestSigner dsaSigner = new DSADigestSigner(new ECDSASigner(), new SHA256Digest()); 
        dsaSigner.init(true, getPrivateStaticKey());
        dsaSigner.update(ephemeralPublicKey, 0, ephemeralPublicKey.length);
        byte[] ephemeralSignature = dsaSigner.generateSignature();
        
        //create the shared key
        byte [] sharedKeyBytesEncrypt = EncryptionUtility.sharedEcdhKeyCreate(
                    (ECPrivateKeyParameters)ephemeralKeyPair.getPrivate(), getRemotePublicKey(remoteSystemId), 
                    localSystemId, remoteSystemId);
        Key key = new SecretKeySpec(sharedKeyBytesEncrypt, "AES");

        //initialization vector creation, these are random bytes, 
        byte[] ivBytes = new byte[12];
        EncryptionUtility.RANDOM_BC.nextBytes(ivBytes);
        IvParameterSpec ivBytes_New = new IvParameterSpec(ivBytes);
        
        //initialize the cipher, and encrypt the message
        cipher.init(Cipher.ENCRYPT_MODE, key, ivBytes_New);
        byte[] encrypt = cipher.doFinal(thMessage.getTerraHarvestPayload().toByteArray());
        
        //build EncryptInfo
        EncryptInfo encryptInfo = EncryptInfo.newBuilder().
                setEphemeralKey(ByteString.copyFrom(ephemeralPublicKey)).
                setEphemeralKeySignature(ByteString.copyFrom(ephemeralSignature)).
                setInitializationVector(ByteString.copyFrom(ivBytes)).
                build();
  
        //build the message containing encrypted payload information
        TerraHarvestMessage encryptedMessage = builder.setTerraHarvestPayload(ByteString.copyFrom(encrypt)).
                setEncryptInfo(encryptInfo).build();
        
        return encryptedMessage;
    }
/**
     * Encrypt a message with ECDH.
     */
Encrypt a message with ECDH.
Encrypt a message with ECDH .
public static TerraHarvestMessage encryptMessageWithECDH(TerraHarvestMessage thMessage) 
        throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, 
        NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException
    {
        final Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
        TerraHarvestMessage.Builder builder = thMessage.toBuilder();
        builder.setEncryptType(EncryptType.AES_ECDH_ECDSA);
        int remoteSystemId = TerraHarvestMessageHelper.getControllerId();
        int localSystemId = TerraHarvestMessageHelper.getSourceId();
        AsymmetricCipherKeyPair ephemeralKeyPair = EncryptionUtility.createKeyPair();
        byte[] ephemeralPublicKey = ((ECPublicKeyParameters)ephemeralKeyPair.getPublic()).getQ().getEncoded(false);
        DSADigestSigner dsaSigner = new DSADigestSigner(new ECDSASigner(), new SHA256Digest()); 
        dsaSigner.init(true, getPrivateStaticKey());
        dsaSigner.update(ephemeralPublicKey, 0, ephemeralPublicKey.length);
        byte[] ephemeralSignature = dsaSigner.generateSignature();
        byte [] sharedKeyBytesEncrypt = EncryptionUtility.sharedEcdhKeyCreate(
                    (ECPrivateKeyParameters)ephemeralKeyPair.getPrivate(), getRemotePublicKey(remoteSystemId), 
                    localSystemId, remoteSystemId);
        Key key = new SecretKeySpec(sharedKeyBytesEncrypt, "AES");
        byte[] ivBytes = new byte[12];
        EncryptionUtility.RANDOM_BC.nextBytes(ivBytes);
        IvParameterSpec ivBytes_New = new IvParameterSpec(ivBytes);
        cipher.init(Cipher.ENCRYPT_MODE, key, ivBytes_New);
        byte[] encrypt = cipher.doFinal(thMessage.getTerraHarvestPayload().toByteArray());
        EncryptInfo encryptInfo = EncryptInfo.newBuilder().
                setEphemeralKey(ByteString.copyFrom(ephemeralPublicKey)).
                setEphemeralKeySignature(ByteString.copyFrom(ephemeralSignature)).
                setInitializationVector(ByteString.copyFrom(ivBytes)).
                build();
        TerraHarvestMessage encryptedMessage = builder.setTerraHarvestPayload(ByteString.copyFrom(encrypt)).
                setEncryptInfo(encryptInfo).build();
        return encryptedMessage;
    }
public static TerraHarvestMessage encryptMessageWithECDH ( TerraHarvestMessage thMessage ) throws IllegalBlockSizeException , BadPaddingException , InvalidKeyException , InvalidAlgorithmParameterException , NoSuchAlgorithmException , NoSuchProviderException , NoSuchPaddingException { final Cipher cipher = Cipher . getInstance ( "AES/GCM/NoPadding" , "BC" ) ; TerraHarvestMessage . Builder builder = thMessage . toBuilder ( ) ; builder . setEncryptType ( EncryptType . AES_ECDH_ECDSA ) ; int remoteSystemId = TerraHarvestMessageHelper . getControllerId ( ) ; int localSystemId = TerraHarvestMessageHelper . getSourceId ( ) ; AsymmetricCipherKeyPair ephemeralKeyPair = EncryptionUtility . createKeyPair ( ) ; byte [ ] ephemeralPublicKey = ( ( ECPublicKeyParameters ) ephemeralKeyPair . getPublic ( ) ) . getQ ( ) . getEncoded ( false ) ; DSADigestSigner dsaSigner = new DSADigestSigner ( new ECDSASigner ( ) , new SHA256Digest ( ) ) ; dsaSigner . init ( true , getPrivateStaticKey ( ) ) ; dsaSigner . update ( ephemeralPublicKey , 0 , ephemeralPublicKey . length ) ; byte [ ] ephemeralSignature = dsaSigner . generateSignature ( ) ; byte [ ] sharedKeyBytesEncrypt = EncryptionUtility . sharedEcdhKeyCreate ( ( ECPrivateKeyParameters ) ephemeralKeyPair . getPrivate ( ) , getRemotePublicKey ( remoteSystemId ) , localSystemId , remoteSystemId ) ; Key key = new SecretKeySpec ( sharedKeyBytesEncrypt , "AES" ) ; byte [ ] ivBytes = new byte [ 12 ] ; EncryptionUtility . RANDOM_BC . nextBytes ( ivBytes ) ; IvParameterSpec ivBytes_New = new IvParameterSpec ( ivBytes ) ; cipher . init ( Cipher . ENCRYPT_MODE , key , ivBytes_New ) ; byte [ ] encrypt = cipher . doFinal ( thMessage . getTerraHarvestPayload ( ) . toByteArray ( ) ) ; EncryptInfo encryptInfo = EncryptInfo . newBuilder ( ) . setEphemeralKey ( ByteString . copyFrom ( ephemeralPublicKey ) ) . setEphemeralKeySignature ( ByteString . copyFrom ( ephemeralSignature ) ) . setInitializationVector ( ByteString . copyFrom ( ivBytes ) ) . build ( ) ; TerraHarvestMessage encryptedMessage = builder . setTerraHarvestPayload ( ByteString . copyFrom ( encrypt ) ) . setEncryptInfo ( encryptInfo ) . build ( ) ; return encryptedMessage ; }
Encrypt a message with ECDH.
Encrypt a message with ECDH .
//Retrieve the cipher to be used for encrypting messages. //need to set the encryption information so put the message to a builder //destination and source ids //asymmetrical key generator //digital signer will 'digest' or take parts of the ephemeral key and this systems //private key and create a signature that will be used when the message is received to verify the sender //create the shared key //initialization vector creation, these are random bytes,  //initialize the cipher, and encrypt the message //build EncryptInfo //build the message containing encrypted payload information