// Check AES Encription with short STring
        public void TestAESencryptionWithString()
        {
            String DataToBeEncoded = "Test";
            String expectedResult  = "567b77516c6d96978561ee8244b01afb";

            byte[] result    = EncryptionHelper.encryptStringWithAes(DataToBeEncoded, aesKey);
            String hexResult = HexEncDec.ByteArrayToString(result);

            Assert.IsTrue(String.Equals(expectedResult, hexResult, StringComparison.InvariantCultureIgnoreCase));
        }
        // Check AES Encription with Byte Array
        public void TestAESencryptionWithByteArray()
        {
            String originalData = "Super long test that should be encrypted properly";

            byte[] DataToBeEncoded = Encoding.UTF8.GetBytes(originalData);
            String expectedResult  = "8031b6fb67ee4cf45e5bff5e6927f016675ed9c8e89b5aed8f9418c8ca04b65706c71a65039302e937342eed892be761251bb3596b64145060fd478a2fe839c7";

            byte[] result    = EncryptionHelper.encryptBytesWithAes(DataToBeEncoded, aesKey);
            String hexResult = HexEncDec.ByteArrayToString(result);

            Assert.IsTrue(String.Equals(expectedResult, hexResult, StringComparison.InvariantCultureIgnoreCase));

            byte[] decodeResult    = EncryptionHelper.decryptWithAes(result, aesKey);
            String decodeStrResult = Encoding.UTF8.GetString(decodeResult);

            Assert.IsTrue(String.Equals(decodeStrResult, originalData, StringComparison.InvariantCultureIgnoreCase));
        }
Exemplo n.º 3
0
        public void TestWalletSignaturesNonDeterministic()
        {
            //This is the comparison class
            CompareLogic compareLogic = new CompareLogic();

            List <String> mnemonic = new List <String>(singleVector2.Split(" ", StringSplitOptions.RemoveEmptyEntries));
            NetworkInfo   info     = new NetworkInfo(bech32Hrp: "did:com:", lcdUrl: "");
            Wallet        wallet   = Wallet.derive(mnemonic, networkInfo);
            String        data     = "Quos Iupiter perdere vult, dementat prius";

            String signature1 = HexEncDec.ByteArrayToString(wallet.sign(Encoding.UTF8.GetBytes(data)));
            String signature2 = HexEncDec.ByteArrayToString(wallet.sign(Encoding.UTF8.GetBytes(data)));

            // Check it - we use compareNet objects here
            // a String comparison would have been enough...
            ComparisonResult result = compareLogic.Compare(signature1, signature2);

            Assert.AreEqual(result.AreEqual, false);
        }
        // Here we have to address the Dart direct methods - to be encapsulated in a class in C#
        #region Properties

        #endregion

        #region Constructors

        #endregion

        #region Public Methods

        /// Transforms [this] document into one having the proper fields encrypted as
        /// specified inside the [encryptedData] list.
        /// All the fields will be encrypted using the specified [aesKey].
        /// This key will later be encrypted for each and every Did specified into
        /// the [recipients] list.
        /// The overall encrypted data will be put inside the proper document field.
        public static async Task <CommercioDoc> encryptField(
            CommercioDoc doc,
            KeyParameter aesKey,
            List <EncryptedData> encryptedData,
            List <String> recipients,
            Wallet wallet
            )
        {
            // -----------------
            // --- Encryption
            // -----------------

            // Encrypt the contents
            String encryptedContentUri = null;

            if (encryptedData.Contains(EncryptedData.CONTENT_URI))
            {
                encryptedContentUri = HexEncDec.ByteArrayToString(EncryptionHelper.encryptStringWithAes(doc.contentUri, aesKey));
            }

            String encryptedMetadataContentUri = null;

            if (encryptedData.Contains(EncryptedData.METADATA_CONTENT_URI))
            {
                encryptedMetadataContentUri = HexEncDec.ByteArrayToString(EncryptionHelper.encryptStringWithAes(doc.metadata.contentUri, aesKey));
            }

            String encryptedMetadataSchemaUri = null;

            if (encryptedData.Contains(EncryptedData.METADATA_SCHEMA_URI))
            {
                String schemaUri = doc.metadata.schema?.uri;
                if (schemaUri != null)
                {
                    encryptedMetadataSchemaUri = HexEncDec.ByteArrayToString(EncryptionHelper.encryptStringWithAes(schemaUri, aesKey));
                }
            }

            // ---------------------
            // --- Keys creation
            // ---------------------

            // I will trasform all the Dart function instructions in imperative loops
            // Get the recipients Did Documents
            List <DidDocument> recipientsDidDocs = new List <DidDocument>();

            foreach (String recipient in recipients)
            {
                recipientsDidDocs.Add(await IdHelper.getDidDocument(recipient, wallet));
            }

            // Get a list of al the Did Documents and the associated encryption key
            List <_Pair> keys = new List <_Pair>();

            foreach (DidDocument didDoc in recipientsDidDocs)
            {
                if (didDoc != null)
                {
                    _Pair p = new _Pair(didDoc);
                    if (p.pubKey != null)
                    {
                        keys.Add(p);
                    }
                }
            }

            // Create the encryption key field
            List <CommercioDocEncryptionDataKey> encryptionKeys = new List <CommercioDocEncryptionDataKey>();

            foreach (_Pair pair in keys)
            {
                byte[] encryptedAesKey = EncryptionHelper.encryptBytesWithRsa(
                    aesKey.GetKey(),
                    pair.pubKey
                    );
                CommercioDocEncryptionDataKey dataKey = new CommercioDocEncryptionDataKey(
                    recipientDid: pair.document.id,
                    value: HexEncDec.ByteArrayToString(encryptedAesKey)
                    );
                encryptionKeys.Add(dataKey);
            }

            // Copy the metadata
            CommercioDocMetadataSchema metadataSchema = doc.metadata?.schema;

            if (metadataSchema != null)
            {
                metadataSchema = new CommercioDocMetadataSchema(
                    version: metadataSchema.version,
                    uri: encryptedMetadataSchemaUri ?? metadataSchema.uri
                    );
            }

            // Return a copy of the document
            return(new CommercioDoc(
                       senderDid: doc.senderDid,
                       recipientDids: doc.recipientDids,
                       uuid: doc.uuid,
                       checksum: doc.checksum,
                       contentUri: encryptedContentUri ?? doc.contentUri,
                       metadata: new CommercioDocMetadata(
                           contentUri: encryptedMetadataContentUri ?? doc.metadata.contentUri,
                           schema: metadataSchema,
                           schemaType: doc.metadata.schemaType
                           ),
                       encryptionData: new CommercioDocEncryptionData(
                           keys: encryptionKeys,
                           encryptedData: encryptedData.Select((e) => e.ToEnumMemberAttrValue()).ToList()
                           )
                       ));
        }