ZengCode.Com (The Thai Php Framework)  


Home   Download   Manual   About us    

Facebook   


MAIN MENU
News
Php Tips
Spring+Strut+Hibernate
Android Programming
Design Pattern By PHP
Ubuntu
Linux Quick Tips
C# Design Pattern
C# using Linq น่าใช้จริงๆ
C# Tips & Technique
Java & JavaScript Tips
Database & SQL
ZengCode Framework Guide
Zeng Code Code
Programming
IPhone (Tips and Trick)

Download เอกสารที่น่าสนใจ

     RSACryptoServiceProvider decrypt with public key  (2009-10-27)

Here are the code fragments I described above:
---------------- Create a key pair -------------------------------
RSACryptoServiceProvider csp = new RSACryptoServiceProvider(1024);

string s = csp.ToXmlString(true);
StreamWriter sw = new StreamWriter("KeyPriv.xml");
sw.WriteLine(s);
sw.Close();

s = csp.ToXmlString(false);
sw = new StreamWriter("KeyPub.xml");
sw.WriteLine(s);
sw.Close();

csp.Clear();
------------------------------------------------------------------
---------------- Encrypt with private key ------------------------
RSACryptoServiceProvider csp = new RSACryptoServiceProvider(1024);

StreamReader sr = new StreamReader("KeyPriv.xml");
string s = sr.ReadToEnd();
sr.Close();
csp.FromXmlString(s);

byte[] inp = System.Text.Encoding.Unicode.GetBytes(clearText);
byte[] outp = csp.Encrypt(inp, false);
cypherText = Convert.ToBase64String(outp);
csp.Clear();
------------------------------------------------------------------
---------------- Decrypt with public key -------------------------
RSACryptoServiceProvider csp = new RSACryptoServiceProvider(1024);

StreamReader sr = new StreamReader("KeyPub.xml");
string s = sr.ReadToEnd();
sr.Close();
csp.FromXmlString(s);

byte[] inp = Convert.FromBase64String(cypherText);
// This always throws an exception "invalid key" :(((
byte[] outp = csp.Decrypt(inp, false);
clearText = System.Text.Encoding.Unicode.GetString(outp);
csp.Clear();
------------------------------------------------------------------


Comment
Name
Comment
Security CodeCAPTCHA Image

web hit counter

This page took 0.067849 seconds to load.