ZengCode.Com (The Thai Php Framework)  


Home   Download   Manual   About us    

Facebook   


MAIN MENU
เขียนโปรแกรมบน iPhone ด้วย MonoTouch
News
Php Tips
Ubuntu
Spring+Strut+Hibernate
Android Programming
Design Pattern By PHP
C# Design Pattern
Linux Quick Tips
C# Tips & Technique
C# using Linq น่าใช้จริงๆ
Java & JavaScript Tips
MAVEN
Database & SQL
ZengCode Framework Guide
Mac OSx
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.056243 seconds to load.