public void EncryptDecrypt_FromFile()
        {
            RsaHelpers.GenerateRsaKeys(pubPrivFilePath: _pubPrivFilePath, pubOnlyFilePath: _pubOnlyFilePath);
            string _cipherText = RsaHelpers.Encrypt(filePath: _pubOnlyFilePath, value: _plainText);

            Assert.AreEqual(_plainText, RsaHelpers.Decrypt(filePath: _pubPrivFilePath, value: _cipherText));
        }
        public void EncryptDecrypt_FromContainer()
        {
            RsaHelpers.GenerateRsaKeys(keyContainerName: _kcn);
            string _encryptedText = RsaHelpers.Encrypt(keyContainerName: _kcn, value: _plainText);

            Assert.AreEqual(_plainText, RsaHelpers.Decrypt(keyContainerName: _kcn, value: _encryptedText));
            Assert.Throws <FormatException>(() => RsaHelpers.Decrypt(keyContainerName: _kcn, value: _encryptedText + "x"));
        }
        public void EncryptDecrypt_FromFile_LongKey()
        {
            RsaHelpers.GenerateRsaKeys(pubPrivFilePath: _pubPrivFilePath, pubOnlyFilePath: _pubOnlyFilePath, keySize: 16384);
            _plainText = "password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__password__";
            string _cipherText = RsaHelpers.Encrypt(filePath: _pubOnlyFilePath, value: _plainText);

            Assert.AreEqual(_plainText, RsaHelpers.Decrypt(filePath: _pubPrivFilePath, value: _cipherText));
        }
Exemple #4
0
    static void RsaGenKey(string keyContainerName, string keyFile, int keySize, bool showHelp)
    {
        if (showHelp)
        {
            List <Parameter> parms = new List <Parameter>
            {
                new Parameter {
                    Key = "[kcn]", Type = typeof(string), HelpText = "Key container name"
                },
                new Parameter {
                    Key = "[keyFile]", Type = typeof(string), HelpText = "Path to key files"
                },
                new Parameter {
                    Key = "[keySize]", Type = typeof(int), HelpText = "Key size"
                }
            };
            ConsoleColor defaultColor = Console.ForegroundColor;
            Console_WriteLine($"Parameter options for Rsa genkey:\r\n", ConsoleColor.Green);
            WriteMethodParametersHelp(parms);
            Console.WriteLine("\r\nNote:");
            Console.WriteLine("Specify either key container name or path to key files.\r\n");
            Console.WriteLine("If keyFile is specified, action will create the following 2 files:");
            Console.WriteLine("1. {keyFile}.pubPriv (public and private key)");
            Console.WriteLine("2. {keyFile}.pubOnly (public key only)");
            Console.ForegroundColor = defaultColor;
        }
        else
        {
            Console.WriteLine("Generating Rsa key pair.\r\n");
            if (string.IsNullOrWhiteSpace(keyFile))
            {
                RsaHelpers.GenerateRsaKeys(keyContainerName: keyContainerName, keySize: keySize);
            }
            else
            {
                RsaHelpers.GenerateRsaKeys(keyContainerName: keyContainerName, pubPrivFilePath: $"{keyFile}.pubPriv", pubOnlyFilePath: $"{keyFile}.pubOnly", keySize: keySize);
            }

            if (!string.IsNullOrWhiteSpace(keyContainerName))
            {
                Console.WriteLine($"Created public/private keypair in user profile key store.");
            }
            if (!string.IsNullOrWhiteSpace(keyFile))
            {
                Console.WriteLine($"Created public/private keypair in [{keyFile}.pubPriv].");
                Console.WriteLine($"Created public key (only) in [{keyFile}.pubOnly].");
            }
        }
    }
        public void GenerateRsaKeys_FileOnly()
        {
            if (File.Exists(_pubPrivFilePath))
            {
                File.Delete(_pubPrivFilePath);
            }
            if (File.Exists(_pubOnlyFilePath))
            {
                File.Delete(_pubOnlyFilePath);
            }

            RsaHelpers.GenerateRsaKeys((string)null, _pubPrivFilePath, _pubOnlyFilePath);
            Assert.IsTrue(File.Exists(_pubPrivFilePath));
            Assert.IsTrue(File.Exists(_pubOnlyFilePath));
        }
        public void GenerateRsaKeys_ContainerAndFile()
        {
            if (File.Exists(_pubPrivFilePath))
            {
                File.Delete(_pubPrivFilePath);
            }
            if (File.Exists(_pubOnlyFilePath))
            {
                File.Delete(_pubOnlyFilePath);
            }

            RsaHelpers.GenerateRsaKeys(_kcn, _pubPrivFilePath, _pubOnlyFilePath);
            Assert.IsTrue(RsaHelpers.KeyContainerExists(_kcn));
            Assert.IsTrue(File.Exists(_pubPrivFilePath));
            Assert.IsTrue(File.Exists(_pubOnlyFilePath));
        }
 public void GenerateRsaKeys_FileOnly_BadPath()
 {
     Assert.Throws <DirectoryNotFoundException>(() => RsaHelpers.GenerateRsaKeys((string)null, _badFilePath, null));
     Assert.Throws <DirectoryNotFoundException>(() => RsaHelpers.GenerateRsaKeys((string)null, null, _badFilePath));
 }
 public void GenerateRsaKeys_NullAndEmptyInputs()
 {
     Assert.Throws <ArgumentException>(() => RsaHelpers.GenerateRsaKeys((string)null, null, null));
     Assert.Throws <ArgumentException>(() => RsaHelpers.GenerateRsaKeys(string.Empty, string.Empty, string.Empty));
 }
 public void Decrypt_BadValue()
 {
     RsaHelpers.GenerateRsaKeys(pubPrivFilePath: _pubPrivFilePath);
     Assert.Throws <FormatException>(() => RsaHelpers.Decrypt(filePath: _pubPrivFilePath, value: _encryptedText + "x"));
 }
 public void Decrypt_withPubOnlyKey()
 {
     RsaHelpers.GenerateRsaKeys(pubOnlyFilePath: _pubOnlyFilePath);
     Assert.Throws <CryptographicException>(() => RsaHelpers.Decrypt(filePath: _pubOnlyFilePath, value: _encryptedText));
 }