Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want the corresponding code in vb.net with asp.net. below is the c# code. plese help me. i am new in asp.net
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);
    }
}
Posted
Updated 11-Dec-11 6:20am
v2
Comments
Abhinav S 11-Dec-11 12:21pm    
Pre tags added.

There are free online converters that you can use. Have a look at this link for example:
http://www.developerfusion.com/tools/convert/csharp-to-vb/[^]

Good luck!
 
Share this answer
 
Comments
Wonde Tadesse 11-Dec-11 15:50pm    
5+
Monjurul Habib 12-Dec-11 13:36pm    
5!
Have a look at this tool.Code Translation for .NET (C#<->VB.NET)[^]
 
Share this answer
 
v2
Comments
Monjurul Habib 12-Dec-11 13:36pm    
5!
Wonde Tadesse 12-Dec-11 18:02pm    
Thanks
Try
http://converter.telerik.com/[^]
to Convert online VB to C# or C# to VB
 
Share this answer
 
Comments
Wonde Tadesse 11-Dec-11 15:49pm    
5+
RaviRanjanKr 12-Dec-11 14:39pm    
Thanks :)
Monjurul Habib 12-Dec-11 13:36pm    
5!
RaviRanjanKr 12-Dec-11 14:39pm    
Thanks Monjurul :)
Hi
try the following

Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Security.Cryptography
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO
''' <summary>
''' Summary description for Cryptography
''' </summary>
Public Class Cryptography
	Public Shared rsa As RSACryptoServiceProvider
	Public Shared Sub AssignParameter()
		Const  PROVIDER_RSA_FULL As Integer = 1
		Const  CONTAINER_NAME As String = "SpiderContainer"
		Dim cspParams As CspParameters
		cspParams = New CspParameters(PROVIDER_RSA_FULL)
		cspParams.KeyContainerName = CONTAINER_NAME
		cspParams.Flags = CspProviderFlags.UseMachineKeyStore
		cspParams.ProviderName = "Microsoft Strong Cryptographic Provider"
		rsa = New RSACryptoServiceProvider(cspParams)
	End Sub
	Public Shared Function EncryptData(data2Encrypt As String, path As String) As String
		AssignParameter()
		'StreamReader reader = new StreamReader(@"C:\Inetpub\wwwroot\dotnetspiderencryption\publickey.xml");
		Dim reader As New StreamReader(path & "/publickey.xml")
		Dim publicOnlyKeyXML As String = reader.ReadToEnd()
		rsa.FromXmlString(publicOnlyKeyXML)
		reader.Close()
		'read plaintext, encrypt it to ciphertext	
		Dim plainbytes As Byte() = System.Text.Encoding.UTF8.GetBytes(data2Encrypt)
		Dim cipherbytes As Byte() = rsa.Encrypt(plainbytes, False)
		Return Convert.ToBase64String(cipherbytes)
	End Function
	Public Shared Sub AssignNewKey(path As String)
		AssignParameter()
		'provide public and private RSA params	
		Dim writer As New StreamWriter(path & "/privatekey.xml")
		Dim publicPrivateKeyXML As String = rsa.ToXmlString(True)
		writer.Write(publicPrivateKeyXML)
		writer.Close()
		'provide public only RSA params	
		writer = New StreamWriter(path & "/publickey.xml")
		Dim publicOnlyKeyXML As String = rsa.ToXmlString(False)
		writer.Write(publicOnlyKeyXML)
		writer.Close()
	End Sub
	Public Shared Function DecryptData(data2Decrypt As String, path As String) As String
		AssignParameter()
		Dim getpassword As Byte() = Convert.FromBase64String(data2Decrypt)
		Dim reader As New StreamReader(path & "/privatekey.xml")
		Dim publicPrivateKeyXML As String = reader.ReadToEnd()
		rsa.FromXmlString(publicPrivateKeyXML)
		reader.Close()
		'read ciphertext, decrypt it to plaintext	
		Dim plain As Byte() = rsa.Decrypt(getpassword, False)
		Return System.Text.Encoding.UTF8.GetString(plain)
	End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900