Results 1 to 6 of 6

Thread: RSA decryption problem in C#

  1. #1
    Join Date
    Aug 2006
    Posts
    155

    RSA decryption problem in C#

    I am trying to encrypt a string using RSA in C# and also I am trying to decrypt it in JavaScript. But of no use. I have already tried many ways for doing the different things for RSA in both C# and JavaScript but i just cant decrypt my strings. I am using RSACryptoServiceProvider for doing the RSA for my asymmetric encryption in C#. Does anyone know how RSACryptoServiceProvider chooses which key (public or private) to use during encrypting.?? Please help me as soon as possible.!!
    Desktop * Athlon X2 4200 | 2048M RAM | 160G HD | 7600GT
    MacMini * G4 1.33GHz | 512M RAM | 40G HD | Radeon 9200
    Laptop * Sempron 2800 | 512M RAM | 60G HD
    PDA * Dell Axim x51 | 128M Internal | 256M SD Card

  2. #2
    Join Date
    Nov 2008
    Posts
    1,192

    Re: RSA decryption problem in C#

    First tell me that, can you decrypt in C# using your own code? If not, you'll have to show the coding that you had made. Typically, one does not 'encrypt a string' with RSA in a real app. Typically, one encrypts a symmetric key which in turn is used for the actual encryption of data or one decrypts a hash of data, to achieve a signature. There is a documented interop problem with RSACryptoServiceProvider, you may need to reverse the bytes in the array before use. Another issue is padding, please ensure that you're using the same padding parameters for encryption and decryption. So provide the code for getting more helpful information.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: RSA decryption problem in C#

    I have provided some code that can be useful for the programs using the RSACryptoServiceProvider.
    For the Visual Basic (Declaration)
    Code:
    <ComVisibleAttribute(True)> _
    Public NotInheritable Class RSACryptoServiceProvider _
        Inherits RSA _
        Implements ICspAsymmetricAlgorithm
    If you are using Visual Basic (Usage)
    Code:
    Dim instance As RSACryptoServiceProvider
    This is for the C#
    Code:
    [ComVisibleAttribute(true)]
    public sealed class RSACryptoServiceProvider : RSA, 
        ICspAsymmetricAlgorithm

  4. #4
    Join Date
    Aug 2006
    Posts
    155

    Re: RSA decryption problem in C#

    Thank You all you tried to solve my problem. Here is my coding :
    Code:
    public class myRSA
    {
    private System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
    private RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    private static string modPath = "C:\\rsaHamster\\mod.txt";
    private static string expoPath = "C:\\rsaHamster\\expo.txt";
    private static string dPath = "C:\\rsaHamster\\D.txt";
    public void export()
    {
    RSACryptoServiceProvider rsaExport = new RSACryptoServiceProvider();
    RSAParameters rsaParam = new RSAParameters();
    rsaParam = rsaExport.ExportParameters(true);
    
    fileMgr.write(rsaParam.Modulus,modPath);
    fileMgr.write(rsaParam.Exponent,expoPath);
    fileMgr.write(rsaParam.D,dPath);
    fileMgr.write(rsaParam.DP, dpPath);
    }
    public RSAParameters import(bool check)
    {
    RSAParameters rsaParam = new RSAParameters();
    
    byte[] mod = fileMgr.read(modPath);
    byte[] expo = fileMgr.read(expoPath);
    byte[] D = fileMgr.read(dPath);
    byte[] dp = fileMgr.read(dpPath);
    if (check == true)
    {
    rsaParam.D = D;
    rsaParam.Exponent = expo;
    rsaParam.Modulus = mod;
    
    rsaParam.DP = dp;
    rsaParam.DQ = dq;
    rsaParam.InverseQ = inverseQ;
    
    }
    else if (check == false)
    {
    rsaParam.Exponent = expo;
    rsaParam.Modulus = mod;
    rsaParam.DP = dp;
    rsaParam.DQ = dq;
    rsaParam.InverseQ = inverseQ;
    }
    return rsaParam;
    }
    
    public byte[] encryptUsingPublic(string msg)
    {
    RSACryptoServiceProvider pubRsa = new RSACryptoServiceProvider();
    RSAParameters rsaParam = new RSAParameters();
    rsaParam = this.import(false);
    pubRsa.ImportParameters(rsaParam);
    
    Byte[] data = encoding.GetBytes(msg);
    Byte[] encrypted = pubRsa.Encrypt(data, false);
    
    return encrypted;
    }
    public byte[] decryptUsingPrivate(byte[] cipher)
    {
    
    RSACryptoServiceProvider decRsa = new RSACryptoServiceProvider();
    RSAParameters rsaParam = new RSAParameters();
    rsaParam = this.import(true);
    decRsa.ImportParameters(rsaParam);
    byte[] plain;
    plain = decRsa.Decrypt(cipher, false);
    return plain;
    }
    public byte[] encryptUsingPrivate(string msg)
    {
    RSACryptoServiceProvider privRsa = new RSACryptoServiceProvider();
    RSAParameters rsaParam = new RSAParameters();
    rsaParam = this.import(true);
    privRsa.ImportParameters(rsaParam);
    
    Byte[] data = encoding.GetBytes(msg);
    Byte[] encrypted = privRsa.Encrypt(data, false);
    
    return encrypted;
    }
    
    public byte[] decryptUsingPublic(byte[] cipher)
    {
    RSACryptoServiceProvider decRsa = new RSACryptoServiceProvider();
    RSAParameters rsaParam = new RSAParameters();
    rsaParam = this.import(false);
    decRsa.ImportParameters(rsaParam);
    
    byte[] plain;
    plain = decRsa.Decrypt(cipher, false);
    
    return plain;
    }
    }
    
    class MainProg
    {
    public static void Main(string[] args)
    {
    System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
    myRSA myrsa = new myRSA();
    myrsa.export();
    
    Console.Write("Enter string to be encrypted: ");
    string msg = Console.ReadLine();
    
    string val = null;
    Console.WriteLine();
    while (val != "c")
    {
    Console.WriteLine("a. Encrypt using Public then Decrypt using Private (challenge)");
    Console.WriteLine("b. Encrypt using Private then Decrypt using Public (key exchange)");
    Console.WriteLine("c. Exit");
    val = Console.ReadLine();
    if (val == "a")
    {
    byte[] encrypted = myrsa.encryptUsingPublic(msg);
    Console.Write("\nEncrypted msg is: ");
    Console.WriteLine(encoding.GetString(encrypted));
    Console.WriteLine();
    byte[] decrypted = myrsa.decryptUsingPrivate(encrypted);
    }
    else if (val == "b")
    {
    byte[] encrypted = myrsa.encryptUsingPrivate(msg);
    Console.Write("\nEncrypted msg is: ");
    Console.WriteLine(encoding.GetString(encrypted));
    Console.WriteLine();
    byte[] decrypted = myrsa.decryptUsingPublic(encrypted);
    }
    }
    Console.Read();
    }
    }
    Desktop * Athlon X2 4200 | 2048M RAM | 160G HD | 7600GT
    MacMini * G4 1.33GHz | 512M RAM | 40G HD | Radeon 9200
    Laptop * Sempron 2800 | 512M RAM | 60G HD
    PDA * Dell Axim x51 | 128M Internal | 256M SD Card

  5. #5
    Join Date
    Nov 2008
    Posts
    996

    Re: RSA decryption problem in C#

    Asymmetric encryption uses a related key-pair to encrypt and decrypt data.
    One of the keys is the “public key” and the other is the “private key”.
    The data encrypted with the public key can only be decrypted with the private key, and vice-versa. RSA is one of the popular asymmetric algorithms and that’s what we’re going to deal with in this article. To use the cryptographic services, we need to use the System.Security.Cryptography. namespace Code to Enable RSA Encryption/Decryption
    Code:
    public class Cryptography
    {
      public static RSACryptoServiceProvider rsa;
    
      public static void AssignParameter()
      {
    	const int PROVIDER_RSA_FULL = 1; 
    	const string CONTAINER_NAME = "SpiderContainer";
    	CspParameters cspParams;
    	cspParams = new CspParameters(PROVIDER_RSA_FULL);
    	cspParams.KeyContainerName = CONTAINER_NAME;
    	cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
    	cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
    	rsa = new RSACryptoServiceProvider(cspParams); 
      }
    
      public static string EncryptData(string data2Encrypt)
      {
    	AssignParameter();
          StreamReader reader = new   StreamReader(@"C:\Inetpub\wwwroot\dotnetspiderencryption\publickey.xml");
    	string publicOnlyKeyXML = reader.ReadToEnd();
    	rsa.FromXmlString(publicOnlyKeyXML);
    	reader.Close();
    
    	//read plaintext, encrypt it to ciphertext
    
    	byte[] plainbytes =	System.Text.Encoding.UTF8.GetBytes(data2Encrypt);
    	byte[] cipherbytes = rsa.Encrypt(plainbytes,false);
    	return Convert.ToBase64String(cipherbytes);
      }
    
      public static void AssignNewKey()
      {
    	AssignParameter();
    	
    	//provide public and private RSA params
    	StreamWriter writer = new   
    
    StreamWriter(@"C:\Inetpub\wwwroot\dotnetspiderencryption\privatekey.xml");
    	string publicPrivateKeyXML = rsa.ToXmlString(true);
    	writer.Write(publicPrivateKeyXML);
    	writer.Close();
    
    	//provide public only RSA params
    	writer = new StreamWriter(@"C:\Inetpub\wwwroot\dotnetspiderencryption\publickey.xml");
    	string publicOnlyKeyXML = rsa.ToXmlString(false);
    	writer.Write(publicOnlyKeyXML);
    	writer.Close();
    }
     public static string DecryptData(string data2Decrypt)
      {
    	AssignParameter();
    
    	byte[] getpassword = Convert.FromBase64String(data2Decrypt);
    			
    	StreamReader reader = new 
    
    StreamReader(@"C:\Inetpub\wwwroot\dotnetspiderencryption\privatekey.xml");
    	string publicPrivateKeyXML = reader.ReadToEnd();
    	rsa.FromXmlString(publicPrivateKeyXML);
    	reader.Close();
    			
    	//read ciphertext, decrypt it to plaintext
    	byte[] plain =	rsa.Decrypt(getpassword,false); 
    	return System.Text.Encoding.UTF8.GetString(plain);
    }
    }

  6. #6
    Join Date
    Mar 2008
    Posts
    349

    Re: RSA decryption problem in C#

    The key length supported by the RSACryptoServiceProvider is from 384 bits to 16384 bits in increments of 8 bits. You will get this support only if you are having the Microsoft Enhanced Cryptographic Provider installed. If you are having the Microsoft Base Cryptographic Provider installed, then it supports key lengths from 384 bits to 512 bits in increments of 8 bits. You should ensure that IDisposable is called when you are finished with the RSACryptoServiceProvider. Hope that these hints will help you somewhat.!!

Similar Threads

  1. DES encryption and decryption using C or Java
    By sayanmaji in forum Software Development
    Replies: 5
    Last Post: 26-11-2010, 03:20 AM
  2. Asymmetric Decryption Problem
    By DarrenYoung in forum Software Development
    Replies: 1
    Last Post: 20-09-2010, 05:46 PM
  3. XML encryption and decryption
    By Anirvinya in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 03:31 AM
  4. Do you know difference between encryption and decryption?
    By rooki in forum Networking & Security
    Replies: 4
    Last Post: 30-11-2009, 12:24 PM
  5. Encryption in JavaScript, Decryption in PHP
    By Rail racer in forum Software Development
    Replies: 6
    Last Post: 11-10-2008, 03:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,269,377.15833 seconds with 17 queries