DES加密/解密中的C#编码
我的主要方法运行没有错误,但解密的消息不正确。我几乎可以肯定我没有正确编码,但我无法确定问题所在。任何帮助将不胜感激。
这是我的第一篇文章,所以如果我无意中违反了规则或未遵守指南,请告诉我。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题出在倒数第二行吗?
我可能偏离了轨道,但我不认为您打算将字符串字节转换回 base64。您只需要将字节转换回字符串吗?
Is the problem on the second to last line?
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?
看起来问题出在解密中:
我认为您不想对解密的数据执行此操作。解密后的字节已经是 ASCII 码了。
Looks like the problem is in Decrypt:
I don't think you want to do this to the decrypted data. The decrypted bytes are already ASCII.
我用这个解决了这个问题
I fix the problem using just this