gpt4 book ai didi

DES加密/解密中的C#编码

转载 作者:太空狗 更新时间:2023-10-30 01:10:42 24 4
gpt4 key购买 nike

我的主要方法运行没有错误,但解密的消息不正确。我几乎可以肯定我没有正确编码,但我无法确定问题所在。任何帮助将不胜感激。

这是我的第一篇文章,所以如果我无意中违反了规则或没有遵守准则,请告诉我。

static void Main(string[] args)
{
string unencryptedString = "cat";
string encryptedString;
string decryptedString;

string password = "password";

System.Console.WriteLine("Unencrypted String: " + unencryptedString);
System.Console.WriteLine("Password: " + password);

encryptedString = StandardEncryptor.Encrypt(unencryptedString, password);
System.Console.WriteLine("Encrypted String: " + encryptedString);

decryptedString = StandardEncryptor.Decrypt(encryptedString, password);
System.Console.WriteLine("Decrypted String: " + decryptedString);

System.Console.ReadLine();
}

public static string Encrypt(string message, string password)
{
// Encode message and password
byte[] messageBytes = ASCIIEncoding.ASCII.GetBytes(message);
byte[] passwordBytes = ASCIIEncoding.ASCII.GetBytes(password);

// Set encryption settings -- Use password for both key and init. vector
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
ICryptoTransform transform = provider.CreateEncryptor(passwordBytes, passwordBytes);
CryptoStreamMode mode = CryptoStreamMode.Write;

// Set up streams and encrypt
MemoryStream memStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memStream, transform, mode);
cryptoStream.Write(messageBytes, 0, messageBytes.Length);
cryptoStream.FlushFinalBlock();

// Read the encrypted message from the memory stream
byte[] encryptedMessageBytes = new byte[memStream.Length];
memStream.Position = 0;
memStream.Read(encryptedMessageBytes, 0, encryptedMessageBytes.Length);

// Encode the encrypted message as base64 string
string encryptedMessage = Convert.ToBase64String(encryptedMessageBytes);

return encryptedMessage;
}

public static string Decrypt(string encryptedMessage, string password)
{
// Convert encrypted message and password to bytes
byte[] encryptedMessageBytes = Convert.FromBase64String(encryptedMessage);
byte[] passwordBytes = ASCIIEncoding.ASCII.GetBytes(password);

// Set encryption settings -- Use password for both key and init. vector
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
ICryptoTransform transform = provider.CreateDecryptor(passwordBytes, passwordBytes);
CryptoStreamMode mode = CryptoStreamMode.Write;

// Set up streams and decrypt
MemoryStream memStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memStream, transform, mode);
cryptoStream.Write(encryptedMessageBytes, 0, encryptedMessageBytes.Length);
cryptoStream.FlushFinalBlock();

// Read decrypted message from memory stream
byte[] decryptedMessageBytes = new byte[memStream.Length];
memStream.Position = 0;
memStream.Read(decryptedMessageBytes, 0, decryptedMessageBytes.Length);

// Encode deencrypted binary data to base64 string
string message = Convert.ToBase64String(decryptedMessageBytes);

return message;
}

最佳答案

问题是在倒数第二行吗?

string message = Convert.ToBase64String(decryptedMessageBytes);

我在这里可能偏离了轨道,但我不认为您打算将字符串字节转换回 base64。您只需要将字节转换回字符串吗?

string message = ASCIIEncoding.ASCII.FromBytes(decryptedMessageBytes);

关于DES加密/解密中的C#编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4100996/

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