C#AES Commandline程序

时间:2016-10-04 00:04:01

标签: c# encryption cryptography aes

我正在进行AES加密和解密,它是从命令行执行的。 它应该像openSSL一样工作,但使用C#代码 因此,如果您想调用该程序,它应该如下所示:

AesU -e [d] -k key< -i initial_vector> -m CBC -in input_file -out output_file
AesU:是程序的名称
-e / -d:加密或解密
-k key:键将被键替换(支持的长度应为128,192,256位)
-i:是可选的 -m:加密/解密模式(ECB,CBC,CFB,OFB和CTR)
-in / -out:加密文本/解密文本的inputfile和outputfile

到目前为止,这是我的代码,它有点但不是很有效,我不知道。当我试图加密时,我开始时会收到很多“0”。当我再次解密它时,除了最后几个正确解密的字母外,一切都是神秘的

 class Program
{
    static void Main(string[] args)
    {

        // checking for encryption and decryption
        switch (args[0]){
            case "-e":
                msg = ByteArrayToString(encryption(msg, key, iv, encryptionType));
                break;
            case "-d":
                msg = decryption(msg, key, iv, encryptionType);
                break;
            default:
                Console.WriteLine("ERROR: Please enter -e or -d for encryption or decryption nothing else (Check the Syntax in the README)");
                return;
        }
        writing(outputPath, msg);
    }

    static byte[] encryption(string plaintext, byte[] key, string initialVector, string mode)
    {
        byte[] encrypted;
        byte[] iv = StringToByteArray(initialVector);
        string helpStringCTR ="";

        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Key = key;
            switch (mode)
            {
                case "ECB":
                    aesAlg.Mode = CipherMode.ECB;
                    aesAlg.Padding = PaddingMode.PKCS7;
                    break;
                case "CBC":
                    aesAlg.Mode = CipherMode.CBC;
                    //aesAlg.Padding = PaddingMode.PKCS7;
                    break;
                case "CFB":
                    aesAlg.Mode = CipherMode.CFB;
                    aesAlg.Padding = PaddingMode.PKCS7;
                    break;
                case "OFB":
                    aesAlg.Mode = CipherMode.OFB;
                    aesAlg.Padding = PaddingMode.PKCS7;
                    break;
                // Needs ECB + Counter
                case "CTR":
                    aesAlg.Mode = CipherMode.ECB;
                    aesAlg.Padding = PaddingMode.PKCS7;
                    helpStringCTR = plaintext;
                    plaintext = initialVector;
                    break;
                default:
                    break;
            }

            var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
            // Create the streams used for encryption. 
            using (var msEncrypt = new MemoryStream())
            {
                using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (var swEncrypt = new StreamWriter(csEncrypt))
                    {
                        //Write all data to the stream.
                        swEncrypt.Write(plaintext);
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
        }

        var combinedIvCt = new byte[iv.Length + encrypted.Length];
        Array.Copy(iv, 0, combinedIvCt, 0, iv.Length);
        Array.Copy(encrypted, 0, combinedIvCt, iv.Length, encrypted.Length);

        // bitwise XOR of the encrypted IV with the plaintext (only for CTR)
        if (mode == "CTR")
        {
            for (int i = 0; i < initialVector.Length; i++)
            {
                combinedIvCt[i] = (byte)(combinedIvCt[i] ^ helpStringCTR[i]);
            }
        }

        // Return the encrypted bytes from the memory stream. 
        return combinedIvCt;
    }

    static string decryption(string ciphertext, byte[] key, string initialVector, string mode)
    {
        //placeholder for decrypted text
        string plaintext;
        byte[] ciphertextarray = StringToByteArray(ciphertext);
        string helpStringCTR = "";

        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Key = key;
            byte[] IV = new byte[aesAlg.BlockSize / 8];
            byte[] cipherText = new byte[ciphertextarray.Length - IV.Length];
            Array.Copy(ciphertextarray, IV, IV.Length);
            Array.Copy(ciphertextarray, IV.Length, cipherText, 0, cipherText.Length);

            aesAlg.IV = IV;
            switch (mode)
            {
                case "ECB":
                    aesAlg.Mode = CipherMode.ECB;
                    aesAlg.Padding = PaddingMode.PKCS7;
                    break;
                case "CBC":
                    aesAlg.Mode = CipherMode.CBC;
                    aesAlg.Padding = PaddingMode.PKCS7;
                    break;
                case "CFB":
                    aesAlg.Mode = CipherMode.CFB;
                    aesAlg.Padding = PaddingMode.PKCS7;
                    break;
                case "OFB":
                    aesAlg.Mode = CipherMode.OFB;
                    aesAlg.Padding = PaddingMode.PKCS7;
                    break;
                // Needs ECB + Counter
                case "CTR":
                    aesAlg.Mode = CipherMode.ECB;
                    aesAlg.Padding = PaddingMode.PKCS7;
                    helpStringCTR = ciphertext;
                    ciphertext = initialVector;
                    break;
                default:
                    break;
            }
            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for decryption. 
            using (var msDecrypt = new MemoryStream(cipherText))
            {
                using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (var srDecrypt = new StreamReader(csDecrypt))
                    {

                        // Read the decrypted bytes from the decrypting stream
                        // and place them in a string.
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }

        }
        // bitwise XOR of the decrypted IV with the cyphertext (only for CTR)
        if (mode == "CTR")
        {
            for (int i = 0; i < initialVector.Length; i++)
            {
                byte[] help = StringToByteArray(plaintext);
                help[i] = (byte)(help[i] ^ helpStringCTR[i]);
                plaintext = ByteArrayToString(help);
            }
        }

        return plaintext;

    }

0 个答案:

没有答案