From: Eric on
Hi,

For my applications I use the following 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}

Private Key As String = "MyKey"

'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) As String
Dim bytIn As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(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) As String
'convert from Base64 to binary

Try
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

'replace 'zero'-bytes with spaces (byte =32)
For ab As Integer = 0 To bytTemp.Length - 1
If bytTemp(ab) = 0 Then bytTemp(ab) = 32
Next

Return Encoding.ASCII.GetString(bytTemp).Trim
Catch ex As Exception
Return ""
End Try


End Function

End Class


Most of the time this works as it should, but sometimes it encodes a string
and the gives an error on decoding it.

The error message is: Invalid length for a Base-64 char array.

Why do I get his error?
What is wrong in the code?

please help.

rg,
Eric
From: Leo on
Eric has brought this to us :
> Hi,
>
> For my applications I use the following 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}
>
> Private Key As String = "MyKey"
>
> '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) As String
> Dim bytIn As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(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) As String
> 'convert from Base64 to binary
>
> Try
> 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
>
> 'replace 'zero'-bytes with spaces (byte =32)
> For ab As Integer = 0 To bytTemp.Length - 1
> If bytTemp(ab) = 0 Then bytTemp(ab) = 32
> Next
>
> Return Encoding.ASCII.GetString(bytTemp).Trim
> Catch ex As Exception
> Return ""
> End Try
>
>
> End Function
>
> End Class
>
>
> Most of the time this works as it should, but sometimes it encodes a string
> and the gives an error on decoding it.
>
> The error message is: Invalid length for a Base-64 char array.
>
> Why do I get his error?
> What is wrong in the code?
>
> please help.
>
> rg,
> Eric

Please try the forums as this is a classic VB group only.

--
ClassicVB Users Regroup! comp.lang.basic.visual.misc
Free usenet access at http://www.eternal-september.org


From: Mike Williams on
"Eric" <Eric(a)discussions.microsoft.com> wrote in message
news:C82EC378-3C43-4A2B-94A9-E8427FF5FF7C(a)microsoft.com...

> For my applications I use the following class:
> Imports System.Security.Cryptography

You are in the wrong newsgroup. This group is for Classic Visual Basic, the
latest version of which is VB6. The product you appear to be using, despite
its deliberately misleading marketing name, is an imposter. Questions
relating to the imposter should be posted to one of the imposter's own
newsgroups. Micro$oft are currently in the process of attempting to close
them down and replace them with their own heavily policed comic book
advertising forums where people can post images to each other and say nice
things about Micro$oft, but I'm sure you will find that Micro$oft will fail
in that task. The microsoft.public.dotnet.languages.vb group is the place
you want to be.

Mike




From: Cor Ligthert[MVP] on
Eric,

This newsgroup is commonly used for non supported Visual Basic versions, for
supported newsgroups are no newsgroups anymore on the Microsoft servers
(although you can reach them with most providers like Google)

Try this forum, which is for Visual Basic language versions which have still
program language support from Microsoft (Visual Basic language version 7 to
10).

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/threads?page=1

Success

Cor

"Eric" <Eric(a)discussions.microsoft.com> wrote in message
news:C82EC378-3C43-4A2B-94A9-E8427FF5FF7C(a)microsoft.com...
> Hi,
>
> For my applications I use the following 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}
>
> Private Key As String = "MyKey"
>
> '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) As String
> Dim bytIn As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(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) As String
> 'convert from Base64 to binary
>
> Try
> 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
>
> 'replace 'zero'-bytes with spaces (byte =32)
> For ab As Integer = 0 To bytTemp.Length - 1
> If bytTemp(ab) = 0 Then bytTemp(ab) = 32
> Next
>
> Return Encoding.ASCII.GetString(bytTemp).Trim
> Catch ex As Exception
> Return ""
> End Try
>
>
> End Function
>
> End Class
>
>
> Most of the time this works as it should, but sometimes it encodes a
> string
> and the gives an error on decoding it.
>
> The error message is: Invalid length for a Base-64 char array.
>
> Why do I get his error?
> What is wrong in the code?
>
> please help.
>
> rg,
> Eric

From: Larry Serflaten on

"Leo" <ttdhead(a)gmail.com> wrote
> Eric has brought this to us :
> > Hi,

<lengthy quoted text snipped for brievity>

> > Why do I get his error?
> > What is wrong in the code?
> >
> > please help.
>
> Please try the forums as this is a classic VB group only.


Please trim your replies to just enough to indicate what you are
responding to. Quoting 100 lines for a one line reply is absurd.....

LFS