alphonso

Hi,

I wanted to create a DLL using VB Orcas, and use it in C# Orcas. So, I went to select Class Library which is used for making DLLs, and I coded the code there. Here's the code:

Option Strict On

Option Explicit On

Imports System.Text

Imports System.Security.Cryptography

Public Class EncryptionAndPassphraseCreation

#Region "Enumeration Declarations"

Enum SymmetricEncryptionAlgorithmChoices

AES

DES

RC2

TripleDes

End Enum

Enum PassPhraseIntensityChoices

Level1

Level2

Level3

End Enum

#End Region

''' <summary>COMMENT: The variable used to choose the SymmetricEncryptionAlgorithmChoices</summary>

Private SetEncryptionAlgorithm As SymmetricEncryptionAlgorithmChoices

''' <summary>

''' COMMENT: Optional property; this must be set to determine whether to produce a random

''' passphrase of level 1, level 2, or level 3 intensity. Level 1 produces a length of

''' 200 random alphabetical and numerical characters as a passphrase, level 2 produces

''' 500, while level 3 produces 1000.

''' </summary>

Private PassPhraseIntensity As PassPhraseIntensityChoices

''' <summary>

''' COMMENT: Required property; this must be set to determine the symmetric algorithm to

''' be used for cryptographic purposes.

''' </summary>

Private Property _EncryptionAlgorithm() As SymmetricEncryptionAlgorithmChoices

Get

Return SetEncryptionAlgorithm

End Get

Set(ByVal value As SymmetricEncryptionAlgorithmChoices)

SetEncryptionAlgorithm = value

End Set

End Property

Private Property _PassPhraseIntensity() As PassPhraseIntensityChoices

Get

Return PassPhraseIntensity

End Get

Set(ByVal value As PassPhraseIntensityChoices)

PassPhraseIntensity = value

End Set

End Property

''' <summary>

''' COMMENT: Call this subroutine if you wish to create a symmetric key, provided you have

''' set the _EncryptionAlgorithm property

''' </summary>

''' <remarks></remarks>

Shared Function CreateSymmetricKey(ByVal passPhrase As String, ByVal Salt() As Byte, ByVal algorithmType As SymmetricEncryptionAlgorithmChoices) As String

Dim deriveBytes As New Rfc2898DeriveBytes(passPhrase, Salt)

Select Case algorithmType

Case SymmetricEncryptionAlgorithmChoices.AES

Dim algorithmTypeAES As New AesCryptoServiceProvider

Dim keySize As Integer = algorithmTypeAES.LegalKeySizes(0).MaxSize

Dim bytePassPhrase() As Byte = deriveBytes.GetBytes(keySize)

Return Convert.ToBase64String(bytePassPhrase)

Exit Function

Case SymmetricEncryptionAlgorithmChoices.DES

Dim algorithmTypeAES As New DESCryptoServiceProvider

Dim keySize As Integer = algorithmTypeAES.LegalKeySizes(0).MaxSize

Dim bytePassPhrase() As Byte = deriveBytes.GetBytes(keySize)

Return Convert.ToBase64String(bytePassPhrase)

Exit Function

Case SymmetricEncryptionAlgorithmChoices.RC2

Dim algorithmTypeAES As New RC2CryptoServiceProvider

Dim keySize As Integer = algorithmTypeAES.LegalKeySizes(0).MaxSize

Dim bytePassPhrase() As Byte = deriveBytes.GetBytes(keySize)

Return Convert.ToBase64String(bytePassPhrase)

Exit Function

Case SymmetricEncryptionAlgorithmChoices.TripleDes

Dim algorithmTypeAES As New TripleDESCryptoServiceProvider

Dim keySize As Integer = algorithmTypeAES.LegalKeySizes(0).MaxSize

Dim bytePassPhrase() As Byte = deriveBytes.GetBytes(keySize)

Return Convert.ToBase64String(bytePassPhrase)

Exit Function

End Select

Return "An error has occurred"

End Function

Shared Function CreateRandomPassphrase(ByVal intensityLevel As PassPhraseIntensityChoices) As String

Dim appender As New StringBuilder

Dim KeyGen As RandomKeyGenerator

Dim NumKeys As Integer

Dim i_Keys As Integer

Dim RandomKey As String = ""

NumKeys = 1

KeyGen = New RandomKeyGenerator

KeyGen.KeyLetters = "abcdefghijklmnopqrstuvwxyz"

