我有解密用3DES加密字符串下面的代碼片段:解密密文TripleDes的靜態密鑰和IV通過的OpenSSL工具
private static byte[] KEY_192 = new byte[]
{
37, 19, 88, 164, 71, 3, 227, 30, 19,
174, 45, 84, 23, 253, 149, 108, 12,
107, 16, 192, 98, 22, 179, 200
};
private static byte[] IV_192 = new byte[]
{
47, 108, 239, 71, 33, 98, 177, 13,
36, 51, 69, 88, 189, 17, 210, 14,
174, 230, 20, 60, 174, 100, 12, 22
};
public static string DecryptTripleDES(string value)
{
if (!string.IsNullOrEmpty(value))
{
System.Security.Cryptography.TripleDESCryptoServiceProvider cryptoProvider = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
byte[] buffer = System.Convert.FromBase64String(value);
System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);
System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, cryptoProvider.CreateDecryptor(DataEncryptionEngine.KEY_192, DataEncryptionEngine.IV_192), System.Security.Cryptography.CryptoStreamMode.Read);
System.IO.StreamReader sr = new System.IO.StreamReader(cs);
return sr.ReadToEnd();
}
return null;
}
我試圖使用OpenSSL或Ruby,但沒有複製它運氣,加密base64編碼字符串的一個例子是:
JDiLOoP3iIY=
當在Linux下嘗試此操作時出現以下錯誤。
$ echo JDiLOoP3iIY = | openssl enc -d -des3 -a -K 371988164713227301917445842325314910812107161929822179200 -iv 471082397133981771336516988189172101417423020601741001222;十六進制字符串 太長無效十六進制值
我錯過了什麼?謝謝!
編輯:如果有幫助,這是加密的字符串函數:
public static string EncryptTripleDES(string value)
{
if (!string.IsNullOrEmpty(value))
{
System.Security.Cryptography.TripleDESCryptoServiceProvider cryptoProvider = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, cryptoProvider.CreateEncryptor(DataEncryptionEngine.KEY_192, DataEncryptionEngine.IV_192), System.Security.Cryptography.CryptoStreamMode.Write);
System.IO.StreamWriter sw = new System.IO.StreamWriter(cs);
sw.Write(value);
sw.Flush();
cs.FlushFinalBlock();
ms.Flush();
return System.Convert.ToBase64String(ms.GetBuffer(), 0, System.Convert.ToInt32(ms.Length));
}
return null;
}
想這對紅寶石也一樣,我得到 '壞解密'
#!/usr/bin/env ruby
require 'openssl'
require 'base64'
string = 'JDiLOoP3iIY='
def decrypt(cpass)
cipher = OpenSSL::Cipher::Cipher.new("des-ede-cbc")
cipher.decrypt
cipher.key = "251358A44703E31E13AE2D5417FD956C0C6B10C06216B3C8"
cipher.iv = "2F6CEF472162B10D24334558BD11D20EAEE6143CAE640C16"
return cipher.update(Base64.decode64(cpass)) + cipher.final
end
decrypted = decrypt(string)
puts "decrypted string: #{decrypted}"
3DES需要64位的IV,不192位的IV。 – CodesInChaos
我知道,但這是怎麼在這個代碼中使用... – bsteo