Click here to Skip to main content
16,008,075 members
Home / Discussions / Visual Basic
   

Visual Basic

 
Generaladd numbers from listbox to a textbox Pin
Golfmaltais27-Oct-02 4:17
sussGolfmaltais27-Oct-02 4:17 
GeneralRe: add numbers from listbox to a textbox Pin
Andy H27-Oct-02 6:38
Andy H27-Oct-02 6:38 
GeneralVS.Net Collection Editor Pin
zorifila27-Oct-02 2:42
zorifila27-Oct-02 2:42 
GeneralRead Summary Information out of WordFiles Pin
mkrelli27-Oct-02 1:20
mkrelli27-Oct-02 1:20 
GeneralA question of a newbie Pin
smartnose26-Oct-02 15:55
smartnose26-Oct-02 15:55 
GeneralRe: A question of a newbie Pin
David Stone26-Oct-02 16:19
sitebuilderDavid Stone26-Oct-02 16:19 
GeneralRe: A question of a newbie Pin
Daniel Turini27-Oct-02 2:51
Daniel Turini27-Oct-02 2:51 
GeneralVB.Net Encryption Class Pin
JeromeH25-Oct-02 14:00
JeromeH25-Oct-02 14:00 
After many hours of battling with others sample code and sifting through countless message board posts I believe I have managed to come up with a fairly functional and stable encryption and decryption class that utilises multiple encryption methods.

The code is shown below and a sample call would look something like
Dim Crypto As New Crypto(Crypto.Providers.DES)
Dim Encrypted as string = Crypto.Encrypt("Test String", "Keystring")
Dim Decrypted as string = Crypto.Decrypt(Encrypted , "Keystring")

‘Crypto class

Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

'SymmCrypto is a wrapper of System.Security.Cryptography.SymmetricAlgorithm classes
'and simplifies the interface. It supports customized SymmetricAlgorithm as well.
'Original Code from Frank Fang
'Revised by Jerome Howard to remove Bad Data errors, create seperate CryptoIV and
'use the maximum legal keysize for each encryption algorithm

Public Class Crypto
'256 Bit IV Key that is truncated when a smaller keys are required
Private bytIV() As Byte = _
{12, 241, 10, 21, 90, 74, 11, 39, 9, 91, 45, 78, 189, 211, 133, 62, 121, 22, 101, 34, 90, 74, 121, 39, 93, 9, 45, 78, 1, 211, 33, 162}

'Supported .Net intrinsic SymmetricAlgorithm classes.
Public Enum Providers
DES
RC2
Rijndael
End Enum

Private _CryptoService As SymmetricAlgorithm

'Constructor for using an intrinsic .Net SymmetricAlgorithm class.
Public Sub New(ByVal NetSelected As Providers)
Select Case NetSelected
Case Providers.DES
_CryptoService = New DESCryptoServiceProvider()
Case Providers.RC2
_CryptoService = New RC2CryptoServiceProvider()
Case Providers.Rijndael
_CryptoService = New RijndaelManaged()
End Select
End Sub

'Constructor for using a customized SymmetricAlgorithm class.
Public Sub New(ByVal ServiceProvider As SymmetricAlgorithm)
_CryptoService = ServiceProvider
End Sub

'Depending on the legal key size limitations of a specific CryptoService provider
'and length of the private key provided, padding the secret key with a character
'or triming it to meet the legal size of the algorithm.
Private Function GetLegalKey(ByVal Key As String) As Byte()
'key sizes are in bits
Dim sTemp As String
If (_CryptoService.LegalKeySizes.Length > 0) Then
Dim maxSize As Integer = _CryptoService.LegalKeySizes(0).MaxSize
If Key.Length * 8 > maxSize Then
sTemp = Key.Substring(0, (maxSize / 8))
ReDim Preserve bytIV((maxSize / 8) - 1)
Else
Dim moreSize As Integer = _CryptoService.LegalKeySizes(0).MinSize
Do While (Key.Length * 8 > moreSize)
moreSize += _CryptoService.LegalKeySizes(0).SkipSize
Loop
ReDim Preserve bytIV(moreSize / 8)
sTemp = Key.PadRight(moreSize / 8, "X")
End If
Else
sTemp = Key
ReDim Preserve bytIV(Key.Length)
End If

'convert the secret key to byte array
Return ASCIIEncoding.ASCII.GetBytes(sTemp)
End Function

