Here is a little sample on how to encrypt and decrypt with the RSA encryption method:
<code>
private object pubkey;
private object prikey;
static void Main( string[] args )
{
try
{
Program start = new Program();
start.run();
}
catch( Exception ex )
{
Console.WriteLine( ex.ToString() );
}
finally
{
Console.WriteLine( "Fin" );
Console.ReadLine();
}
}
private void run()
{
generateKey();
string test = encryptFunctionStringOnly( "I'm testing the encryption module, with string only" );
string message = decryptFunctionStringOnly( test );
Console.WriteLine( "After Decryption:\n" + message );
}
private void generateKey()
{
XceedEncryptionLib.XceedEncryption encrypt = new XceedEncryptionLib.XceedEncryption();
encrypt.License( "CRY11-GRT9A-J7X75-BNAA" );
XceedEncryptionLib.XceedRSAEncryptionMethod rsa = new XceedEncryptionLib.XceedRSAEncryptionMethod();
try
{
object seed = null;
rsa.SetRandomKeyPair( 1024, ref seed );
prikey = rsa.get_PrivateKey();
pubkey = rsa.get_PublicKey();
}
catch( System.Runtime.InteropServices.COMException except )
{
Console.WriteLine( except.ToString() );
}
}
private string encryptFunctionStringOnly( string message )
{
Console.WriteLine( "Before Encryption:\n" + message );
string encryptKey;
XceedEncryptionLib.XceedEncryption encrypt = new XceedEncryptionLib.XceedEncryption();
encrypt.License( "CRY11-GRT9A-J7X75-BNAA" );
XceedEncryptionLib.XceedRSAEncryptionMethod rsa = new XceedEncryptionLib.XceedRSAEncryptionMethod();
object encryptedData = ( object )"";
try
{
rsa.set_PublicKey( ref pubkey );
encrypt.EncryptionMethod = rsa;
object data = ( object )message;
encryptedData = encrypt.Encrypt( ref data, true );
encryptKey = encrypt.ToString( ref encryptedData );
}
catch( System.Runtime.InteropServices.COMException except )
{
Console.WriteLine( except.ToString() );
}
return Convert.ToBase64String( ( byte[] )encryptedData );
}
private string decryptFunctionStringOnly( string message )
{
XceedEncryptionLib.XceedEncryption encrypt = new XceedEncryptionLib.XceedEncryption();
encrypt.License( "CRY11-GRT9A-J7X75-BNAA" );
XceedEncryptionLib.XceedRSAEncryptionMethod rsa = new XceedEncryptionLib.XceedRSAEncryptionMethod();
string Output = "";
System.Text.UnicodeEncoding enc = new System.Text.UnicodeEncoding();
try
{
rsa.set_PublicKey( ref pubkey );
rsa.set_PrivateKey( ref prikey );
encrypt.EncryptionMethod = rsa;
object encryptedData = ( object )Convert.FromBase64String( message );//enc.GetBytes( message );
Output = enc.GetString( ( byte[] )encrypt.Decrypt( ref encryptedData, true ) );
}
catch( System.Runtime.InteropServices.COMException except )
{
Console.WriteLine( except.ToString() );
}
return ( string )Output;
}
</code>
Charles Bérubé-Rémillard
Technical Support
Xceed Software Inc.