Exemplo n.º 1
0
        public void send_a_message_with_digital_signature_expect_intercepted_message_as_invalid()
        {
            // User generates a message, hashes the message and signs it with their PRIVATE key //
            var message = Encoding.UTF8.GetBytes("This is a message that will be changed after the digital signature.");

            // User sends the message //
            byte[] hashedMessage = Hashing.ComputeHashSha256(message);

            // Receiver calculates the hash of the encrypted data //
            var digitalSignature = new DigitalSignature();

            digitalSignature.AssignNewKey();

            // this signature is sent along with the message ? //
            var signature = digitalSignature.SignData(hashedMessage);

            // Message intercepted and altered //
            message       = Encoding.UTF8.GetBytes("This is a message that was intercepted after the signature.");
            hashedMessage = Hashing.ComputeHashSha256(message);

            // Receiver verifies the digital signature using the public key //
            var verified = digitalSignature.VerifySignature(hashedMessage, signature);

            // the message should be verified at this point//
            Assert.IsFalse(verified);
        }
Exemplo n.º 2
0
        public void SaveAndLoadKeys()
        {
            var ds = new DigitalSignature();

            ds.AssignNewKey();

            ds.SavePublicKey(out var exponent1, out var modulus1);
            ds.SavePrivateKey(out var exponent2, out var modulus2, out var p, out var q, out var dp, out var dq, out var inverseQ, out var d);

            var signature1 = ds.SignData(_hash);
            var result1    = ds.VerifySignature(_hash, signature1);

            Assert.IsTrue(result1);


            var sut = new DigitalSignature();

            sut.LoadPublicKey(exponent1, modulus1);
            sut.LoadPrivateKey(exponent2, modulus2, p, q, dp, dq, inverseQ, d);

            var signature2 = ds.SignData(_hash);
            var result2    = ds.VerifySignature(_hash, signature2);

            Assert.IsTrue(result2);

            Assert.AreEqual(signature1, signature2);
        }
Exemplo n.º 3
0
        public void HashAlgorithmTests(int keySize, string hashAlgorithm)
        {
            var ds = new DigitalSignature(keySize, hashAlgorithm);

            ds.AssignNewKey();

            byte[] hash;
            switch (hashAlgorithm)
            {
            case "SHA1":
                hash = Hash.Create(HashType.SHA1, "Hello world", string.Empty);
                break;

            case "SHA256":
                hash = Hash.Create(HashType.SHA256, "Hello world", string.Empty);
                break;

            case "SHA384":
                hash = Hash.Create(HashType.SHA384, "Hello world", string.Empty);
                break;

            case "SHA512":
                hash = Hash.Create(HashType.SHA512, "Hello world", string.Empty);
                break;

            default:
                throw new ArgumentException("hashAlgorithm");
            }
            var signature = ds.SignData(hash);
            var result    = ds.VerifySignature(hash, signature);

            Assert.IsTrue(result);
        }
Exemplo n.º 4
0
        public Form4()
        {
            InitializeComponent();
            rsa.AssignNewKey();

            string      subKey = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion";
            RegistryKey key    = Microsoft.Win32.Registry.LocalMachine;
            RegistryKey skey   = key.OpenSubKey(subKey);

            string name      = skey.GetValue("ProductName").ToString();
            string releaseId = skey.GetValue("ReleaseId").ToString();

            lb_osInfo.Text = name + " " + releaseId + (Environment.Is64BitOperatingSystem ? " 64-bit" : " 32-bit");

            if (File.Exists("data.blc"))
            {
                chain.LoadFile("data.blc");

                for (int i = 0; i < chain.Blocks.Count; i++)
                {
                    nBlock++;
                    cb_blockList.Items.Add("Block " + (i + 1).ToString());
                }
                lb_blockName.Text          = "Block " + (nBlock + 1).ToString();
                cb_blockList.SelectedIndex = cb_blockList.Items.Count - 1;
                cacheBlock = new Block(nBlock);
            }
        }
Exemplo n.º 5
0
        //[TestCase(8192)] // Slow
        //[TestCase(16384)] // Very slow
        public void KeySizeTests(int keySize)
        {
            var ds = new DigitalSignature(keySize, "SHA256");

            ds.AssignNewKey();

            var signature = ds.SignData(_hash);
            var result    = ds.VerifySignature(_hash, signature);

            Assert.IsTrue(result);
        }
Exemplo n.º 6
0
        public void SavedPublicAndPrivateKeysMatch()
        {
            var ds = new DigitalSignature();

            ds.AssignNewKey();

            ds.SavePublicKey(out var exponent1, out var modulus1);
            ds.SavePrivateKey(out var exponent2, out var modulus2, out var p, out var q, out var dp, out var dq, out var inverseQ, out var d);

            Assert.AreEqual(exponent1, exponent2);
            Assert.AreEqual(modulus1, modulus2);
        }
Exemplo n.º 7
0
        public static string HashDocumentAndSignData(ref DigitalSignature newDigSig, string document)
        {
            byte[] hashedDocument = newDigSig.HashDocument(Encoding.UTF8.GetBytes(document));

            newDigSig.AssignNewKey();
            var signature = newDigSig.SignData(hashedDocument);

            DocumentAndSignature = new StoredDocumentAndSignature {
                Document = document, HashedDocument = hashedDocument, Signature = signature
            };

            return("Document er hashed og signed");
        }
