2012-05-25 137 views
-1

我在加密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; 
    } 
+0

」現在我有下面的代碼加密這個,加密的結果應該再次與數據塊XOR並導致64位結果,但情況並非如此。你是什​​麼意思? – CodesInChaos

回答

0

我認爲你需要填充設置爲PaddingMode.None

desEncrypt.Padding = PaddingMode.None; 

但你確實應該認真思考,如果DES和歐洲央行真的是你想要的。


b.t.w.

byte[] encryptedData = new byte[enecryptedStream.Length]; 
encryptedStream.Position = 0; 
encryptedStream.Read(encryptedData, 0, encryptedData.Length); 

可以被代替:

encryptedData = encryptedStream.ToArray(); 
+0

嘿!非常感謝你。這工作100%。加密結果現在是一個64位的結果。它不是我決定使用DES和ECB。這是我生成使用這種加密的東西的硬件。但只限於一些舊的硬件。新的不使用這個。感謝您的BTW。 – MariusvStraaten

+0

當我測試了所有內容並與指南結果值進行比較後,它不匹配。 這些是他們給我的測試值。數據塊是215135734068666F,密鑰塊是ababababababab。 然後生成的加密與原始數據塊XOR。 我想要得到的最終結果是414945DD33C97C47。但我得到288a08c01a57ed3d – MariusvStraaten

0

也許是必要設置DES提供商使用FIPS 46-3標準使得DEA使用在FIPS指定的置換表等等46- 3。不幸的是,我也在爲這個問題而努力。 「