示例#1
0
        private static void Decrypt(string[] arguments)
        {
            using (RijndaelManaged myRijndael = new RijndaelManaged())
            {
                Console.WriteLine("Key path:");
                byte[] key = File.ReadAllBytes(EncryptionReadLine());
                Console.WriteLine("IV path ('Enter' for none):");
                byte[] IV = File.ReadAllBytes(EncryptionReadLine());

                Console.WriteLine("\nDecryption finished.");

                // Use key from key path
                myRijndael.Key = key;
                if (IV.Length > 0)
                {
                    myRijndael.IV = IV;
                }
                else
                {
                    myRijndael.GenerateIV();
                }

                string filePath = arguments[0];

                // Get the decrypted byte array from filePath
                byte[] encrypted = File.ReadAllBytes(filePath);

                // Decrypt the bytes to a string.
                string decrypted = Crypher.DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

                if (arguments.Length > 1)
                {
                    string decryptedFilePath = arguments[1];
                    Console.WriteLine($"Decrypted text is in: '{decryptedFilePath}'");
                    File.WriteAllText(decryptedFilePath, decrypted);
                }
                else
                {
                    Console.WriteLine("Decrypted text is:\n\n" + decrypted);
                }
            }
        }
示例#2
0
        private static void Encrypt(string[] arguments)
        {
            /*
             * Create a new instance of the RijndaelManaged
             * class.  This generates a new key and initialization
             * vector (IV).
             */
            using (RijndaelManaged myRijndael = new RijndaelManaged())
            {
                Console.WriteLine("Input key path. (Press 'Enter' to autogenerate)");
                string keyPath = EncryptionReadLine();
                string IVPath  = string.Empty;

                if (keyPath == String.Empty)
                {
                    myRijndael.GenerateKey();
                    myRijndael.GenerateIV();
                }
                else
                {
                    myRijndael.Key = File.ReadAllBytes(keyPath);

                    Console.WriteLine("Input IV path. (Press 'Enter' to autogenerate)");
                    IVPath = EncryptionReadLine();

                    if (IVPath == String.Empty)
                    {
                        myRijndael.GenerateIV();
                    }
                    else
                    {
                        myRijndael.IV = File.ReadAllBytes(IVPath);
                    }
                }

                // Example: encrypt {filePath} {encryptedFilePath}
                // Reads from filePath and writes encrypted output to encryptedFilePath.
                if (arguments.Length > 1)
                {
                    string filePath          = arguments[0];
                    string encrypredFilePath = arguments[1];
                    string text = File.ReadAllText(filePath);

                    // Encrypt the string to an array of bytes.
                    byte[] encrypted = Crypher.EncryptStringToBytes(text, myRijndael.Key, myRijndael.IV);

                    // Create file with the encrypted output.
                    File.WriteAllBytes(encrypredFilePath, encrypted);
                }
                // Example: encrypt
                // Simply prints encrypted input to console.
                else
                {
                    Console.WriteLine("Text to encrypt: ");
                    string text = EncryptionReadLine();

                    byte[] encrypted = Crypher.EncryptStringToBytes(text, myRijndael.Key, myRijndael.IV);

                    Console.WriteLine(Convert.ToBase64String(encrypted));
                }

                Console.WriteLine("\nEncryption finished.");

                // Send key to separate text file.
                if (keyPath == String.Empty)
                {
                    File.WriteAllBytes("key.txt", myRijndael.Key);
                    File.WriteAllBytes("IV.txt", myRijndael.IV);
                    Console.WriteLine("Your key is in 'key.txt'.\nYour IV is in 'IV.txt'.");
                }
                else if (IVPath == String.Empty)
                {
                    File.WriteAllBytes("IV.txt", myRijndael.IV);
                    Console.WriteLine("Your IV is in 'IV.txt'.");
                }
            }
        }