简体   繁体   English

加密和解密给定密钥的字符串

[英]encrypt and decrypt string given key

I inherited the code below.我继承了下面的代码。 Unfortunately, the decrypted value of hello_world is not:不幸的是,hello_world 的解密值不是:

hello world

but (in my case):但是(就我而言):

&�|ktR���ڼ��S����%��< ���8�

Any ideas?有任何想法吗? It also appears that the result is different every time, which is kind of obvious given the code.似乎每次的结果都不同,这在代码中很明显。 Could I change this so that I can send the data encryted once and then decrypt in the future again?我可以更改此设置,以便我可以发送加密一次的数据,然后在将来再次解密吗? Thanks!谢谢!

Code:代码:

using System;
using System.IO;
using System.Security.Cryptography;

namespace crypt
{
    class Program
    {
        static void Main(string[] args)
        {
            var key = @"abcdefghijklmnopqrstuvw==";

            using (var aesAlg = Aes.Create())
            {
                aesAlg.Mode = CipherMode.CBC;
                aesAlg.Padding = PaddingMode.PKCS7;
                aesAlg.Key = Convert.FromBase64String(key);
                aesAlg.GenerateIV();
                var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                var enc_hello_world = EncryptProperty(encryptor, "hello world");

                var hello_world = DecryptProperty(encryptor, enc_hello_world);
            }

        }
        private static string EncryptProperty(ICryptoTransform encryptor, string valueToEncrypt)
        {
            byte[] encrypted;
            using (var msEncrypt = new MemoryStream())
            {
                using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (var swEncrypt = new StreamWriter(csEncrypt))
                    {
                        swEncrypt.Write(valueToEncrypt);
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
            return Convert.ToBase64String(encrypted);
        }

        private static string DecryptProperty(ICryptoTransform decryptor, string valueToDecrypt)
        {
            string decrypted;

            using (var msDecrypt = new MemoryStream(Convert.FromBase64String(valueToDecrypt)))
            {
                using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (var srDecrypt = new StreamReader(csDecrypt))
                    {
                        decrypted = srDecrypt.ReadToEnd();
                    }
                }
            }
            return decrypted;
        }
    }
}

AES-CBC requires two variables to encode and decode data: key and IV (initialization vector). AES-CBC 需要两个变量来编码和解码数据:密钥和 IV(初始化向量)。 Initialization vector can be sent in plaintext and it doesn't result in worse security of your encryption.初始化向量可以以明文形式发送,并且不会导致加密的安全性变差。

aesAlg.GenerateIV();

This is where your IV gets created, you need to store this (I do this by prepending that to the resulting data) and then access it and set the IV when decrypting.这是创建您的 IV 的地方,您需要存储它(我通过将其添加到结果数据中来做到这一点),然后访问它并在解密时设置 IV。

You could also use an empty IV but this will make it easier for attackers to expose your key, so this is not recommended.您也可以使用空的 IV,但这会使攻击者更容易暴露您的密钥,因此不建议这样做。

Here is a good example of AES in C#: https://gist.github.com/mark-adams/87aa34da3a5ed48ed0c7 (it seems to be using the method I've mentioned).这是 C# 中 AES 的一个很好的例子: https : //gist.github.com/mark-adams/87aa34da3a5ed48ed0c7 (它似乎使用了我提到的方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM