Example #1
0
        public void Crypto_SymmetricKey()
        {
            SymmetricKey key = Crypto.GenerateSymmetricKey(CryptoAlgorithm.AES, 256);
            SymmetricKey copy;
            string       inputString;

            byte[] inputBytes;

            copy = new SymmetricKey(key.ToString());
            Assert.AreEqual(key.Algorithm, copy.Algorithm);
            CollectionAssert.AreEqual(key.Key, copy.Key);
            CollectionAssert.AreEqual(key.IV, copy.IV);

            inputString = "Hello World!";
            Assert.AreEqual(inputString, Crypto.DecryptString(Crypto.EncryptString(inputString, key), key));
            Assert.AreEqual(inputString, Crypto.DecryptStringWithSalt4(Crypto.EncryptStringWithSalt4(inputString, key), key));
            Assert.AreEqual(inputString, Crypto.DecryptStringWithSalt8(Crypto.EncryptStringWithSalt8(inputString, key), key));

            inputBytes = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            CollectionAssert.AreEqual(inputBytes, Crypto.Decrypt(Crypto.Encrypt(inputBytes, key), key));
            CollectionAssert.AreEqual(inputBytes, Crypto.DecryptWithSalt4(Crypto.EncryptWithSalt4(inputBytes, key), key));
            CollectionAssert.AreEqual(inputBytes, Crypto.DecryptWithSalt8(Crypto.EncryptWithSalt8(inputBytes, key), key));

            key = new SymmetricKey("plaintext");
            Assert.AreEqual(CryptoAlgorithm.PlainText, key.Algorithm);
            CollectionAssert.AreEqual(new byte[0], key.Key);
            CollectionAssert.AreEqual(new byte[0], key.IV);
            Assert.AreEqual("PLAINTEXT", key.ToString());
        }
Example #2
0
        private static int GenKey(string algorithm, string sKeySize)
        {
            int keySize;

            if (!int.TryParse(sKeySize, out keySize) || keySize <= 0)
            {
                Program.Output("key size must be >= 0");
                return(1);
            }

            switch (algorithm.ToUpper())
            {
            case CryptoAlgorithm.RC2:
            case CryptoAlgorithm.DES:
            case CryptoAlgorithm.TripleDES:
            case CryptoAlgorithm.AES:

                SymmetricKey key = Crypto.GenerateSymmetricKey(algorithm, keySize);

                Program.Output("");
                Program.Output("SymKey {0}\r\n", key.ToString());
                Program.Output("KEY:   {0}\r\n", Helper.ToHex(key.Key));
                Program.Output("IV:    {0}\r\n", Helper.ToHex(key.IV));
                return(0);

            case CryptoAlgorithm.RSA:

                string privateKey;
                string publicKey;

                privateKey = AsymmetricCrypto.CreatePrivateKey(algorithm, keySize);
                publicKey  = AsymmetricCrypto.GetPublicKey(algorithm, privateKey);

                Program.Output("");
                Program.Output("PRIVATE KEY :\r\n\r\n{0}\r\n", privateKey);
                Program.Output("PUBLIC KEY:\r\n\r\n{0}\r\n", publicKey);

                return(0);

            default:

                Program.Error("[{0}] is not a supported encryption algorithm.", algorithm);
                return(1);
            }
        }