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();
}
}
Bookmarks