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

发布于 2024-10-01 01:36:41 字数 3139 浏览 2 评论 0原文

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

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

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

My main method runs without errors, but the decrypted message is not correct. I'm almost certain I'm not properly encoding, but I can't nail down the problem. Any help would be greatly appreciated.

This is my first post, so if I've inadvertently broken a rule or not adhered to a guideline, please let me know.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世界等同你 2024-10-08 01:36:41

问题出在倒数第二行吗?

string message = Convert.ToBase64String(decryptedMessageBytes);

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

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

Is the problem on the second to last line?

string message = Convert.ToBase64String(decryptedMessageBytes);

I may be off track here but I don't think you intended to convert the string bytes back to base64. Do you just need to convert the bytes back to a string?

string message = ASCIIEncoding.ASCII.FromBytes(decryptedMessageBytes);
攒一口袋星星 2024-10-08 01:36:41

看起来问题出在解密中:

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

我认为您不想对解密的数据执行此操作。解密后的字节已经是 ASCII 码了。

Looks like the problem is in Decrypt:

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

I don't think you want to do this to the decrypted data. The decrypted bytes are already ASCII.

浪菊怪哟 2024-10-08 01:36:41

我用这个解决了这个问题

//Encode deencrypted binary data to base64 string
string message = ASCIIEncoding.ASCII.GetString(decryptedMessageBytes);

I fix the problem using just this

//Encode deencrypted binary data to base64 string
string message = ASCIIEncoding.ASCII.GetString(decryptedMessageBytes);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文