我已经尝试了通过网络找到的所有内容,但我无法弄清楚问题是什么。我有这些加密/解密字符串的方法:
public static class SecurityUtils {
public static readonly string EncryptionKey = "my.key." + Guid.NewGuid();
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Assert, Unrestricted = true)]
public static string Encrypt(this string stringToEncrypt, string key) {
if (string.IsNullOrEmpty(stringToEncrypt)) {
throw new ArgumentException("An empty string value cannot be encrypted.");
}
if (string.IsNullOrEmpty(key)) {
throw new ArgumentException("Cannot encrypt using an empty key. Please supply an encryption key.");
}
var cspp = new CspParameters { KeyContainerName = key, Flags = CspProviderFlags.UseMachineKeyStore };
var rsa = new RSACryptoServiceProvider(cspp) { PersistKeyInCsp = true };
byte[] bytes = rsa.Encrypt(System.Text.Encoding.UTF8.GetBytes(stringToEncrypt), true);
return BitConverter.ToString(bytes);
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Assert, Unrestricted = true)]
public static string Decrypt(this string stringToDecrypt, string key) {
if (string.IsNullOrEmpty(stringToDecrypt)) {
throw new ArgumentException("An empty string value cannot be encrypted.");
}
if (string.IsNullOrEmpty(key)) {
throw new ArgumentException("Cannot decrypt using an empty key. Please supply a decryption key.");
}
var cspp = new CspParameters { KeyContainerName = key, Flags = CspProviderFlags.UseMachineKeyStore };
var rsa = new RSACryptoServiceProvider(cspp) { PersistKeyInCsp = true };
var decryptArray = stringToDecrypt.Split(new[] { "-" }, StringSplitOptions.None);
var decryptByteArray = Array.ConvertAll(decryptArray, (s => Convert.ToByte(byte.Parse(s, System.Globalization.NumberStyles.HexNumber))));
byte[] bytes = rsa.Decrypt(decryptByteArray, true);
string result = System.Text.Encoding.UTF8.GetString(bytes);
return result;
}
}
我在几个项目中使用这个代码好几个月,看起来很完美。直到我正在进行的项目。它按照我的意愿在本地工作。但是当我将它发布到我的服务器时,我收到了这个错误:
解码OAEP填充时发生错误。
堆栈追踪:
[CryptographicException: Error occurred while decoding OAEP padding.]
System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle pKeyContext, Byte[] pbEncryptedKey, Int32 cbEncryptedKey, Boolean fOAEP, ObjectHandleOnStack ohRetDecryptedKey) +0
System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP) +214
myNamespace.SecurityUtils.Decrypt(String stringToDecrypt, String key) +260
... the rest...
你有什么想法吗?提前谢谢。