首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >译码和编码字符串.对称算法的HardCoded密钥

译码和编码字符串.对称算法的HardCoded密钥
EN

Stack Overflow用户
提问于 2011-07-17 17:53:55
回答 2查看 6.3K关注 0票数 1

我编写了下面的类,用于编码和解码字符串数据(使用一个键的对称算法):

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace MyProject.Classes
{
    public static class SymmetricEncryption
    {
        private const string MyKey = "bla bla bla";

        private static string _AlgorithmName;
        public static string AlgorithmName
        {
            get { return _AlgorithmName; }
            set { _AlgorithmName = value; }
        }

        public static string EncryptData(string ClearData)
        {
            // Convert string ClearData to byte array
            byte[] ClearData_byte_Array = Encoding.UTF8.GetBytes(ClearData);

            // Now Create The Algorithm
            SymmetricAlgorithm Algorithm = SymmetricAlgorithm.Create(AlgorithmName);
            Algorithm.Key = Encoding.UTF8.GetBytes(MyKey);

            // Encrypt information
            MemoryStream Target = new MemoryStream();

            // Append IV
            Algorithm.GenerateIV();
            Target.Write(Algorithm.IV, 0, Algorithm.IV.Length);

            // Encrypt Clear Data
            CryptoStream cs = new CryptoStream(Target, Algorithm.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(ClearData_byte_Array, 0, ClearData_byte_Array.Length);
            cs.FlushFinalBlock();

            // Output
            byte[] Target_byte_Array = Target.ToArray();
            string Target_string = Convert.ToBase64String(Target_byte_Array);
            return Target_string;
        }

        public static string DecryptData(string EncryptedData)
        {
            byte[] EncryptedData_byte_Array = Convert.FromBase64String(EncryptedData);

            // Now Create The Algorithm
            SymmetricAlgorithm Algorithm = SymmetricAlgorithm.Create(AlgorithmName);
            Algorithm.Key = Encoding.UTF8.GetBytes(MyKey);

            // Decrypt information
            MemoryStream Target = new MemoryStream();

            // Read IV
            int ReadPos = 0;
            byte[] IV = new byte[Algorithm.IV.Length];
            Array.Copy(EncryptedData_byte_Array, IV, IV.Length);
            Algorithm.IV = IV;
            ReadPos += Algorithm.IV.Length;

            // Decrypt Encrypted Data
            CryptoStream cs = new CryptoStream(Target, Algorithm.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(EncryptedData_byte_Array, ReadPos, EncryptedData_byte_Array.Length - ReadPos);
            cs.FlushFinalBlock();

            // Output
            byte[] Target_byte_Array = Target.ToArray();
            string Target_string = Encoding.UTF8.GetString(Target_byte_Array);
            return Target_string;
        }


    }
}

用法如下:

代码语言:javascript
复制
protected void Page_Load(object sender, EventArgs e)
{
    SymmetricEncryptionUtility.AlgorithmName = "TripleDES";
    Response.Write(SymmetricEncryptionUtility.EncryptData("1234-4567-8910-2345"));
}

我有一些关于MyKey -> 的问题,我们如何才能为对称算法提供硬编码密钥并在上层类中使用呢?

上面的代码错误如下:

Server Error in '/' Application.

代码语言:javascript
复制
 Specified key is not a valid size for this algorithm.      Description: An unhandled exception occurred during the

执行当前web请求。请查看堆栈跟踪以获得有关错误的更多信息,以及它起源于代码的位置。

代码语言:javascript
复制
 Exception Details:

System.Security.Cryptography.CryptographicException:指定的密钥不是此算法的有效大小。

如何纠正此错误?

提前感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-17 18:04:28

您可以使用System.Security.Cryptography.Rfc2898DeriveBytes安全地根据string密码和byte[] salt为您的密钥生成正确的字节数:

代码语言:javascript
复制
var helper = new Rfc2898DeriveBytes(password, salt);
algorithm.Key = helper.GetBytes(algorithm.KeySize / 8);

有关Rfc2898DeriveBytes和如何使用它的更多信息,请查看其MSDN上的页面

票数 4
EN

Stack Overflow用户

发布于 2011-07-17 18:04:10

阅读错误并查看TripleDES.Key文档

该算法支持从128位到192位的64位递增密钥长度。

这意味着,例如

代码语言:javascript
复制
private const string MyKey = "bla bla bla blah";

会起作用的。

您没有问过这个问题,但我不确定如何创建这个类作为静态类是个好主意。如果您在代码中的两个不同位置使用它,可能会导致意外的结果,因为AlgorithmName是静态的。

另外,我不认为有一个常数键而是可变的算法是没有意义的,特别是因为不同的算法需要不同长度的密钥。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6725661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档