我在加密C#中的东西时遇到了困难。C#DES ECB加密
我有3个变量。 第一个是16位十六进制,让我们把它叫做X值IE 0072701351979990 第二个也是16位十六进制值,让我们把它叫做ŸIE 3008168011FFFFFF
这两个值必须是异或“编辑拿到钥匙用于DES-ECB加密。
因此导致307a66934068666f。现在这是我加密的关键。 然后我有这个作为我的数据块,这是64位加密0E329232EA6D0D73
现在我有下面的代码加密这个。 加密结果应与数据块再次异或,并且结果为64位。不是这种情况。
这是我对加密
$ public static string DESEncrypt(string keyBlock,string dataBlock){
DES desEncrypt = new DESCryptoServiceProvider();
byte[] keyBlockBytes = BitConverter.GetBytes(Convert.ToInt64(keyBlock, 16));
byte[] dataBlockBytes = BitConverter.GetBytes(Convert.ToInt64(dataBlock, 16));
desEncrypt.Mode = CipherMode.ECB;
desEncrypt.Key = keyBlockBytes;
ICryptoTransform transForm = desEncrypt.CreateEncryptor();
MemoryStream enecryptedStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(enecryptedStream, transForm, CryptoStreamMode.Write);
cryptoStream.Write(dataBlockBytes, 0, dataBlockBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] encryptedData = new byte[enecryptedStream.Length];
enecryptedStream.Position = 0;
enecryptedStream.Read(encryptedData, 0, encryptedData.Length);
string enCryptedHex = BitConverter.ToString(encryptedData);
return enCryptedHex.Replace("-","");
}
我在做什么错误的代码?
更新的问题 我已经从CodeInChaos测试了上述解决方案。 它确实给了我一个64位的结果。但仍然有错误。
这是我更新的代码。
关键块值是abababababababab ,数据块值是215135734068666F。
生成的64位结果应该与数据块再次异或。
最终答案是假设是414945DD33C97C47但我得到 288a08c01a57ed3d。
为什么它不出来?
以下是加密供应商文档中的规范。
加密是符合FIPS 46-3的DEA,即ECB模式下的单个DES,使用具有奇数奇偶校验的单个64- 位DES密钥。
$ public static string DESEncrypt(string keyBlock,string dataBlock){
DES desEncrypt = new DESCryptoServiceProvider();
byte[] keyBlockBytes = BitConverter.GetBytes(Convert.ToInt64(keyBlock, 16));
byte[] dataBlockBytes = BitConverter.GetBytes(Convert.ToInt64(dataBlock, 16));
desEncrypt.Mode = CipherMode.ECB;
desEncrypt.Key = keyBlockBytes;
desEncrypt.Padding = PaddingMode.None;
ICryptoTransform transForm = desEncrypt.CreateEncryptor();
MemoryStream enecryptedStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(enecryptedStream, transForm, CryptoStreamMode.Write);
cryptoStream.Write(dataBlockBytes, 0, dataBlockBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] encryptedData = enecryptedStream.ToArray();
string enCryptedHex = BitConverter.ToString(encryptedData);
enCryptedHex = enCryptedHex.Replace("-", "");
long iDeaEncrypt = Convert.ToInt64(enCryptedHex, 16);
long iDataBlock = Convert.ToInt64(dataBlock, 16);
long decoderKey = iDeaEncrypt^iDataBlock;
string decKeyHex = Convert.ToString(decoderKey, 16);
return decKeyHex;
}
”现在我有下面的代码加密这个,加密的结果应该再次与数据块XOR并导致64位结果,但情况并非如此。你是什么意思? – CodesInChaos