none
Encrypting string with a private key RRS feed

  • Question

  • Hi,

    I have the following string and the private key and I need to encrypt the string with the private key. In PHP I can achieve this by using the following command: openssl_sign($concData, $signature, $privKey, OPENSSL_ALGO_SHA256);

    String="IPCPurchase-1.4-en-000000000000010-61938166610-1-SDK_PHP_1.4-EUR-20.5-OIS000001-https://sql.oi-soft.com-https://www.sparkleimage.com.mt-https://www.bridgepoint.com.mt--sammuts@hotmail.com-+356 79299785-Ivan-Sammut-MLT-Zurrieq-ZRQ 2300-Tulips Court, Flat 3-1-Laptop-1-20.5-20.5-EUR-0-1-0"

    Certificate = 

    "-----BEGIN RSA PRIVATE KEY-----
    MIICXAIBAAKBgQCf0TdcTuphb7X+Zwekt1XKEWZDczSGecfo6vQfqvraf5VPzcnJ
    2Mc5J72HBm0u98EJHan+nle2WOZMVGItTa/2k1FRWwbt7iQ5dzDh5PEeZASg2UWe
    hoR8L8MpNBqH6h7ZITwVTfRS4LsBvlEfT7Pzhm5YJKfM+CdzDM+L9WVEGwIDAQAB
    AoGAYfKxwUtEbq8ulVrD3nnWhF+hk1k6KejdUq0dLYN29w8WjbCMKb9IaokmqWiQ
    5iZGErYxh7G4BDP8AW/+M9HXM4oqm5SEkaxhbTlgks+E1s9dTpdFQvL76TvodqSy
    l2E2BghVgLLgkdhRn9buaFzYta95JKfgyKGonNxsQA39PwECQQDKbG0Kp6KEkNgB
    srCq3Cx2od5OfiPDG8g3RYZKx/O9dMy5CM160DwusVJpuywbpRhcWr3gkz0QgRMd
    IRVwyxNbAkEAyh3sipmcgN7SD8xBG/MtBYPqWP1vxhSVYPfJzuPU3gS5MRJzQHBz
    sVCLhTBY7hHSoqiqlqWYasi81JzBEwEuQQJBAKw9qGcZjyMH8JU5TDSGllr3jybx
    FFMPj8TgJs346AB8ozqLL/ThvWPpxHttJbH8QAdNuyWdg6dIfVAa95h7Y+MCQEZg
    jRDl1Bz7eWGO2c0Fq9OTz3IVLWpnmGwfW+HyaxizxFhV+FOj1GUVir9hylV7V0DU
    QjIajyv/oeDWhFQ9wQECQCydhJ6NaNQOCZh+6QTrH3TC5MeBA1Yeipoe7+BhsLNr
    cFG8s9sTxRnltcZl1dXaBSemvpNvBizn0Kzi8G3ZAgc=
    -----END RSA PRIVATE KEY-----"

    The resulting output string should be "SJ4uNkR4sOR8eW94CcCXXJXDpfMYA0FIM+1kRR4GkZ6Zko4ldQWSeCSGGSihyf9VL9lU+0pbT9GQEshYEvQ/ke4Y4zq9+GNe7jxwvG1TtT44TLxaVkBYm5z35PU7HmmNzf28xN3waQhp/C+dsCPJYdP3Q8ip9pceCEbQIezq1uI="

    Any idea how I can achieve this please

    Thanks

    Ivan 

    Saturday, November 2, 2019 2:09 PM

