78f078ded4ae388e490c240c038c7f28

This is a simple example of Cryptography Class using RSA algorithm and Base64.
I did this in my notepad and I have not compiled to test the sintax. :)

using System.Security.Cryptography;

public class CryptographyRSA()
{
    private string PrivateKey = "<RSAKeyValue>YOUR_RSA_KEY</RSAKeyValue>";
    private string PublicKey = "<RSAKeyValue>YOUR_RSA_KEY</RSAKeyValue>";

    public CryptographyRSA(){}

    public string EncryptText(string strToEncript)
    {
        byte[] bytes = new UnicodeEncoding().GetBytes(strToEncript));

        RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
        provider.FromXmlString(PublicKey);

        byte[] inArray = provider.Encrypt(bytes, false);
        
        provider = null;
        
        return Convert.ToBase64String(inArray);
    }

    public string DecryptText(string strToDecript)
    {
        UnicodeEncoding encoding = new UnicodeEncoding();

        RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
        RSACryptoServiceProvider.UseMachineKeyStore = false;

        provider.FromXmlString(PrivateKey);

        byte[] rgb = Convert.FromBase64String(strToDecript);
        byte[] bytes = provider.Decrypt(rgb, false);

        provider = null;

        return encoding.GetString(bytes);
    }
    
}

Refactorings

No refactoring yet !

22e33503870d8e20493c4dd6b2f9767f

Rik Hemsley

August 30, 2008, August 30, 2008 18:40, permalink

No rating. Login to rate!

So what are you hoping for help with? Compiling? Spell checking?

78f078ded4ae388e490c240c038c7f28

acidmind

September 1, 2008, September 01, 2008 13:24, permalink

No rating. Login to rate!

Hello Rik...
Sorry... this code is working good, but this is the best way to do the Encryption/Decryption?

Your refactoring





Format Copy from initial code

or Cancel