Please use Codeblocks for posting Code. reading long code without Syntax Highlighting is almsot impossible.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Xml;
namespace RSA_Encryption
{
class Program
{
static void Main(string[] args)
{
RSAParameters();
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\Encrytionanddecrytion.xml");
Encrypt(doc);
Decrypt(doc);
}
public static void RSAParameters()
{
var rsa = new RSACryptoServiceProvider();
var parameters = rsa.ExportParameters(true);
byte[] privateParameters = rsa.ExportCspBlob(true);
byte[] publicParameters = rsa.ExportCspBlob(false);
Console.WriteLine("PrivateKey");
string privatekey = Convert.ToBase64String(privateParameters);
Console.WriteLine(privatekey);
Console.WriteLine("PublicKey");
string publickey = Convert.ToBase64String(publicParameters);
Console.WriteLine(publickey);
Console.Read();
}
static void Encrypt(XmlDocument doc)
{
RSACryptoServiceProvider cipher = new RSACryptoServiceProvider();
string PublicKey = Properties.Resources.PublicKey;
byte[] Publicbytekey = System.Convert.FromBase64String(PublicKey);
cipher.ImportCspBlob(Publicbytekey);
XmlNode Password = doc.SelectSingleNode("/Encrytionanddecrytion/Password");
byte[] Password = Encoding.ASCII.GetBytes(Password.InnerText);
byte[] encryptpassword = cipher.Encrypt(Password, false);
string EncryptedPassword = Convert.ToBase64String(encryptpassword);
Password.InnerText = EncryptedPassword;
Console.WriteLine(Password);
Console.Read();
}
static void Decrypt(XmlDocument doc)
{
RSACryptoServiceProvider cipher = new RSACryptoServiceProvider();
string PrivateKey = Properties.Resources.PrivateKey;
byte[] Privatebytekey = System.Convert.FromBase64String(PrivateKey);
cipher.ImportCspBlob(Privatebytekey);
XmlNode Password = doc.SelectSingleNode("/Encrytionanddecrytion/Password");
byte[] Password = Encoding.ASCII.GetBytes(Password.InnerText);
byte[] decryptpassword = cipher.Decrypt(Password, false);
string decryptedPassword = Convert.ToBase64String(decryptpassword);
Password.InnerText = decryptedPassword;
}
}
}
We need to know the line on wich the exception happens. It would also help to have the Stacktrace of the exception including all inner exceptions.
The Framework version you are targetting might have an effect too.
Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.
No comments:
Post a Comment