All replies

  • search rsa sha256 C# in a search engine. 


    Visual C++ MVP

    Saturday, November 2, 2019 8:56 PM
  • Hi Ivan Sammut, 

    Thank you for posting here.

    For your question, you want to encrypt string with a private key using RSA.

    We usually use public key to encrypt data and private key to decrypt data in C#.

    Here’s a related reference.

    How to use public and private key encryption technique in C#

    I note that you use SHA256, so you can refer to the following reference about using SHA256 in RSA encryptor in .NET.

    Can RSACryptoServiceProvider (.NET's RSA) use SHA256 for encryption (not signing) instead of SHA1?

    Hope them can help you.

    Besides, if I have any misunderstandings, please provide more descriptions about ‘openssl_sign($concData, $signature, $privKey, OPENSSL_ALGO_SHA256)’. It will help us to analyze your problem.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 4, 2019 7:27 AM
    Moderator
  • Hi,

    My current code is like this:

    string sPrivateKey = @"-----BEGIN RSA PRIVATE KEY-----
    MIICXAIBAAKBgQCf0TdcTuphb7X+Zwekt1XKEWZDczSGecfo6vQfqvraf5VPzcnJ
    2Mc5J72HBm0u98EJHan+nle2WOZMVGItTa/2k1FRWwbt7iQ5dzDh5PEeZASg2UWe
    hoR8L8MpNBqH6h7ZITwVTfRS4LsBvlEfT7Pzhm5YJKfM+CdzDM+L9WVEGwIDAQAB
    AoGAYfKxwUtEbq8ulVrD3nnWhF+hk1k6KejdUq0dLYN29w8WjbCMKb9IaokmqWiQ
    5iZGErYxh7G4BDP8AW/+M9HXM4oqm5SEkaxhbTlgks+E1s9dTpdFQvL76TvodqSy
    l2E2BghVgLLgkdhRn9buaFzYta95JKfgyKGonNxsQA39PwECQQDKbG0Kp6KEkNgB
    srCq3Cx2od5OfiPDG8g3RYZKx/O9dMy5CM160DwusVJpuywbpRhcWr3gkz0QgRMd
    IRVwyxNbAkEAyh3sipmcgN7SD8xBG/MtBYPqWP1vxhSVYPfJzuPU3gS5MRJzQHBz
    sVCLhTBY7hHSoqiqlqWYasi81JzBEwEuQQJBAKw9qGcZjyMH8JU5TDSGllr3jybx
    FFMPj8TgJs346AB8ozqLL/ThvWPpxHttJbH8QAdNuyWdg6dIfVAa95h7Y+MCQEZg
    jRDl1Bz7eWGO2c0Fq9OTz3IVLWpnmGwfW+HyaxizxFhV+FOj1GUVir9hylV7V0DU
    QjIajyv/oeDWhFQ9wQECQCydhJ6NaNQOCZh+6QTrH3TC5MeBA1Yeipoe7+BhsLNr
    cFG8s9sTxRnltcZl1dXaBSemvpNvBizn0Kzi8G3ZAgc=
    -----END RSA PRIVATE KEY-----";
                string sValue = "IPCPurchase-1.4-en-000000000000010-61938166610-1-SDK_PHP_1.4-EUR-20.5-OIS000001-https://sql.oi-soft.com-https://www.sparkleimage.com.mt-https://www.bridgepoint.com.mt--sammuts@hotmail.com-+356 79299785-Ivan-Sammut-MLT-Zurrieq-ZRQ 2300-Tulips Court, Flat 3-1-Laptop-1-20.5-20.5-EUR-0-1-0";
                //sValue = Convert.ToBase64String(sValue); 
                byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(sValue);
                sValue = System.Convert.ToBase64String(toEncodeAsBytes);
                /* ^^^ OK Till here ^^^*/
              

                RSACryptoServiceProvider RSA = this.CreateRsaProviderFromPrivateKey(sPrivateKey.Replace("-----BEGIN RSA PRIVATE KEY-----", string.Empty)
                    .Replace("-----END RSA PRIVATE KEY-----", string.Empty)
                    .Replace("\n", string.Empty));


                
                RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
                RSAFormatter.SetHashAlgorithm("SHA256");
                SHA256Managed SHhash = new SHA256Managed();
                byte[] SignedHashValue = RSAFormatter.CreateSignature(SHhash.ComputeHash(new UnicodeEncoding().GetBytes(sValue)));
                string signature = System.Convert.ToBase64String(SignedHashValue);

    It work fine till where it says " /* ^^^ OK Till here ^^^*/".

    But then somehow the signature created is different from the one obtained by the php openssl_sign function.

    The result from the c# code is "jZ1+fBInpR6ugpz/5DY3d+f0LRVnJDOJoIqe6wDAjXzH4wO9qdR1d6aWbwqC8YU2ah2Dy4d235j85MJbxBlBpSqz6PyfX/1rPpQ1AZDzVZA11NDxla27Aa9iaBl5MvGxC4jHYpw01tAcnteseAw8hvjJ89WApwYApIuAGP+WFZs="

    whilst the signature generated from php is "SJ4uNkR4sOR8eW94CcCXXJXDpfMYA0FIM+1kRR4GkZ6Zko4ldQWSeCSGGSihyf9VL9lU+0pbT9GQEshYEvQ/ke4Y4zq9+GNe7jxwvG1TtT44TLxaVkBYm5z35PU7HmmNzf28xN3waQhp/C+dsCPJYdP3Q8ip9pceCEbQIezq1uI="

    The entire php code is 

    $privKey = openssl_get_privatekey($this->getCnf()->getPrivateKey());
    openssl_sign($concData, $signature, $privKey, OPENSSL_ALGO_SHA256);
    return base64_encode($signature);

    Thanks

    Ivan

    Monday, November 4, 2019 11:38 AM
  • try ansi encoding. 


    Visual C++ MVP

    Monday, November 4, 2019 1:22 PM
  • Hi Ivan Sammut,

    Thanks for your feedback.

    I make a test based on your code, and I get the same result from the c# code.

    Considering we cannot make a test using php, I find some references may help you.

    1. Convert hash_256 in PHP to C# equivalent
    2. php to C# converter [closed]

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, November 6, 2019 8:20 AM
    Moderator