472,780 Members | 1,233 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

Encryption Class Update

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
Nov 21 '05 #1
0 1123

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ronald Evers | last post by:
Hey all, I want to store passwords in a postgresql database. Currently I use the MD5Password class below and I've been developing on windows. I ran into problems when running my application on...
14
by: Ray Cassick \(Home\) | last post by:
Ok, time to ask the question here.. I have been battling over this one for sometime now and just have to ask it. I have created a few classes that I use to act a security keys. These classes get...
3
by: Molly Gibson | last post by:
Hi all, I have recently installed Apache/1.3.28 + mod_auth_pgsql-0.9.12 (http://www.giuseppetanzilli.it/mod_auth_pgsql/) The only way I have been able to get it to successfully authenticate...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
12
by: Mitchell Vincent | last post by:
As the subject suggests, I'm looking for a compression and encryption component(s) for use with VB.NET. I would rather then be all managed code but will use ActiveX/COM if I have to.. Price is...
4
by: hohans | last post by:
Hi all, I have an encryption class that encrypts and decrypts password using TripleDESCryptoServiceProvider. It was written originally in framework 1.0 and been working fine. And those...
7
by: Mark Rae | last post by:
Hi, Picking your collective brains again, this time regarding the storage of the key used in symmetric encryption. Let's say you have a requirement to add encryption to a C# project, so you...
11
by: John Williams | last post by:
I've written a simple program to do XOR encryption as my first foray into understanding how encryption works. The code compiles fine, however it segmentation faults on every run. using gdb to...
4
by: Fritjolf | last post by:
Hi. I've got a strange problem... I've made a simple program to test encryption/decryption. I use Rijndael encryption and here are the most important properties. RijndaelManaged cipher =...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.