非常简单的加密用C#和SymmetricAlgorithm [英] Really simple encryption with C# and SymmetricAlgorithm

查看:667
本文介绍了非常简单的加密用C#和SymmetricAlgorithm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个非常简单的地穴/解密方法。我会用始终不变的静态密钥。我知道这种做法的风险。目前我使用下面的代码,但它不会产生crypting和decripting相同的字符串后,相同的结果(没有在字符串中间的一些垃圾)。

I'm looking for a very simple crypt / decrypt method. I will be using always the same static key. I'm aware of the risks of this approach. Currently I'm using the following code but it does not generate the same result after crypting and decripting the same string (there is some garbage in the middle of the string).

    public static string Crypt(this string text)
    {
        string result = null;

        if (!String.IsNullOrEmpty(text))
        {
            byte[] plaintextBytes = Encoding.Unicode.GetBytes(text);

            SymmetricAlgorithm symmetricAlgorithm = DES.Create();
            symmetricAlgorithm.Key = new byte[8] {1, 2, 3, 4, 5, 6, 7, 8};
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, symmetricAlgorithm.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cryptoStream.Write(plaintextBytes, 0, plaintextBytes.Length);
                }

                result = Encoding.Unicode.GetString(memoryStream.ToArray());
            }
        }

        return result;
    }

    public static string Decrypt(this string text)
    {
        string result = null;

        if (!String.IsNullOrEmpty(text))
        {
            byte[] encryptedBytes = Encoding.Unicode.GetBytes(text);

            SymmetricAlgorithm symmetricAlgorithm = DES.Create();
            symmetricAlgorithm.Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            using (MemoryStream memoryStream = new MemoryStream(encryptedBytes))
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, symmetricAlgorithm.CreateDecryptor(), CryptoStreamMode.Read))
                {
                    byte[] decryptedBytes = new byte[encryptedBytes.Length];
                    cryptoStream.Read(decryptedBytes, 0, decryptedBytes.Length);
                    result = Encoding.Unicode.GetString(decryptedBytes);
                }
            }
        }

        return result;
    }



我可以改变什么是必要的,没有限制(但我只想有在方法隐窝和另外一个没有它们之间共享变量)来解密。

I can change whatever is needed, no limits (but I want just to have on method to crypt and another one to decrypt without sharing variables between them).

感谢。

推荐答案

如果你不想来处理自己的钥匙,然后让操作系统也为了您。例如。使用 Windows数据保护(DPAPI)。

If you don't want to handle keys yourself then let the operating system do it for your. E.g. use Windows Data Protection (DPAPI).

您可以编写自己的,字符串为主,的 System.Security.Cryptography.ProtectedData.Protect 和的 撤消 通过使用类似的方法:

You can write your own, string-based, version of System.Security.Cryptography.ProtectedData.Protect and Unprotect methods by using something like:

public static string Crypt (this string text)
{
    return Convert.ToBase64String (
        ProtectedData.Protect (
            Encoding.Unicode.GetBytes (text) ) );
}

public static string Derypt (this string text)
{
    return Encoding.Unicode.GetString (
        ProtectedData.Unprotect (
             Convert.FromBase64String (text) ) );
}

这篇关于非常简单的加密用C#和SymmetricAlgorithm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