none
CLR and RSA RRS feed

  • Question

  • Hi!

    I have a problem: Trere is WinForm. This WinForm decrypts and encrypts text. I have made CLR with code from my WinForm (It's scalar function. It forms text and calls CLR for encrypt/decrypt text). But, when I call this function, I get error:

    " A .NET Framework error occurred during execution of user-defined routine or aggregate "fn_fk1_getdata":   System.Security.Cryptography.CryptographicException: Key not valid for use in specified state.    System.Security.Cryptography.CryptographicException:      at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)     at System.Security.Cryptography.Utils._EncryptKey(SafeKeyHandle hPubKey, Byte[] key)     at System.Security.Cryptography.RSACryptoServiceProvider.Encrypt(Byte[] rgb, Boolean fOAEP)     at UserDefinedFunctions.fn_fk1_getdata(String xml, String url) " 

    Public and private Keys keeps local. 

    What can I correct this problem?

    Many thanks for the answer

    PS I apologize for my English

    Thursday, May 28, 2015 8:19 AM

All replies

  • Can you post a simple, complete repro of your issue?

    David


    David http://blogs.msdn.com/b/dbrowne/

    Thursday, May 28, 2015 2:16 PM
  • Can you post a simple, complete repro of your issue?

    David


    David http://blogs.msdn.com/b/dbrowne/

    Hello Ya,

    Or you could have a try to do a source code debug according to this blog:http://blogs.msdn.com/b/dotnet/archive/2014/02/24/a-new-look-for-net-reference-source.aspx to ses if you would be lucky to find the caused reason.

    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.


    Friday, May 29, 2015 3:28 AM
    Moderator
  • This is my WinForm

    {
            StreamReader sr = new StreamReader(@"C:\...\tect.cer"); //Надо указать путь к pem  файлу
            PemReader pr = new PemReader(sr);
            CspParameters RSAParams = new CspParameters();
            RSAParams.Flags = CspProviderFlags.UseMachineKeyStore;
            RsaKeyParameters KeyPair = (RsaKeyParameters)pr.ReadObject();
            RSAParameters rsapar = new RSAParameters();
            sr.Close();
            rsapar.Modulus = KeyPair.Modulus.ToByteArrayUnsigned();
            rsapar.Exponent = KeyPair.Exponent.ToByteArrayUnsigned();
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024, RSAParams);
            rsa.ImportParameters(rsapar);
            byte[] encrypted = rsa.Encrypt(Encoding.Default.GetBytes(to_encrypt), false);
            byte[] encrypted = rsa.Encrypt(decrypted, false);
            string encdate = Convert.ToBase64String(encrypted);
            textBox2.Text = BitConverter.ToString(decrypted).Replace("-", "");
            
        }
        

    It work perfect

    It is my CLR

     public static string fn_fk1_getdata(string xml, string url)
        {
            
            StreamReader sr = new StreamReader("C:\\...\\test.cer"); 
            PemReader pr = new PemReader(sr);
            CspParameters RSAParams = new CspParameters();
            RSAParams.ProviderType = 1;
            RSAParams.Flags = CspProviderFlags.UseMachineKeyStore;
            RsaKeyParameters KeyPair = (RsaKeyParameters)pr.ReadObject();
            RSAParameters rsapar = new RSAParameters();
            sr.Close();
            rsapar.Modulus = KeyPair.Modulus.ToByteArrayUnsigned();
            rsapar.Exponent = KeyPair.Exponent.ToByteArrayUnsigned();
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024, RSAParams);
            rsa.ImportParameters(rsapar);
            byte[] encrypted = rsa.Encrypt(Encoding.Default.GetBytes(xml), false);
           
            string encdate = Convert.ToBase64String(encrypted);
    
    
            string str =encdate;
    
           
            string str1 = "<request><confirm><transaction>123</transaction></confirm><sign>" + str
                + "</sign></request>";

    This CLR doesn't work: "System.Security.Cryptography.CryptographicException: Key not valid for use in specified state."


    • Edited by Ya_Vanilko Friday, May 29, 2015 8:01 AM
    Friday, May 29, 2015 8:00 AM
  • What do you mean by "It is my CLR"?  What kind of program is the failure in?

    Is this CLR hosted inside SQL Server?

    David


    David http://blogs.msdn.com/b/dbrowne/

    Friday, May 29, 2015 1:15 PM