我正在尝试使用ServiceStack的CryptUtils类来使用“静态”键值(在不同时间/会话中使用相同的键)来加密/解密数据。我正在使用的代码如下。公钥和私钥是使用CryptUtils生成的。
问题在于,每次运行我的测试程序时,encryptedAddress都是一个不同的值(每次运行程序时我都期望得到相同的值)。
我做错了什么?
感谢。
class Program {
const string PUBLIC_KEY = "<RSAKeyValue><Modulus>5/tn3eY3KpQUvlJ3u...";
const string PRIVATE_KEY = "<RSAKeyValue><Modulus>5/tn3eY3KpQUvlJ3u...";
static void Main(string[] args) {
RsaKeyPair keyPair = new RsaKeyPair();
keyPair.PrivateKey = PRIVATE_KEY;
keyPair.PublicKey = PUBLIC_KEY;
string address = "1234 Any Street";
string encryptedAddress = CryptUtils.Encrypt(keyPair.PublicKey, address, RsaKeyLengths.Bit2048);
string decryptedAddress = CryptUtils.Decrypt(keyPair.PrivateKey, encryptedAddress, RsaKeyLengths.Bit2048);
}
}
答案 0 :(得分:1)
这是设计加密使用不同的随机填充导致不同的加密值,这减少了它容易受到明文攻击。使用私钥将其解密回原始值非常重要。