mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/encryption/TestEncryptMessageServiceImpl.java
@Test
    public final void testEncryptDecryptPayload() 
        throws IOException, NoSuchAlgorithmException, NoSuchProviderException, 
        NoSuchPaddingException, InvalidKeySignatureException 
    {
        m_SUT.activate(m_Context);

        // First generate keys for the controllers. In reality this would be done in advance and the keys
        // loaded into a config file on each controller.
        // In our case source is controller A with controller id =1 and destination is controller B with id = 2
        
        final AsymmetricCipherKeyPair keyPairA = EncryptionUtility.createKeyPair();
        //Extract public key bytes
        final byte[] staticPublicKeyBytesA = ((ECPublicKeyParameters)keyPairA.getPublic()).getQ().getEncoded(false);
        //First convert the key to hexadecimal format string to save it into the file
        final String staticPublicKeyStringA = EncryptionHelper.toHex(staticPublicKeyBytesA);
        
        //Extract private key bytes
        final BigInteger staticPrivateKeyBytesA = ((ECPrivateKeyParameters)keyPairA.getPrivate()).getD();
        //First convert the key to hexadecimal format string to save it into the file
        final String staticPrivateKeyStringA = staticPrivateKeyBytesA.toString(16);
        
        m_SUT.saveStaticKeyPair(staticPublicKeyStringA, staticPrivateKeyStringA);
        m_SUT.saveAuthorizedKey(1, staticPublicKeyStringA);
   
        final AsymmetricCipherKeyPair keyPairB = EncryptionUtility.createKeyPair();
        //Extract public key bytes
        final byte[] staticPublicKeyBytesB = ((ECPublicKeyParameters)keyPairB.getPublic()).getQ().getEncoded(false);
        //First convert the key to hexadecimal format string to save it into the file
        final String staticPublicKeyStringB = EncryptionHelper.toHex(staticPublicKeyBytesB);
        
        //Extract private key bytes
        final BigInteger staticPrivateKeyBytesB = ((ECPrivateKeyParameters)keyPairB.getPrivate()).getD();
        //First convert the key to hexadecimal format string to save it into the file
        final String staticPrivateKeyStringB = staticPrivateKeyBytesB.toString(16);
        
        //set the private static keys for both the controller 2 which is the destination controller
        //In normal circumstances you don't have to do this step

        m_SUT.saveAuthorizedKey(2, staticPublicKeyStringB);
 
        // construct a single message containing data within a namespace message
        ControllerInfoData systemInfoData = ControllerInfoData.newBuilder()
                .setName("test-name")
                .build();
        BaseNamespace baseNamespaceMessage = BaseNamespace.newBuilder()
                .setType(BaseMessageType.ControllerInfo)
                .setData(systemInfoData.toByteString())
                .build();
        //build payload
        TerraHarvestPayload payload = TerraHarvestPayload.newBuilder().
                setNamespace(Namespace.Base).
                setNamespaceMessage(baseNamespaceMessage.toByteString()).
                build();
        // build a terra harvest message
        final TerraHarvestMessage.Builder fullMessage = TerraHarvestMessage.newBuilder().
                setVersion(RemoteConstants.SPEC_VERSION).
                setSourceId(1).
                // send message back to source
                setDestId(2).
                setMessageId(100).
                setEncryptType(RemoteBase.EncryptType.AES_ECDH_ECDSA);
                
        //Encrypt payload
        TerraHarvestMessage fullEncryptedMessage = m_SUT.encryptMessage(fullMessage, payload); 
        
        //Each controller will have to do it independently. but for testing purposes I also set the
        //pub-priv key for controller 2
        m_SUT.saveStaticKeyPair(staticPublicKeyStringB, staticPrivateKeyStringB);
        
        //Decrypt payload
        TerraHarvestPayload payloadDecrypted = m_SUT.decryptRemoteMessage(fullEncryptedMessage);
        System.out.printf("Original payload:  %s\n", payload);
        System.out.printf("After Encryption: %s\n", fullEncryptedMessage);
        System.out.printf("After Decryption: %s\n", payloadDecrypted);
        
        assertThat(payloadDecrypted, is(payload));        
    }