KeyGen.KeyNumbers = "0123456789"

Select Case intensityLevel

Case PassPhraseIntensityChoices.Level1

KeyGen.KeyChars = 200

Case PassPhraseIntensityChoices.Level2

KeyGen.KeyChars = 500

Case PassPhraseIntensityChoices.Level3

KeyGen.KeyChars = 1000

End Select

For i_Keys = 1 To NumKeys

RandomKey = KeyGen.Generate()

appender.Append(RandomKey)

Next

Return RandomKey

End Function

End Class

Public Class RandomKeyGenerator

Dim Key_Letters As String

Dim Key_Numbers As String

Dim Key_Chars As Integer

Dim LettersArray As Char()

Dim NumbersArray As Char()

''' <date>27072005</date><time>071924</time>

''' <type>property</type>

''' <summary>

''' WRITE ONLY PROPERTY. HAS TO BE SET BEFORE CALLING GENERATE()

''' </summary>

Protected Friend WriteOnly Property KeyLetters() As String

Set(ByVal Value As String)

Key_Letters = Value

End Set

End Property

''' <date>27072005</date><time>071924</time>

''' <type>property</type>

''' <summary>

''' WRITE ONLY PROPERTY. HAS TO BE SET BEFORE CALLING GENERATE()

''' </summary>

Protected Friend WriteOnly Property KeyNumbers() As String

Set(ByVal Value As String)

Key_Numbers = Value

End Set

End Property

''' <date>27072005</date><time>071924</time>

''' <type>property</type>

''' <summary>

''' WRITE ONLY PROPERTY. HAS TO BE SET BEFORE CALLING GENERATE()

''' </summary>

Protected Friend WriteOnly Property KeyChars() As Integer

Set(ByVal Value As Integer)

Key_Chars = Value

End Set

End Property

''' <date>27072005</date><time>072344</time>

''' <type>function</type>

''' <summary>

''' GENERATES A <B style="COLOR: black; BACKGROUND-COLOR: #99ff99">RANDOM</B> STRING OF LETTERS AND NUMBERS.

''' LETTERS CAN BE RANDOMLY CAPITAL OR SMALL.

''' </summary>

''' <returns type="String">RETURNS THE

''' RANDOMLY GENERATED KEY</returns>

Function Generate() As String

Dim i_key As Integer

Dim Random1 As Single

Dim arrIndex As Int16

Dim sb As New StringBuilder

Dim RandomLetter As String

LettersArray = Key_Letters.ToCharArray

NumbersArray = Key_Numbers.ToCharArray

For i_key = 1 To Key_Chars

Randomize()

Random1 = Rnd()

arrIndex = -1

If (CType(Random1 * 111, Integer)) Mod 2 = 0 Then

Do While arrIndex < 0

arrIndex = _

Convert.ToInt16(LettersArray.GetUpperBound(0) _

* Random1)

Loop

RandomLetter = LettersArray(arrIndex)

If (CType(arrIndex * Random1 * 99, Integer)) Mod 2 <> 0 Then

RandomLetter = LettersArray(arrIndex).ToString

RandomLetter = RandomLetter.ToUpper

End If

sb.Append(RandomLetter)

Else

Do While arrIndex < 0

arrIndex = _

Convert.ToInt16(NumbersArray.GetUpperBound(0) _

* Random1)

Loop

sb.Append(NumbersArray(arrIndex))

End If

Next

Return sb.ToString

End Function

End Class

When I save this project, the DLL isn't there. I tried the csc process in command prompt, but it says that csc is not recognized as a command. What am I doing wrong Smile


Re: Visual Basic Express Edition Create dll in VB, consume in C#

spotty

Class Libraries create in VB express are .NET indepenent and can be called between VB and C# without a problem.

Ensure that you have both products installed as VB express doesnt install C# express. When you build you VB DLL as above there should be a bin sub folder in the class library folder. When you build a dll should be created in a sub folder bin\release or bin\debug.

When you create your C# project you simply need to add a reference to the DLL or the VB project and the class is accessible. Some thing to consider is the root namespace of the VB class (You can check on the VB project properties) - so something to consider when you are trying to use the class and methods.

Tried and testing in 2005 and Orcas and guaranteed to work.





Re: Visual Basic Express Edition Create dll in VB, consume in C#

alphonso

Thanks, Spotty, I can see the DLLs now. Smile