Exemplo n.º 8
0
        public Form1()
        {
            InitializeComponent();
            rsa.AssignNewKey();

            if (File.Exists("Blockchain.txt"))
            {
                chain.LoadFile("Blockchain.txt");

                for (int i = 0; i < chain.Blocks.Count; i++)
                {
                    nBlock++;
                    cb_blockList.Items.Add("Block " + (i + 1).ToString());
                }
                txt_nameBlock.Text         = "Block " + (nBlock + 1).ToString();
                cb_blockList.SelectedIndex = cb_blockList.Items.Count - 1;
                cacheBlock = new Block(nBlock);
            }
        }
Exemplo n.º 9
0
        public void send_a_message_with_digital_signature_expect_message_validation()
        {
            // User generates a message, hashes the message and signs it with their PRIVATE key //
            var message = Encoding.UTF8.GetBytes("This is a message that will be verified through digital signature.");

            // User sends the message //
            byte[] hashedMessage = Hashing.ComputeHashSha256(message);

            // Receiver calculates the hash of the encrypted data //
            var digitalSignature = new DigitalSignature();

            digitalSignature.AssignNewKey();

            // Receiver verifies the digital signature using the public key //
            var signature = digitalSignature.SignData(hashedMessage);
            var verified  = digitalSignature.VerifySignature(hashedMessage, signature);

            // the message should be verified at this point//
            Assert.IsTrue(verified);
        }
Exemplo n.º 10
0
 public KeyStore(byte[] authenticatedHashKey)
 {
     AuthenticatedHashKey = authenticatedHashKey;
     DigitalSignature     = new DigitalSignature();
     DigitalSignature.AssignNewKey();
 }
Exemplo n.º 11
0
 public HybridEncryption()
 {
     _aes = new AesEncryption();
     _digitalSignature = new DigitalSignature();
     _digitalSignature.AssignNewKey();
 }
Exemplo n.º 12
0
		static void Main()
		{
			//const string original = "Very secret and important information that can not fall into the wrong hands.";
			//string original = new String('0', 127);
			string original = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrs";
			original = GenerateRandomText();
			//string original = "abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890";
			//string original = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam non dictum diam. Donec feugiat libero sed arcu interdum consectetur vitae amet.";
			//string original = @"?=??Y@쳘?{?? &긳 ? v ? ";

			var hybrid = new HybridEncryption();

			var rsaParams = new RSAWithRSAParameterKey();
			rsaParams.ImportKeys();

			var digitalSignature = new DigitalSignature();
			digitalSignature.AssignNewKey();

			Console.WriteLine("Hybrid Encryption with Integrity Check Demonstration in .NET");
			Console.WriteLine("------------------------------------------------------------");
			Console.WriteLine();

			try
			{
				var originalData = Encoding.UTF8.GetBytes(original);

				byte[] compressedBytes = Compress(originalData);
				byte[] decompressedBytes = Decompress(compressedBytes);

				var encryptedBlock = hybrid.EncryptData(
					originalData, rsaParams, digitalSignature);

				var decrpyted = hybrid.DecryptData(encryptedBlock, rsaParams, digitalSignature);

				//byte[] gzippedBytes = GetGZippedBytes(encryptedBlock.EncryptedData);
				//byte[] ungzippedBytes = GetUnGZippedBytes(gzippedBytes);
				byte[] gzippedBytes = Compress(encryptedBlock.EncryptedData);
				byte[] ungzippedBytes = Decompress(gzippedBytes);

				Console.WriteLine("Original Message = " + original);
				Console.WriteLine("Original Message Length: {0}", original.Length);
				Console.WriteLine("Compressed Original Message = " + Convert.ToBase64String(compressedBytes));
				Console.WriteLine("Compressed Original Message Length: {0}", compressedBytes.Length);
				Console.WriteLine("DeCompressed Original Message = " + Convert.ToBase64String(decompressedBytes));
				Console.WriteLine("DeCompressed Original Message Length: {0}", decompressedBytes.Length);
				Console.WriteLine("Encrypted Data: {0}", Convert.ToBase64String(encryptedBlock.EncryptedData));
				Console.WriteLine("Encrypted Data Size: {0}", encryptedBlock.EncryptedData.Length);
				Console.WriteLine("GZipped Encrypted Data: {0}", Convert.ToBase64String(gzippedBytes));
				Console.WriteLine("GZipped Encrypted Data Size: {0}", gzippedBytes.Length);
				Console.WriteLine("UnGZipped Encrypted Data: {0}", Convert.ToBase64String(ungzippedBytes));
				Console.WriteLine("UnGZipped Encrypted Data Size: {0}", ungzippedBytes.Length);
				Console.WriteLine();
				Console.WriteLine("Message After Decryption = " + Encoding.UTF8.GetString(decrpyted));
			}
			catch (CryptographicException ex)
			{
				Console.WriteLine("Error : " + ex.Message);
			}

			Console.ReadLine();
		}