merlish

merlish

  • NA
  • 59
  • 62.5k

Password encryption

Dec 11 2011 11:52 AM
i want corresponding code in vb.net. plese help me....the follwing is the code

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Security.Cryptography;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;


/// <summary>
/// Summary description for Cryptography
/// </summary>
public class Cryptography
{
    public static RSACryptoServiceProvider rsa;
    public static void AssignParameter()
    {
        const int PROVIDER_RSA_FULL = 1;
        const string CONTAINER_NAME = "SpiderContainer";
        CspParameters cspParams;
        cspParams = new CspParameters(PROVIDER_RSA_FULL);
        cspParams.KeyContainerName = CONTAINER_NAME;
        cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
        cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
        rsa = new RSACryptoServiceProvider(cspParams);
    }
    public static string EncryptData(string data2Encrypt, string path)
    {
        AssignParameter();
        //StreamReader reader = new StreamReader(@"C:\Inetpub\wwwroot\dotnetspiderencryption\publickey.xml");
        StreamReader reader = new StreamReader( path + "/publickey.xml");
        string publicOnlyKeyXML = reader.ReadToEnd();
        rsa.FromXmlString(publicOnlyKeyXML);
        reader.Close();
        //read plaintext, encrypt it to ciphertext
        byte[] plainbytes =System.Text.Encoding.UTF8.GetBytes(data2Encrypt);
        byte[] cipherbytes = rsa.Encrypt(plainbytes, false);
        return Convert.ToBase64String(cipherbytes);
    }
    public static void AssignNewKey(string path)
    {
        AssignParameter();
        //provide public and private RSA params
        StreamWriter writer = new StreamWriter(path + "/privatekey.xml");
        string publicPrivateKeyXML = rsa.ToXmlString(true);
        writer.Write(publicPrivateKeyXML);
        writer.Close();
        //provide public only RSA params
        writer = new StreamWriter(path + "/publickey.xml");
        string publicOnlyKeyXML = rsa.ToXmlString(false);
        writer.Write(publicOnlyKeyXML);
        writer.Close();
    }
    public static string DecryptData(string data2Decrypt, string path)
    {
        AssignParameter();
        byte[] getpassword = Convert.FromBase64String(data2Decrypt);
        StreamReader reader = new StreamReader(path + "/privatekey.xml");
        string publicPrivateKeyXML = reader.ReadToEnd();
        rsa.FromXmlString(publicPrivateKeyXML);
        reader.Close();
        //read ciphertext, decrypt it to plaintext
        byte[] plain =rsa.Decrypt(getpassword,false);
        return System.Text.Encoding.UTF8.GetString(plain);
    }
}




Answers (2)