473,383 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 1142

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 =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.