gpt4 book ai didi

c# - 解码和编码字符串 - HardCoded KEY for Symmetric Algorithms

转载 作者:太空宇宙 更新时间:2023-11-03 20:32:35 24 4
gpt4 key购买 nike

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

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;
}


}
}

和如下用法:

protected void Page_Load(object sender, EventArgs e)
{
SymmetricEncryptionUtility.AlgorithmName = "TripleDES";
Response.Write(SymmetricEncryptionUtility.EncryptData("1234-4567-8910-2345"));
}

我有一些关于 MyKey 的问题 -> 我们如何为对称算法硬编码 key 并在高等类(class)中使用它?

上面的代码ERROR如下:

    Server Error in '/' Application.

    Specified key is not a valid size for this algorithm. 
Description: An unhandled exception occurred during the

execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details:

System.Security.Cryptography.CryptographicException: Specified key is not a valid size for this algorithm.

我该如何解决这个错误?

提前致谢

最佳答案

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

var helper = new Rfc2898DeriveBytes(password, salt);
algorithm.Key = helper.GetBytes(algorithm.KeySize / 8);

有关 Rfc2898DeriveBytes 及其使用方法的更多信息,请查看其 page on MSDN .

关于c# - 解码和编码字符串 - HardCoded KEY for Symmetric Algorithms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6725661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com