C#2005 我正在使用简单的加密和解密来获取IP地址。远程服务器上的应用程序将加密IP地址,客户端将对其进行解密。但是,当客户端解密IP时,我只返回一些IP地址。其余的都是垃圾。 之前:123.456.78.98 之后:fheh& ^ G.78.98
非常感谢,
/// Encrypt the SIP IP address in the remote server
private void encryptSIP_IP(string sip_ip)
{
TripleDESCryptoServiceProvider encrypt = new TripleDESCryptoServiceProvider();
/// Private key
byte[] key = { 0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1, 0};
encrypt.Key = key;
byte[] byteSIP = System.Text.Encoding.Default.GetBytes(sip_ip);
ICryptoTransform encryptor = encrypt.CreateEncryptor();
byte[] encrypted_sip = encryptor.TransformFinalBlock(byteSIP, 0, byteSIP.Length);
/// This will decrypt in the client application
private void decryptSIP_IP(byte[] encrypted_sip)
{
TripleDESCryptoServiceProvider decrypt = new TripleDESCryptoServiceProvider();
/// Private key
byte[] key = { 0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1, 0 };
decrypt.Key = key;
ICryptoTransform decryptor = decrypt.CreateDecryptor();
byte[] original = decryptor.TransformFinalBlock(encrypted_sip, 0, encrypted_sip.Length);
string ip = System.Text.Encoding.Default.GetString(original);
}
}
答案 0 :(得分:8)
除了密钥,您还需要Initialization Vector(8个字节):
// To Encrypt
encrypt.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
// Use same IV to decrypt
decrypt.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
答案 1 :(得分:2)
正如darin所说,你需要设置初始化向量。如果可能,请为每个加密过程选择一个随机的加密过程并以明文形式传输/保存。
顺便说一下,你应该看一下CryptoStream,而不是使用TransformBlock / TransformFinalBlock ...它更干净,imho。
答案 2 :(得分:1)
您是否尝试过双方明确设置编码?也许默认是不同的。