Re: Database Connectivity
- From: "Miro" <mironagy@xxxxxxxxxx>
- Date: Mon, 25 Sep 2006 15:27:44 -0400
Looks like something simillar to what I wrote....... NOT!!! :-)
Thanks... I will be going back to the old code sometime at the end of this
week or early next week once I finish something up what Im doing now and
give her a run. I gotta fix up my Icon/Graphic mess I have created myself.
I will probably have a comment somewhere in there
'Encrypt data 128 bits - it just works ;-)
Thanks agian,
Miro
"Izzy" <israel.richner@xxxxxxxxx> wrote in message
news:1159190558.498861.156470@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Here it is, I have no idea how it works, but it works great. I use it
to encrypt passwords stored in an access file.
To call it:
'This will encrypt a value
Variable = EncryptString128Bit(txt_Password.Text, EncryptionKey)
'This will decrypt a value
Variable = DecryptString128Bit([Password stored in DB goes here],
EncryptionKey)
Have fun,
Izzy
****************************************************************************
Imports System.Security.Cryptography
Imports System.Text
Module mod_Globals
Public EncryptionKey As String = "justsomewordstobeusedasacryptionkey"
Public Function EncryptString128Bit(ByVal vstrTextToBeEncrypted As
String, ByVal vstrEncryptionKey As String) As String
Dim bytValue() As Byte
Dim bytKey() As Byte
Dim bytEncoded() As Byte
Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255,
91, 45, 78, 14, 211, 22, 62}
Dim intLength As Integer
Dim intRemaining As Integer
Dim objMemoryStream As New MemoryStream
Dim objCryptoStream As CryptoStream
Dim objRijndaelManaged As RijndaelManaged
vstrTextToBeEncrypted =
StripNullCharacters(vstrTextToBeEncrypted)
bytValue =
Encoding.ASCII.GetBytes(vstrTextToBeEncrypted.ToCharArray)
intLength = Len(vstrEncryptionKey)
If intLength >= 32 Then
vstrEncryptionKey = Strings.Left(vstrEncryptionKey, 32)
Else
intLength = Len(vstrEncryptionKey)
intRemaining = 32 - intLength
vstrEncryptionKey = vstrEncryptionKey &
Strings.StrDup(intRemaining, "X")
End If
bytKey = Encoding.ASCII.GetBytes(vstrEncryptionKey.ToCharArray)
objRijndaelManaged = New RijndaelManaged
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 Convert.ToBase64String(bytEncoded)
End Function
Public Function DecryptString128Bit(ByVal vstrStringToBeDecrypted
As String, ByVal vstrDecryptionKey As String) As String
Dim bytDataToBeDecrypted() As Byte
Dim bytTemp() As Byte
Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255,
91, 45, 78, 14, 211, 22, 62}
Dim objRijndaelManaged As New RijndaelManaged
Dim objMemoryStream As 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
bytDataToBeDecrypted =
Convert.FromBase64String(vstrStringToBeDecrypted)
intLength = Len(vstrDecryptionKey)
If intLength >= 32 Then
vstrDecryptionKey = Strings.Left(vstrDecryptionKey, 32)
Else
intLength = Len(vstrDecryptionKey)
intRemaining = 32 - intLength
vstrDecryptionKey = vstrDecryptionKey &
Strings.StrDup(intRemaining, "X")
End If
bytDecryptionKey =
Encoding.ASCII.GetBytes(vstrDecryptionKey.ToCharArray)
ReDim bytTemp(bytDataToBeDecrypted.Length)
objMemoryStream = New MemoryStream(bytDataToBeDecrypted)
Try
objCryptoStream = New CryptoStream(objMemoryStream,
objRijndaelManaged.CreateDecryptor(bytDecryptionKey, bytIV),
CryptoStreamMode.Read)
objCryptoStream.Read(bytTemp, 0, bytTemp.Length)
objCryptoStream.FlushFinalBlock()
objMemoryStream.Close()
objCryptoStream.Close()
Catch
End Try
Return StripNullCharacters(Encoding.ASCII.GetString(bytTemp))
End Function
Public 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
End Module
****************************************************************************************
Miro wrote:
Thanks Izzy,
Dont look too hard for it,
I created my own "encryption array" and flush out certain chars with
others
depending on what value it links up to
with my encryption table.
Just wondering if there is an easier way. :)
I have a work around though.
Example ( and keep in mind that i have all the alphabet here )
EncryptionKey1 = { A, C, B, D, E, .... }
EncryptionKey2 = { C, D, B, A, E, .... ) 'includes all letters upper and
lower, and also some ascii chars.
and so on ..
Basically if I have a Password that allows for a length of 10. I
actually
store the database as an 11 char field and the first
char is a 123...
If I have the #2 stored in that field and and A was in the pword, then it
gets replaced with a letter C.
If its a B, a D gets put in its spot, and an E, ( in this case stays as
an
E )
I select a random number from 1 to how many encryption keys i have and
thats
the one i use to encrypt it.
Whenever I have to write to the database and I de-crypt it, I re-encrypt
it
with a different Encryption key.
Its simple, and with 30 encryption keys, it does its job.
Any letters that are not in the Encryption key or something like a
special
char, doesnt get convrted and stays as is.
M.
"Izzy" <israel.richner@xxxxxxxxx> wrote in message
news:1159053014.114809.148840@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Will post the encryption code on Monday, I don't remember who wrote,
but it works great. I use it to store passwords in an access table.
Check back Monday after 8AM Central Time.
Izzy
Miro wrote:
Izzy,
Id be interested in that 128 bit encryption - just to see how you do
it. -
If you wouldnt mind.
Ivan,
I am in the same boat you are, just started learning vb.net
My code is probably all over the place. I started one step behind and
create my access table with adox.
There isnt much info out there. If you need something like that, let
me
know. I found some good examples out there,
but were super hard to find.
Miro
"Izzy" <israel.richner@xxxxxxxxx> wrote in message
news:1158951171.143852.231990@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Something to remember is when you destroy an object the memory
doesn't
get released back to the OS instantly. It's not until the GC
(garbage
collector) comes along and cleans up any unused resources.
I've had instances where I had to tell the garbage collecter to run
manually: gc.Collect()
You'll need that at some point.
Additionally Seth was right, passwords should be encrypted when
stored
in an Access file. I have a routine for that too, it will encrypt it
with 128bit encryption then store it in the Access file. I didn't
write
and I can't remember who did.
But if you want it I'll post it for you.
Izzy
Ivan Weiss wrote:
Izzy, another question.
Wouldnt you need to destroy the object or dispose of it or anything
after utilizing? Or will all resources be freed and database
connection
be closed automatically once the functions run?
-Ivan
*** Sent via Developersdex http://www.developersdex.com ***
.
- References:
- Re: Database Connectivity
- From: Ivan Weiss
- Re: Database Connectivity
- From: Ivan Weiss
- Re: Database Connectivity
- From: Izzy
- Re: Database Connectivity
- From: Miro
- Re: Database Connectivity
- From: Izzy
- Re: Database Connectivity
- From: Miro
- Re: Database Connectivity
- From: Izzy
- Re: Database Connectivity
- Prev by Date: Re: Why is PeekChar causing a problem?
- Next by Date: Re: UTF8 Encoding and MP3 tags
- Previous by thread: Re: Database Connectivity
- Next by thread: Re: Database Connectivity
- Index(es):
Relevant Pages
|