locked
Can any one Give Code for RSA encryption Decryption RRS feed

  • Question

  • I have Problem. Data is encrypted by RSA.encrypt method

    then i stored the encrypted data

    later i retrive the encrypted data and trying to decrypt using RSA.decrypt method

    But I got error while Decrypting is "BAD DATA" error

    can anyone explain how to decrypt the same encrypted data by retriving from db in RSA

    Tuesday, February 24, 2015 6:14 AM

Answers

  • @RAGU DEVA,

    Please share your specific language, this is not the right forum for your topic.

    For example, if it is C#, you can refer to the following blog;

    http://www.codeproject.com/Articles/10877/Public-Key-RSA-Encryption-in-C-NET

    Anyway, please choose language specific forum.

    Best regards,



    Barry
    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.


    • Edited by Barry Wang Wednesday, February 25, 2015 4:41 AM
    • Marked as answer by Fred Bao Friday, March 6, 2015 10:02 AM
    Wednesday, February 25, 2015 4:40 AM
  • Btw, there is pretty high chance that some data got converted to replacement character in Encoding.ASCII.GetString(encryptedBytes);  line of encrypt() method. Consider store it in form of Hexdecimal string or better, save it as binary stream directly.
    • Marked as answer by Fred Bao Friday, March 6, 2015 10:02 AM
    Wednesday, February 25, 2015 5:53 AM
    Answerer

All replies

  •  

    // Encryption Part

                    string plainTextData = File.ReadAllText((Server.MapPath("~/das/") + me.txt) ,Encoding.ASCII);

                       string d1 = encrypt(plainTextData);

                        File.WriteAllText((Server.MapPath("~/data/") + me.txt), d1,Encoding.ASCII);

    // Decryption Part

                       string d = File.ReadAllText((Server.MapPath("~/data/") + me.txt),Encoding.ASCII);
                        
                        string decryptedtex = decrypt(d);


                        File.WriteAllText((Server.MapPath("~/temp/") + f.Name), decryptedtex,Encoding.ASCII);

    public string encrypt(string strCrypt)
            {

                using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider { KeySize = 1024 })
                {
                    rsa.ImportParameters(rsa.ExportParameters(false));

                    byte[] encodedTextBytes = Encoding.ASCII.GetBytes(strCrypt);
                    byte[] encryptedBytes = rsa.Encrypt(encodedTextBytes, false);
                    string base64EncodedData = Encoding.ASCII.GetString(encryptedBytes);
                    return base64EncodedData;

                }


            }
            public string decrypt(string strCrypt)
            {

                using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider { KeySize = 1024 })
                {
                    rsa.ImportParameters(rsa.ExportParameters(true));
                    byte[] encryptedBytes = Encoding.ASCII.GetBytes(strCrypt);
                    byte[] decryptedBytes = rsa.Decrypt(encryptedBytes, false);                      // BAD DATA EXCEPTION
                    string decryptedString = Encoding.ASCII.GetString(decryptedBytes);
                    return decryptedString;
                }

    • Merged by Fred Bao Wednesday, February 25, 2015 4:54 AM The same topic
    Tuesday, February 24, 2015 7:09 AM
  • @RAGU DEVA,

    Please share your specific language, this is not the right forum for your topic.

    For example, if it is C#, you can refer to the following blog;

    http://www.codeproject.com/Articles/10877/Public-Key-RSA-Encryption-in-C-NET

    Anyway, please choose language specific forum.

    Best regards,



    Barry
    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.


    • Edited by Barry Wang Wednesday, February 25, 2015 4:41 AM
    • Marked as answer by Fred Bao Friday, March 6, 2015 10:02 AM
    Wednesday, February 25, 2015 4:40 AM
  • Btw, there is pretty high chance that some data got converted to replacement character in Encoding.ASCII.GetString(encryptedBytes);  line of encrypt() method. Consider store it in form of Hexdecimal string or better, save it as binary stream directly.
    • Marked as answer by Fred Bao Friday, March 6, 2015 10:02 AM
    Wednesday, February 25, 2015 5:53 AM
    Answerer