System.Security.Cryptography.CryptographicException: 系统找不到指定的文件
2014-07-11 10:55
417 查看
最近,研究RSA加密解密时,出现一个Bug。
是这样的,在远程测试接口时,需要进行RSA数据解密。
RSA解密代码如下:
/// <summary>
/// RSA解密
/// </summary>
/// <param name="encryptData">经过Base64编码的密文</param>
/// <param name="privateKey">私钥</param>
/// <returns>RSA解密后的数据</returns>
public static string decrypt(string encryptData, string privateKey)
{
string decryptData = "";
try
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(privateKey);
byte[] bEncrypt = Convert.FromBase64String(encryptData);
int length = bEncrypt.Length;
int offset = 0;
string cache ;
int i = 0;
while (length - offset > 0)
{
if (length - offset > MAXDECRYPTSIZE)
{
cache = Encoding.UTF8.GetString(provider.Decrypt(getSplit(bEncrypt, offset, MAXDECRYPTSIZE), false));
}
else
{
cache = Encoding.UTF8.GetString(provider.Decrypt(getSplit(bEncrypt, offset, length - offset), false));
}
decryptData += cache;
i++;
offset = i*MAXDECRYPTSIZE;
}
}
catch(Exception e)
{
throw e;
}
return decryptData;
}
在进行本地调试或者是发布到本地IIS上,加密解密一切正常,但是,一把服务发布到远程服务器上时,在进行解密,却提示如下错误:
System.Security.Cryptography.CryptographicException:系统找不到指定的文件。
在 Util.Security.RSAUtils.decrypt(String encryptData,String privateKey) 位置 D:\documents\visual studio2010\Projects\MSPJK\MSPJK\RSAUtils.cs:行号 175
在MSPJK.DAO.DAO.login(loginInfo loginInfo) 位置 D:\documents\visualstudio 2010\Projects\MSPJK\MSPJK\DAO.cs:行号 2052
在找了许多资料后算是解决了。解决办法如下:
将代码中:
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
改为:
CspParameters RSAParams = new CspParameters();
RSAParams.Flags = CspProviderFlags.UseMachineKeyStore;
System.Security.Cryptography.RSACryptoServiceProvider provider = new RSACryptoServiceProvider(1024, RSAParams);
再进行测试,OK了!
参考:http://www.verydemo.com/cm.jsp?c=66&u=systemsecuritycryptographycryptographicexception-xi-tong-zhao-bu-dao-zhi-ding-de-wen-jian-jie-jue-ban-fa-zhuan
是这样的,在远程测试接口时,需要进行RSA数据解密。
RSA解密代码如下:
/// <summary>
/// RSA解密
/// </summary>
/// <param name="encryptData">经过Base64编码的密文</param>
/// <param name="privateKey">私钥</param>
/// <returns>RSA解密后的数据</returns>
public static string decrypt(string encryptData, string privateKey)
{
string decryptData = "";
try
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(privateKey);
byte[] bEncrypt = Convert.FromBase64String(encryptData);
int length = bEncrypt.Length;
int offset = 0;
string cache ;
int i = 0;
while (length - offset > 0)
{
if (length - offset > MAXDECRYPTSIZE)
{
cache = Encoding.UTF8.GetString(provider.Decrypt(getSplit(bEncrypt, offset, MAXDECRYPTSIZE), false));
}
else
{
cache = Encoding.UTF8.GetString(provider.Decrypt(getSplit(bEncrypt, offset, length - offset), false));
}
decryptData += cache;
i++;
offset = i*MAXDECRYPTSIZE;
}
}
catch(Exception e)
{
throw e;
}
return decryptData;
}
在进行本地调试或者是发布到本地IIS上,加密解密一切正常,但是,一把服务发布到远程服务器上时,在进行解密,却提示如下错误:
System.Security.Cryptography.CryptographicException:系统找不到指定的文件。
在 Util.Security.RSAUtils.decrypt(String encryptData,String privateKey) 位置 D:\documents\visual studio2010\Projects\MSPJK\MSPJK\RSAUtils.cs:行号 175
在MSPJK.DAO.DAO.login(loginInfo loginInfo) 位置 D:\documents\visualstudio 2010\Projects\MSPJK\MSPJK\DAO.cs:行号 2052
在找了许多资料后算是解决了。解决办法如下:
将代码中:
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
改为:
CspParameters RSAParams = new CspParameters();
RSAParams.Flags = CspProviderFlags.UseMachineKeyStore;
System.Security.Cryptography.RSACryptoServiceProvider provider = new RSACryptoServiceProvider(1024, RSAParams);
再进行测试,OK了!
参考:http://www.verydemo.com/cm.jsp?c=66&u=systemsecuritycryptographycryptographicexception-xi-tong-zhao-bu-dao-zhi-ding-de-wen-jian-jie-jue-ban-fa-zhuan
相关文章推荐
- System.Security.Cryptography.CryptographicException: 系统找不到指定的文件。
- 开发微信发红包时,本地测试没有问题发布IIS上提示 System.Security.Cryptography.CryptographicException: 系统找不到指定的文件。
- System.Security.Cryptography.CryptographicException: 系统找不到指定的文件
- System.Security.Cryptography.CryptographicException: 系统找不到指定的文件。
- System.Security.Cryptography.RSA.FromXmlString 系统找不到指定的文件和X509读取证书文件系统找不到指定的文件异常
- 【已解决】Windows Service 问题System.IO.FileNotFoundException: 未能加载文件或程序集“file:///”或它 的某一个依赖项。系统找不到指定的文件。。
- WebApi 数据保护操作未成功。这可能是由于未为当前线程的用户上下文加载用户配置文件导致的。当线程执行模拟时,可能会出现此情况。","ExceptionType":"System.Security.Cryptography.CryptographicException","StackTrace
- Java项目读取配置文件时,FileNotFoundException 系统找不到指定的文件,System.getProperty("user.dir")的理解
- System.Security.Cryptography.CryptographicException: 指定了无效的提供程序类型
- 解决 System.IO.FileNotFoundException: 未能加载文件或程序集“Team.DLinqDAL”或它的某一个依赖项。系统找不到指定的文件。
- System.IO.FileNotFoundException:“未能加载文件或程序集“xxx”或它的某一个依赖项。系统找不到指定的文件。”
- 服务 在初始化安装时发生异常:System.IO.FileNotFoundException: "file:///D:\testService"未能加载文件或程序集。系统找不到指定文件。
- 未能加载文件或程序集“System.EnterpriseServices, Version=4.0.0.0..."系统找不到指定的文件
- Java properties | FileNotFoundException: properties (系统找不到指定的文件。)
- System.Security.Cryptography.CryptographicException
- 未能加载文件或程序集"System.Data,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"或它的某一个依赖项。系统找不到指定的文件。
- System.Security.Cryptography.CryptographicException: The data to be decrypted exceeds the maximum for this modulus of 128 bytes. (RSACryptoServiceProv
- System.Security.Cryptography.CryptographicException: Keyset does not exist or Access is denied.
- .NET:System.Security.Cryptography.CryptographicException 的解决办法
- 验证配置设置时发生错误,已引发类型为System.Runtime.InteropServices.COMException的异常。其他异常信息:系统找不到指定的路径