我在加密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