Public Function Encrypt(ByVal Source As String, ByVal Key As String) As String
Dim bytIn As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(System.Web.HttpUtility.UrlEncode(Source))
Dim ms As MemoryStream = New MemoryStream()

'set the keys
_CryptoService.Key = GetLegalKey(Key)
_CryptoService.IV = bytIV

'create an Encryptor from the Provider Service instance
Dim encrypto As ICryptoTransform = _CryptoService.CreateEncryptor()

'create Crypto Stream that transforms a stream using the encryption
Dim cs As CryptoStream = New CryptoStream(ms, encrypto, CryptoStreamMode.Write)

'write out encrypted content into MemoryStream
cs.Write(bytIn, 0, bytIn.Length)
cs.FlushFinalBlock()
cs.Close()
Dim bytOut() As Byte = ms.ToArray()
ms.Close()

Return Convert.ToBase64String(bytOut) 'convert into Base64 so that the result can be used in xml
End Function

Public Function Decrypt(ByVal Source As String, ByVal Key As String) As String
'convert from Base64 to binary
Dim bytIn As Byte() = System.Convert.FromBase64String(Source)
Dim ms As MemoryStream = New MemoryStream(bytIn)

Dim bytKey() As Byte = GetLegalKey(Key)
Dim bytTemp(bytIn.Length) As Byte

'set the private key
_CryptoService.Key = bytKey
_CryptoService.IV = bytIV

'create a Decryptor from the Provider Service instance
Dim encrypto As ICryptoTransform = _CryptoService.CreateDecryptor()

'create Crypto Stream that transforms a stream using the decryption
Dim cs As CryptoStream = New CryptoStream(ms, encrypto, CryptoStreamMode.Read)
Try
'read out the result from the Crypto Stream
cs.Read(bytTemp, 0, bytTemp.Length)

cs.FlushFinalBlock()
ms.Close()
cs.Close()
Catch
End Try
Return System.Web.HttpUtility.UrlDecode(Encoding.ASCII.GetString(bytTemp))
End Function

End Class

QuestionHow to access Registry in VB.NET? Pin
ccoppin24-Oct-02 15:51
ccoppin24-Oct-02 15:51 
AnswerRe: How to access Registry in VB.NET? Pin
Andy H25-Oct-02 0:25
Andy H25-Oct-02 0:25 
GeneralRe: How to access Registry in VB.NET? Pin
Nic Rowan25-Oct-02 0:49
Nic Rowan25-Oct-02 0:49 
GeneralRe: How to access Registry in VB.NET? Pin
Nish Nishant26-Oct-02 16:22
sitebuilderNish Nishant26-Oct-02 16:22 
Questionhow to do input validation in VB.NET? Pin
drmzunlimited24-Oct-02 10:38
drmzunlimited24-Oct-02 10:38 
AnswerRe: how to do input validation in VB.NET? Pin
Nick Parker25-Oct-02 5:32
protectorNick Parker25-Oct-02 5:32 
QuestionHow do I display a video file on a form? Pin
James Williams24-Oct-02 8:44
James Williams24-Oct-02 8:44 
AnswerRe: How do I display a video file on a form? Pin
Jerome Conus24-Oct-02 23:14
Jerome Conus24-Oct-02 23:14 
QuestionInterpret Enter as Tab? Pin
dazinith24-Oct-02 3:54
dazinith24-Oct-02 3:54 
AnswerRe: Interpret Enter as Tab? Pin
Anonymous24-Oct-02 15:48
Anonymous24-Oct-02 15:48 
Generallow resource message Pin
Notorious SMC23-Oct-02 20:42
sussNotorious SMC23-Oct-02 20:42 
General[VB.NET] Drag & Drop multiselection on listview Pin
Sebastien Curutchet22-Oct-02 4:20
Sebastien Curutchet22-Oct-02 4:20 
GeneralHttpSendRequest Pin
Vipul Bhatt21-Oct-02 0:50
Vipul Bhatt21-Oct-02 0:50 
GeneralRe: HttpSendRequest Pin
Richard Deeming22-Oct-02 22:24
mveRichard Deeming22-Oct-02 22:24 
GeneralRetrieving exchange users information Pin
Anonymous19-Oct-02 1:58
Anonymous19-Oct-02 1:58 
GeneralPrinter status Pin
Zulfikar Ali18-Oct-02 11:47
Zulfikar Ali18-Oct-02 11:47 
Generalregistering crystal reports in vb.net Pin
devil66618-Oct-02 11:11
devil66618-Oct-02 11:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.