C#DES加密尾随号码不正确
C# DES Encryption Incorrect on Trailing number
des 加密时出现不匹配,仅当原始文本尾随 1,2,3,4 时才会发生。
我在这里写了一个 fiddler 来演示这个问题:https://dotnetfiddle.net/7u0Hzr
如果您不想被重定向,请在此处附加 fiddler 代码:
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
public class Program
{
public static void Main()
{
var key = "abcd1234";
var salt = "4321";
var encrypted = "";
var decrypted = "";
var alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
for (var i = 0; i < 100; i++)
{
var text = $"{Math.Floor((double)i / 10)}{i % 10}";
encrypted = DesEncrypt(text, key, salt);
decrypted = DesDecrypt(encrypted, key, salt);
Console.WriteLine($"Text: {text} | Encrypted: {encrypted} | Decrypted: {decrypted}");
}
}
private static string DesEncrypt(string plaintText, string strKey, string salt)
{
byte[] key = { }; //Encryption Key
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray;
try
{
key = Encoding.UTF8.GetBytes(strKey);
// DESCryptoServiceProvider is a cryptography class defind in c#.
DESCryptoServiceProvider ObjDES = new DESCryptoServiceProvider();
inputByteArray = Encoding.UTF8.GetBytes(plaintText + salt);
MemoryStream Objmst = new MemoryStream();
CryptoStream Objcs = new CryptoStream(Objmst, ObjDES.CreateEncryptor(key, IV), CryptoStreamMode.Write);
Objcs.Write(inputByteArray, 0, inputByteArray.Length);
Objcs.FlushFinalBlock();
return Convert.ToBase64String(Objmst.ToArray());//encrypted string
}
catch (System.Exception ex)
{
throw ex;
}
}
private static string DesDecrypt(string cipherText, string strKey, string salt)
{
byte[] key = { };// Key
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray = new byte[cipherText.Length];
try
{
key = Encoding.UTF8.GetBytes(strKey);
DESCryptoServiceProvider ObjDES = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(cipherText);
MemoryStream Objmst = new MemoryStream();
CryptoStream Objcs = new CryptoStream(Objmst, ObjDES.CreateDecryptor(key, IV), CryptoStreamMode.Write);
Objcs.Write(inputByteArray, 0, inputByteArray.Length);
Objcs.FlushFinalBlock();
Encoding encoding = Encoding.UTF8;
return encoding.GetString(Objmst.ToArray()).TrimEnd(salt.ToCharArray());
}
catch (System.Exception ex)
{
if (ex is FormatException) Console.WriteLine("The token is in the wrong format");
if (ex is System.Security.Cryptography.CryptographicException) Console.WriteLine("Invalid token");
throw ex;
}
}
}
Mis-match happens on des encryption, only happens when the raw text
has trailing 1,2,3,4.
mhhh...我怀疑它发生在加密中...
仔细查看您的解密显示:
return encoding.GetString(Objmst.ToArray()).TrimEnd(salt.ToCharArray());
换句话说:
-您从 MemoryStream
中获取数组
- 用它做一个字符串
-最后从末尾删除所有盐字符实例
(恰好是你遗漏的字符)
des 加密时出现不匹配,仅当原始文本尾随 1,2,3,4 时才会发生。
我在这里写了一个 fiddler 来演示这个问题:https://dotnetfiddle.net/7u0Hzr
如果您不想被重定向,请在此处附加 fiddler 代码:
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
public class Program
{
public static void Main()
{
var key = "abcd1234";
var salt = "4321";
var encrypted = "";
var decrypted = "";
var alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
for (var i = 0; i < 100; i++)
{
var text = $"{Math.Floor((double)i / 10)}{i % 10}";
encrypted = DesEncrypt(text, key, salt);
decrypted = DesDecrypt(encrypted, key, salt);
Console.WriteLine($"Text: {text} | Encrypted: {encrypted} | Decrypted: {decrypted}");
}
}
private static string DesEncrypt(string plaintText, string strKey, string salt)
{
byte[] key = { }; //Encryption Key
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray;
try
{
key = Encoding.UTF8.GetBytes(strKey);
// DESCryptoServiceProvider is a cryptography class defind in c#.
DESCryptoServiceProvider ObjDES = new DESCryptoServiceProvider();
inputByteArray = Encoding.UTF8.GetBytes(plaintText + salt);
MemoryStream Objmst = new MemoryStream();
CryptoStream Objcs = new CryptoStream(Objmst, ObjDES.CreateEncryptor(key, IV), CryptoStreamMode.Write);
Objcs.Write(inputByteArray, 0, inputByteArray.Length);
Objcs.FlushFinalBlock();
return Convert.ToBase64String(Objmst.ToArray());//encrypted string
}
catch (System.Exception ex)
{
throw ex;
}
}
private static string DesDecrypt(string cipherText, string strKey, string salt)
{
byte[] key = { };// Key
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray = new byte[cipherText.Length];
try
{
key = Encoding.UTF8.GetBytes(strKey);
DESCryptoServiceProvider ObjDES = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(cipherText);
MemoryStream Objmst = new MemoryStream();
CryptoStream Objcs = new CryptoStream(Objmst, ObjDES.CreateDecryptor(key, IV), CryptoStreamMode.Write);
Objcs.Write(inputByteArray, 0, inputByteArray.Length);
Objcs.FlushFinalBlock();
Encoding encoding = Encoding.UTF8;
return encoding.GetString(Objmst.ToArray()).TrimEnd(salt.ToCharArray());
}
catch (System.Exception ex)
{
if (ex is FormatException) Console.WriteLine("The token is in the wrong format");
if (ex is System.Security.Cryptography.CryptographicException) Console.WriteLine("Invalid token");
throw ex;
}
}
}
Mis-match happens on des encryption, only happens when the raw text has trailing 1,2,3,4.
mhhh...我怀疑它发生在加密中...
仔细查看您的解密显示:
return encoding.GetString(Objmst.ToArray()).TrimEnd(salt.ToCharArray());
换句话说:
-您从 MemoryStream
中获取数组
- 用它做一个字符串
-最后从末尾删除所有盐字符实例
(恰好是你遗漏的字符)