// 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 decription
        public void TestAESdecryption()
        {
            String originalData   = "8031b6fb67ee4cf45e5bff5e6927f016675ed9c8e89b5aed8f9418c8ca04b65706c71a65039302e937342eed892be761251bb3596b64145060fd478a2fe839c7";
            String expectedResult = "Super long test that should be encrypted properly";

            byte[] DataToBeDecoded = HexEncDec.StringToByteArray(originalData);

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

            Assert.IsTrue(String.Equals(expectedResult, result, StringComparison.InvariantCulture));
        }
        // **** Code to be reviewed - it compiles, but he has little if any relationship with the Dart code...
        // ****
        public String getEncoded()
        {
            byte[] seedBytes = HexEncDec.StringToByteArray(Seed);
            Ed25519KeyPairGenerator        wkGen  = new Ed25519KeyPairGenerator();
            SecureRandom                   rnd    = new SecureRandom(seedBytes);
            Ed25519KeyGenerationParameters genPar = new Ed25519KeyGenerationParameters(rnd);

            wkGen.Init(genPar);
            AsymmetricCipherKeyPair    keys = wkGen.GenerateKeyPair();
            Ed25519PublicKeyParameters key  = (Ed25519PublicKeyParameters)keys.Public;

            byte[] output = key.GetEncoded();
            return(Convert.ToBase64String(output));
        }
        public void TestParsingKeys()
        {
            String RSAPublicText = TestResources.RSAPublicText;

            // Test the parsing
            RsaKeyParameters pubKey = RSAKeyParser.parsePublicKeyFromPem(RSAPublicText);
            // Here to make debug simpler...
            BigInteger modulus     = pubKey.Modulus;
            BigInteger exp         = pubKey.Exponent;
            BigInteger testModulus = new BigInteger(HexEncDec.StringToByteArray(TestResources.EncodedHexPubModulus));
            BigInteger testExp     = new BigInteger(TestResources.PubExponentTextValue.ToString());

            // Check the RSA
            Assert.IsTrue(modulus.CompareTo(testModulus) == 0);
            Assert.IsTrue(exp.CompareTo(testExp) == 0);
        }
Exemplo n.º 5
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()
                           )
                       ));
        }