예제 #1
0
        public Principal(string name, int generator, int modulus)
        {
            Console.WriteLine($"Creating principal {name}...");
            Name      = name;
            Generator = generator;
            Modulus   = modulus;

            _secret       = PrimeGenerator.GetPrime();
            SecretMessage = SquareAndMultiply.Calculate(Generator, _secret, Modulus);

            Console.WriteLine($"{Name} has selected a secret: {_secret}");
            Console.WriteLine($"{Name} calculated its secret message ({Generator}^{_secret}) mod {modulus}: {SecretMessage}");

            rsa         = new RSACryptoServiceProvider(2048);
            _publicKey  = rsa.ExportParameters(false);
            _privateKey = rsa.ExportParameters(true);

            Console.WriteLine($"{Name} created 2048 bit RSA public and private keys");

            RSAPublicKeyStore.AddPublicKey(Name, _publicKey);

            _secretMessages       = new Dictionary <string, int>();
            _symmetricKeys        = new Dictionary <string, string>();
            _authorizedPrincipals = new HashSet <string>();

            Console.WriteLine($"Principal {Name} has been created successfully.\r\n");
        }
예제 #2
0
        private bool VerifyEncryptedMessage(string principalName, string encryptedMessage)
        {
            Console.WriteLine($"{Name} is decrypting and verifying the encrypted message...");

            RSACryptoServiceProvider rsaVerify = new RSACryptoServiceProvider();

            rsaVerify.ImportParameters(RSAPublicKeyStore.GetPublicKey(principalName));

            string message = (((long)_secretMessages[principalName] << 32) | SecretMessage).ToString();

            byte[] messageBytes = Encoding.Unicode.GetBytes(message);

            string decryptedMessage = Encryption.Decrypt(encryptedMessage, _symmetricKeys[principalName]);

            byte[] decryptedMessageBytes = Convert.FromBase64String(decryptedMessage);
            bool   isVerified            = rsaVerify.VerifyData(messageBytes, CryptoConfig.MapNameToOID("SHA512"), decryptedMessageBytes);

            Console.WriteLine($"{Name}, decrypted message: {decryptedMessage}");
            Console.WriteLine($"{Name}, verifying decrpyted message with {principalName}'s public key...");
            if (isVerified)
            {
                Console.WriteLine($"{Name}, verification is successfull.");
            }
            else
            {
                Console.WriteLine($"{Name}, encrypted message cannot be verified!");
            }

            return(isVerified);
        }