none
关于RSA解密的C#程序导入数据库程序集运行出错的问题。 RRS feed

  • 问题

  • 问题描述:利用VS2010编写出C#的RSA解密程序并部署出SQL Server脚本,并利用SQL Server脚本在SQL Server上运行出程序集。利用数据库调用程序集的时候会报错:
    在执行用户定义例程或聚合 "jiemi" 期间出现 .NET Framework 错误: 
    System.Security.SecurityException: 请求“System.Security.Permissions.KeyContainerPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”
    类型的权限已失败。
    System.Security.SecurityException: 
    在 ZYBEncrypt.RSA.RSADecrypt(String m_strDecryptString)。

    C#程序:

    using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; using Microsoft.SqlServer.Server; namespace ZYBEncrypt { public class RSA { /// <summary> /// RSA加密 /// </summary> /// <param name="publickey">公钥</param> /// <param name="m_strEncryptString">MD5加密后的数据</param> /// <returns>RSA公钥加密后的数据</returns> [Microsoft.SqlServer.Server.SqlFunction] public static string RSAEncrypt(string m_strEncryptString) { string str2; try { string publickey = @"<RSAKeyValue><Modulus>AN84CLrcZ0ak0BPSWVpuJyCmDXfe6S0wdhg65oOkwZk3pTW4tdfIWlxIR5n2Z+DoLO18oFJcXVlWuFwcB5ljiP++0dmgJGAtNtLw+Sw7Onf5Ka3W9ns5Q30BSKMsptPke3SWtjTjMZtw1nLByggtgMhdd78jE1YbQzArfQZJaScn</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"; RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(publickey); byte[] bytes = new UnicodeEncoding().GetBytes(m_strEncryptString); str2 = Convert.ToBase64String(provider.Encrypt(bytes, false)); } catch (Exception exception) { throw exception; } return str2; } /// <summary> /// RSA解密 /// </summary> /// <param name="privatekey">私钥</param> /// <param name="m_strDecryptString">待解密的数据</param> /// <returns>解密后的结果</returns> [Microsoft.SqlServer.Server.SqlFunction] public static string RSADecrypt(string m_strDecryptString) { string str2; try { string privatekey = @"<RSAKeyValue><Modulus>AN84CLrcZ0ak0BPSWVpuJyCmDXfe6S0wdhg65oOkwZk3pTW4tdfIWlxIR5n2Z+DoLO18oFJcXVlWuFwcB5ljiP++0dmgJGAtNtLw+Sw7Onf5Ka3W9ns5Q30BSKMsptPke3SWtjTjMZtw1nLByggtgMhdd78jE1YbQzArfQZJaScn</Modulus><Exponent>AQAB</Exponent><P>APLJxlpJpBh8DrizdanLqsw60tHwSWbCuo/SB7Vu20QZTS5ZCePpx/kR0P4r3dchItnZKAxFdzJdtjN2G6/SuwE=</P><Q>AOtdpNr1y4nFuz4DacMMeP/Yhw/7MzP471ovwXo+96XUA/urm3g5JvW/TFQv+ucbxQ052kf8GW7NiNkXpTAgqic=</Q><DP>R9jSK/HS77P2n03sKf0tZBacQKg88H37u4fJ5DUUCgj6wBZ1SM7r8JcHI9BTa9BQgeszYL5F0c/NlVkERHm2AQ==</DP><DQ>ANDbrGrRKuj7GgkRpkx7SPRgBJpd4zX/dQam0h3UupXg6hfuVGv+61+vfeBHI3Lp5Zay+8xdHYSBxyo+HUvz8Ik=</DQ><InverseQ>VMcKCwaEiTPgb1mzvJnLooxVXYEpi9UNnj5qi4uM2jKALJ3txq0/YDrr0COWDcqwxThPzo8quMlsQKivjOBDAw==</InverseQ><D>MLnonfGy0ab0jP8j5iuZevUEh6AvQmALx2pEG4SBhlKxS7LQbxQbNX3Js4AoWN4VwJmt3YywC+p0QBcWSZOCufiq+4N2ClQq/RS0GFQM/tcAwr8A8WflulygaMFiEaagFHeLowsWVu3jAC1c3hnptTvi7ZwwCm2GgGoCTSKiRAE=</D></RSAKeyValue>"; RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(privatekey); byte[] rgb = Convert.FromBase64String(m_strDecryptString); byte[] buffer2 = provider.Decrypt(rgb, false); str2 = new UnicodeEncoding().GetString(buffer2); } catch (Exception exception) { throw exception; } return str2; } } }


    2014年11月26日 11:08

答案

全部回复

  • C# RSA decryption into database procedures set
      In the implementation of user defined routines or polymerization An error occurred during.NET Framework:  
      System.Security.SecurityException:Request“System.Security.Permissions.KeyContainerPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”The permission of type has failed.
      System.Security.SecurityException: 
      in System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
      in System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark)
      in System.Security.CodeAccessPermission.Demand()
      in System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters)
      in System.Security.Cryptography.RSA.FromXmlString(String xmlString)
      in ZYBEncrypt.RSA.RSADecrypt(String content).


    2014年11月26日 6:15
  • Hello yuboping,

    这里有篇blog是讨论在数据库里遇到这个permission 的错误,可能会帮到你,里面的caused reasonconfig文件没有设置真确:

    http://blogs.msdn.com/b/floditt/archive/2009/08/10/request-for-the-permission-of-type-securitypermission-failed.aspx

    并且建议你到数据库专业的forum去问有关数据库的问题

    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/home?category=sqlserver

    当前forum一般用于讨论ADO.NET LINQ的问题。

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2014年11月27日 5:16
    版主