/**
     * This function encrypts a TerraHarvest payload which is inside a TerraHarvest message.
     * On the other end it is decrypted at the destination controller. The decrypted message is compared
     * to the unencrypted payload to make sure they are the same.
     * <p>
     * System 1, Key A are all related to Source or sender controller<br>
     * System 2, Key B are all related to Destination or recipient controller
     */
This function encrypts a TerraHarvest payload which is inside a TerraHarvest message.
On the other end it is decrypted at the destination controller. The decrypted message is compared
to the unencrypted payload to make sure they are the same.

System 1, Key A are all related to Source or sender controller
System 2, Key B are all related to Destination or recipient controller
This function encrypts a TerraHarvest payload which is inside a TerraHarvest message . On the other end it is decrypted at the destination controller . The decrypted message is compared to the unencrypted payload to make sure they are the same . System 1 Key A are all related to Source or sender controller System 2 Key B are all related to Destination or recipient controller
@Test
    public final void testEncryptDecryptPayload() 
        throws IOException, NoSuchAlgorithmException, NoSuchProviderException, 
        NoSuchPaddingException, InvalidKeySignatureException 
    {
        m_SUT.activate(m_Context);
        final AsymmetricCipherKeyPair keyPairA = EncryptionUtility.createKeyPair();
        final byte[] staticPublicKeyBytesA = ((ECPublicKeyParameters)keyPairA.getPublic()).getQ().getEncoded(false);
        final String staticPublicKeyStringA = EncryptionHelper.toHex(staticPublicKeyBytesA);
        final BigInteger staticPrivateKeyBytesA = ((ECPrivateKeyParameters)keyPairA.getPrivate()).getD();
        final String staticPrivateKeyStringA = staticPrivateKeyBytesA.toString(16);
        m_SUT.saveStaticKeyPair(staticPublicKeyStringA, staticPrivateKeyStringA);
        m_SUT.saveAuthorizedKey(1, staticPublicKeyStringA);
        final AsymmetricCipherKeyPair keyPairB = EncryptionUtility.createKeyPair();
        final byte[] staticPublicKeyBytesB = ((ECPublicKeyParameters)keyPairB.getPublic()).getQ().getEncoded(false);
        final String staticPublicKeyStringB = EncryptionHelper.toHex(staticPublicKeyBytesB);
        final BigInteger staticPrivateKeyBytesB = ((ECPrivateKeyParameters)keyPairB.getPrivate()).getD();
        final String staticPrivateKeyStringB = staticPrivateKeyBytesB.toString(16);
        m_SUT.saveAuthorizedKey(2, staticPublicKeyStringB);
        ControllerInfoData systemInfoData = ControllerInfoData.newBuilder()
                .setName("test-name")
                .build();
        BaseNamespace baseNamespaceMessage = BaseNamespace.newBuilder()
                .setType(BaseMessageType.ControllerInfo)
                .setData(systemInfoData.toByteString())
                .build();
        TerraHarvestPayload payload = TerraHarvestPayload.newBuilder().
                setNamespace(Namespace.Base).
                setNamespaceMessage(baseNamespaceMessage.toByteString()).
                build();
        final TerraHarvestMessage.Builder fullMessage = TerraHarvestMessage.newBuilder().
                setVersion(RemoteConstants.SPEC_VERSION).
                setSourceId(1).
                setDestId(2).
                setMessageId(100).
                setEncryptType(RemoteBase.EncryptType.AES_ECDH_ECDSA);
        TerraHarvestMessage fullEncryptedMessage = m_SUT.encryptMessage(fullMessage, payload); 
        m_SUT.saveStaticKeyPair(staticPublicKeyStringB, staticPrivateKeyStringB);
        TerraHarvestPayload payloadDecrypted = m_SUT.decryptRemoteMessage(fullEncryptedMessage);
        System.out.printf("Original payload:  %s\n", payload);
        System.out.printf("After Encryption: %s\n", fullEncryptedMessage);
        System.out.printf("After Decryption: %s\n", payloadDecrypted);
        assertThat(payloadDecrypted, is(payload));        
    }
