I found this encryption class in the newsgroups and wanted to update this
code so that the encryption is 512, not 256.... What has to be done to do
this?
Imports System
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Public Class clsEncryption
Public Shared Function EncryptRijndael(ByVal strTextToBeEncrypted As
String, ByVal strEncryptionKey As String, ByVal MyBit1 As Int16, ByVal
MyBit2 As Int16, ByVal MyBit3 As Int16, ByVal MyBit4 As Int16, ByVal MyBit5
As Int16, ByVal MyBit6 As Int16, ByVal MyBit7 As Int16, ByVal MyBit8 As
Int16, ByVal MyBit9 As Int16, ByVal MyBit10 As Int16, ByVal MyBit11 As
Int16, ByVal MyBit12 As Int16, ByVal MyBit13 As Int16, ByVal MyBit14 As
Int16, ByVal MyBit15 As Int16, ByVal MyBit16 As Int16) As String
Dim myBit As Int16
Dim bytValue() As Byte
Dim bytKey() As Byte
Dim bytEncoded() As Byte
Dim bytIV() As Byte = {MyBit1, MyBit2, MyBit3, MyBit4, MyBit5,
MyBit6, MyBit7, MyBit8, MyBit9, MyBit10, MyBit11, MyBit12, MyBit13, MyBit14,
MyBit15, MyBit16}
Dim intLength As Integer
Dim intRemaining As Integer
Dim objMemoryStream As New System.IO.MemoryStream
Dim objCryptoStream As CryptoStream
Dim objRijndaelManaged As RijndaelManaged
'
************************************************** ********************
' ****** Strip any null character from string to be encrypted
******
'
************************************************** ********************
strTextToBeEncrypted = StripNullCharacters(strTextToBeEncrypted)
'
************************************************** ********************
' ****** Value must be within ASCII range (i.e., no DBCS chars)
******
'
************************************************** ********************
bytValue = Encoding.ASCII.GetBytes(strTextToBeEncrypted.ToCha rArray)
intLength = Len(strEncryptionKey)
'
************************************************** ******************
' ****** Encryption Key must be 256 bits long (32 bytes)
******
' ****** If it is longer than 32 bytes it will be truncated.
******
' ****** If it is shorter than 32 bytes it will be padded
******
' ****** with upper-case Xs.
******
'
************************************************** ******************
If intLength >= 32 Then
strEncryptionKey = Strings.Left(strEncryptionKey, 32)
Else
intLength = Len(strEncryptionKey)
intRemaining = 32 - intLength
strEncryptionKey = strEncryptionKey &
Strings.StrDup(intRemaining, "X")
End If
bytKey = Encoding.ASCII.GetBytes(strEncryptionKey.ToCharArr ay)
objRijndaelManaged = New RijndaelManaged
'
************************************************** *********************
' ****** Create the encryptor and write value to it after it is
******
' ****** converted into a byte array
******
'
************************************************** *********************
Try
objCryptoStream = New CryptoStream(objMemoryStream, _
objRijndaelManaged.CreateEncryptor(bytKey, bytIV), _
CryptoStreamMode.Write)
objCryptoStream.Write(bytValue, 0, bytValue.Length)
objCryptoStream.FlushFinalBlock()
bytEncoded = objMemoryStream.ToArray
objMemoryStream.Close()
objCryptoStream.Close()
Catch
End Try
'
************************************************** *********************
' ****** Return encryptes value (converted from byte Array to
******
' ****** a base64 string). Base64 is MIME encoding)
******
'
************************************************** *********************
Return Convert.ToBase64String(bytEncoded)
End Function
Private Shared Function StripNullCharacters(ByVal vstrStringWithNulls As
String) As String
Dim intPosition As Integer
Dim strStringWithOutNulls As String
intPosition = 1
strStringWithOutNulls = vstrStringWithNulls
Do While intPosition > 0
intPosition = InStr(intPosition, vstrStringWithNulls,
vbNullChar)
If intPosition > 0 Then
strStringWithOutNulls = Left$(strStringWithOutNulls,
intPosition - 1) & _
Right$(strStringWithOutNulls,
Len(strStringWithOutNulls) - intPosition)
End If
If intPosition > strStringWithOutNulls.Length Then
Exit Do
End If
Loop
Return strStringWithOutNulls
End Function
Public Shared Function DecryptRijndael(ByVal strTextToBeDecrypted As
String, ByVal strEncryptionKey As String, ByVal MyBit1 As Int16, ByVal
MyBit2 As Int16, ByVal MyBit3 As Int16, ByVal MyBit4 As Int16, ByVal MyBit5
As Int16, ByVal MyBit6 As Int16, ByVal MyBit7 As Int16, ByVal MyBit8 As
Int16, ByVal MyBit9 As Int16, ByVal MyBit10 As Int16, ByVal MyBit11 As
Int16, ByVal MyBit12 As Int16, ByVal MyBit13 As Int16, ByVal MyBit14 As
Int16, ByVal MyBit15 As Int16, ByVal MyBit16 As Int16) As String
Dim bytDataToBeDecrypted() As Byte
Dim bytTemp() As Byte
Dim bytIV() As Byte = {MyBit1, MyBit2, MyBit3, MyBit4, MyBit5,
MyBit6, MyBit7, MyBit8, MyBit9, MyBit10, MyBit11, MyBit12, MyBit13, MyBit14,
MyBit15, MyBit16}
Dim objRijndaelManaged As New RijndaelManaged
Dim objMemoryStream As New System.IO.MemoryStream
Dim objCryptoStream As CryptoStream
Dim bytDecryptionKey() As Byte
Dim intLength As Integer
Dim intRemaining As Integer
Dim intCtr As Integer
Dim strReturnString As String = String.Empty
Dim achrCharacterArray() As Char
Dim intIndex As Integer
'
************************************************** ***************
' ****** Convert base64 encrypted value to byte array
******
'
************************************************** ***************
bytDataToBeDecrypted =
Convert.FromBase64String(strTextToBeDecrypted)
'
************************************************** ******************
' ****** Encryption Key must be 256 bits long (32 bytes)
******
' ****** If it is longer than 32 bytes it will be truncated.
******
' ****** If it is shorter than 32 bytes it will be padded
******
' ****** with upper-case Xs.
******
'
************************************************** ******************
intLength = Len(strEncryptionKey)
If intLength >= 32 Then
strEncryptionKey = Strings.Left(strEncryptionKey, 32)
Else
intLength = Len(strEncryptionKey)
intRemaining = 32 - intLength
strEncryptionKey = strEncryptionKey &
Strings.StrDup(intRemaining, "X")
End If
bytDecryptionKey =
Encoding.ASCII.GetBytes(strEncryptionKey.ToCharArr ay)
ReDim bytTemp(bytDataToBeDecrypted.Length)
objMemoryStream = New System.IO.MemoryStream(bytDataToBeDecrypted)
'
************************************************** *********************
' ****** Create the decryptor and write value to it after it is
******
' ****** converted into a byte array
******
'
************************************************** *********************
Try
objCryptoStream = New CryptoStream(objMemoryStream, _
objRijndaelManaged.CreateDecryptor(bytDecryptionKe y, bytIV),
_
CryptoStreamMode.Read)
objCryptoStream.Read(bytTemp, 0, bytTemp.Length)
objCryptoStream.FlushFinalBlock()
objMemoryStream.Close()
objCryptoStream.Close()
Catch
End Try
' *****************************************
' ****** Return decypted value ******
' *****************************************
Return StripNullCharacters(Encoding.ASCII.GetString(bytTe mp))
End Function