@ Test public final void testEncryptDecryptPayload ( ) throws IOException , NoSuchAlgorithmException , NoSuchProviderException , NoSuchPaddingException , InvalidKeySignatureException { m_SUT . activate ( m_Context ) ; final AsymmetricCipherKeyPair keyPairA = EncryptionUtility . createKeyPair ( ) ; final byte [ ] staticPublicKeyBytesA = ( ( ECPublicKeyParameters ) keyPairA . getPublic ( ) ) . getQ ( ) . getEncoded ( false ) ; final String staticPublicKeyStringA = EncryptionHelper . toHex ( staticPublicKeyBytesA ) ; final BigInteger staticPrivateKeyBytesA = ( ( ECPrivateKeyParameters ) keyPairA . getPrivate ( ) ) . getD ( ) ; final String staticPrivateKeyStringA = staticPrivateKeyBytesA . toString ( 16 ) ; m_SUT . saveStaticKeyPair ( staticPublicKeyStringA , staticPrivateKeyStringA ) ; m_SUT . saveAuthorizedKey ( 1 , staticPublicKeyStringA ) ; final AsymmetricCipherKeyPair keyPairB = EncryptionUtility . createKeyPair ( ) ; final byte [ ] staticPublicKeyBytesB = ( ( ECPublicKeyParameters ) keyPairB . getPublic ( ) ) . getQ ( ) . getEncoded ( false ) ; final String staticPublicKeyStringB = EncryptionHelper . toHex ( staticPublicKeyBytesB ) ; final BigInteger staticPrivateKeyBytesB = ( ( ECPrivateKeyParameters ) keyPairB . getPrivate ( ) ) . getD ( ) ; final String staticPrivateKeyStringB = staticPrivateKeyBytesB . toString ( 16 ) ; m_SUT . saveAuthorizedKey ( 2 , staticPublicKeyStringB ) ; ControllerInfoData systemInfoData = ControllerInfoData . newBuilder ( ) . setName ( "test-name" ) . build ( ) ; BaseNamespace baseNamespaceMessage = BaseNamespace . newBuilder ( ) . setType ( BaseMessageType . ControllerInfo ) . setData ( systemInfoData . toByteString ( ) ) . build ( ) ; TerraHarvestPayload payload = TerraHarvestPayload . newBuilder ( ) . setNamespace ( Namespace . Base ) . setNamespaceMessage ( baseNamespaceMessage . toByteString ( ) ) . build ( ) ; final TerraHarvestMessage . Builder fullMessage = TerraHarvestMessage . newBuilder ( ) . setVersion ( RemoteConstants . SPEC_VERSION ) . setSourceId ( 1 ) . setDestId ( 2 ) . setMessageId ( 100 ) . setEncryptType ( RemoteBase . EncryptType . AES_ECDH_ECDSA ) ; TerraHarvestMessage fullEncryptedMessage = m_SUT . encryptMessage ( fullMessage , payload ) ; m_SUT . saveStaticKeyPair ( staticPublicKeyStringB , staticPrivateKeyStringB ) ; TerraHarvestPayload payloadDecrypted = m_SUT . decryptRemoteMessage ( fullEncryptedMessage ) ; System . out . printf ( "Original payload:  %s\n" , payload ) ; System . out . printf ( "After Encryption: %s\n" , fullEncryptedMessage ) ; System . out . printf ( "After Decryption: %s\n" , payloadDecrypted ) ; assertThat ( payloadDecrypted , is ( payload ) ) ; }
This function encrypts a TerraHarvest payload which is inside a TerraHarvest message.
This function encrypts a TerraHarvest payload which is inside a TerraHarvest message .
// First generate keys for the controllers. In reality this would be done in advance and the keys // loaded into a config file on each controller. // In our case source is controller A with controller id =1 and destination is controller B with id = 2 //Extract public key bytes //First convert the key to hexadecimal format string to save it into the file //Extract private key bytes //First convert the key to hexadecimal format string to save it into the file //Extract public key bytes //First convert the key to hexadecimal format string to save it into the file //Extract private key bytes //First convert the key to hexadecimal format string to save it into the file //set the private static keys for both the controller 2 which is the destination controller //In normal circumstances you don't have to do this step // construct a single message containing data within a namespace message //build payload // build a terra harvest message // send message back to source //Encrypt payload //Each controller will have to do it independently. but for testing purposes I also set the //pub-priv key for controller 2 //Decrypt payload