Anyone know how to Encrypt a String via 128+-bit Encryption?
Hey guys. Me one more time, except I am not sure if there is such thing in VB.NET via 128-bit Encryption or more. I only know of 64-bit, can someone please teach me? Here is my code for 64-bit:
Encryption:
Code:
Public Function EncryptXString(ByVal sData As String) As String
Dim mSHA As New SHA1Managed
Dim dString() As Byte = ASCIIEncoding.ASCII.GetBytes(sData)
Dim cString As String = Convert.ToBase64String(dString)
Convert.ToBase64String(mSHA.ComputeHash(Encoding.ASCII.GetBytes(sData)))
EncryptXString = cString
End Function
And Decryption:
Code:
Public Function FindXString(ByVal sData As String) As String
Dim dData() As Byte = Convert.FromBase64String(sData)
Dim dString As String = ASCIIEncoding.ASCII.GetString(dData)
FindXString = dString
End Function
Teach me... I want to know the truth, please.
Re: Anyone know how to Encrypt a String via 128+-bit Encryption?
You could try using AES,
Code:
Function AESEncrypit(ByVal PlainText As String, ByVal Password As String)
Dim salt As String = "xXEmoNinjaUnicornXx" 'Mostly a Second Password ^_^
Dim HashAlgorithm As String = "SHA1" 'Can be SHA1 or MD5
Dim PasswordIterations As String = 2
Dim InitialVector As String = "OFRna73m*aze01xY" 'This should be a string of 16 ASCII characters.
Dim KeySize As Integer = 256 'Can be 128, 192, or 256.
If (String.IsNullOrEmpty(PlainText)) Then
Return ""
Exit Function
End If
Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector)
Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt)
Dim PlainTextBytes As Byte() = Encoding.UTF8.GetBytes(PlainText)
Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations)
Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize / 8)
Dim SymmetricKey As RijndaelManaged = New RijndaelManaged()
SymmetricKey.Mode = CipherMode.CBC
Dim CipherTextBytes As Byte() = Nothing
Using Encryptor As ICryptoTransform = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes)
Using MemStream As New MemoryStream()
Using CryptoStream As New CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)
CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length)
CryptoStream.FlushFinalBlock()
CipherTextBytes = MemStream.ToArray()
MemStream.Close()
CryptoStream.Close()
End Using
End Using
End Using
SymmetricKey.Clear()
Return Convert.ToBase64String(CipherTextBytes)
End Function
To use this:
Code:
Dim aesEncrypted As String = AESEncrypit("TEXT TO ENCRYPT HERE", "PASSWORD-for-ENCRYPTION")
MessageBox.Show(aesEncrypted)
If you want decrypt function i'll type it out.
Re: Anyone know how to Encrypt a String via 128+-bit Encryption?
Sweet mother of pearl, nice code you just typed up for me! Really appreciated thanks a ton! I'd +rep you if I could
Re: Anyone know how to Encrypt a String via 128+-bit Encryption?
I am interested in using this encryption function...however some of the variables arn't declared. ie Encoding and PasswordDeriveBytes
I presume i need to implement some information.
Can I please have the full code to use this function?
thanks
Re: Anyone know how to Encrypt a String via 128+-bit Encryption?
Imports System.Security.Cryptography
http://msdn.microsoft.com/en-us/libr...ptography.aspx for more info
Remember: An encryption is not much of a use, if you can't decrypt it.
Re: Anyone know how to Encrypt a String via 128+-bit Encryption?
I remember a post not too long ago from someone who managed to make a compression routine that could make the file about 95% of its original size. He had worked on the method for ages and released the code. He stopped before making the decompression routine as he was too busy :).
Re: Anyone know how to Encrypt a String via 128+-bit Encryption?
Quote:
Originally Posted by
Radjesh Klauke
Remember: An encryption is not much of a use, if you can't decrypt it.
I have to disagree with you on this statement. There are many uses for irreversible encryption. One of the more common use of these one-way encryption algorithms is to encrypt a password.They are also use to compare files. Just Goolge for "MD5 usage" and you'll be hit by the millions... Consider they are just tools and therefore, you should pick the right tool for the right job.
Re: Anyone know how to Encrypt a String via 128+-bit Encryption?
Quote:
Originally Posted by
Grimfort
I remember a post not too long ago from someone who managed to make a compression routine that could make the file about 95% of its original size. He had worked on the method for ages and released the code. He stopped before making the decompression routine as he was too busy :).
Hahaha, I remember that too. :)
Re: Anyone know how to Encrypt a String via 128+-bit Encryption?
After googling I found the same exact code all over the place, so here are both encrypt and decrypt functions along with how to use them. I had to do this because I couldn't find the decrypt code here on this website and I also was getting a error on this line of code:
PHP Code:
AES.Mode = Security.Cryptography.CipherMode.ECB
I had to add "System." in front of Security. Now it looks like this:
PHP Code:
AES.Mode = System.Security.Cryptography.CipherMode.ECB
(And it works! :)) I also had to get rid of the "Try" statements on both decrypt and decrypt functions... or the End Functions would turn green in visual studio 2010... But hey its working now, no errors and it works.
I thought this would help out people like myself, so I hope it does. :wave:
1st • Make a new project, import:
Imports MySql.Data.MySqlClient
Imports System.Text
Imports System.IO
2nd • add some textboxes named: TextBoxinput.text, TextBoxoutput and TextBoxSalt.text
3rd add 2 buttons named: Button1 and Button2
Button1 (encrypt code)
PHP Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'encrypt
Dim aesEncrypted As String = AES_Encrypt(TextBoxinput.Text, TextBoxSalt.Text)
TextBoxoutput.Text = aesEncrypted
End Sub
Button2 (decrypt code)
PHP Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'decrypt
Dim aesDecrypted As String = AES_Decrypt(TextBoxoutput.Text, TextBoxSalt.Text)
TextBoxinput.Text = aesDecrypted
End Sub
4th copy and paste the 2 functions below into your project
PHP Code:
Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim decrypted As String = ""
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = Security.Cryptography.CipherMode.ECB
Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
Dim Buffer As Byte() = Convert.FromBase64String(input)
decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Return decrypted
End Function
Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim encrypted As String = ""
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = Security.Cryptography.CipherMode.ECB
Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Return encrypted
End Function
End Class
Here is the complete form1 code:
PHP Code:
Imports MySql.Data.MySqlClient
Imports System.Text
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'encrypt
Dim aesEncrypted As String = AES_Encrypt(TextBoxinput.Text, TextBoxSalt.Text)
TextBoxoutput.Text = aesEncrypted
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'decrypt
Dim aesDecrypted As String = AES_Decrypt(TextBoxoutput.Text, TextBoxSalt.Text)
TextBoxinput.Text = aesDecrypted
End Sub
Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim decrypted As String = ""
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = Security.Cryptography.CipherMode.ECB
Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
Dim Buffer As Byte() = Convert.FromBase64String(input)
decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Return decrypted
End Function
Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim encrypted As String = ""
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = Security.Cryptography.CipherMode.ECB
Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Return encrypted
End Function
End Class
Re: Anyone know how to Encrypt a String via 128+-bit Encryption?
Hello, Several of you have sent me PMs asking for the decryption code. It was stupid of me to post the encrypt code and not the decryption code. So, Here it is -- Fixed typos in Encrypt function and made it easier to use with Decrypt function.
Encrypt:
Code:
Function AESEncrypt(ByVal PlainText As String, ByVal Password As String, ByVal salt As String)
Dim HashAlgorithm As String = "SHA1" 'Can be SHA1 or MD5
Dim PasswordIterations As String = 2
Dim InitialVector As String = "OFRna73m*aze01xY" 'This should be a string of 16 ASCII characters.
Dim KeySize As Integer = 256 'Can be 128, 192, or 256.
If (String.IsNullOrEmpty(PlainText)) Then
Return ""
Exit Function
End If
Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector)
Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt)
Dim PlainTextBytes As Byte() = Encoding.UTF8.GetBytes(PlainText)
Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations)
Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize / 8)
Dim SymmetricKey As RijndaelManaged = New RijndaelManaged()
SymmetricKey.Mode = CipherMode.CBC
Dim CipherTextBytes As Byte() = Nothing
Using Encryptor As ICryptoTransform = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes)
Using MemStream As New MemoryStream()
Using CryptoStream As New CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)
CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length)
CryptoStream.FlushFinalBlock()
CipherTextBytes = MemStream.ToArray()
MemStream.Close()
CryptoStream.Close()
End Using
End Using
End Using
SymmetricKey.Clear()
Return Convert.ToBase64String(CipherTextBytes)
End Function
Decrypt:
Code:
Function AESDecrypt(ByVal CipherText As String, ByVal password As String, ByVal salt As String) As String
Dim HashAlgorithm As String = "SHA1"
Dim PasswordIterations As String = 2
Dim InitialVector As String = "OFRna73m*aze01xY"
Dim KeySize As Integer = 256
If (String.IsNullOrEmpty(CipherText)) Then
Return ""
End If
Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector)
Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt)
Dim CipherTextBytes As Byte() = Convert.FromBase64String(CipherText)
Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(password, SaltValueBytes, HashAlgorithm, PasswordIterations)
Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize / 8)
Dim SymmetricKey As RijndaelManaged = New RijndaelManaged()
SymmetricKey.Mode = CipherMode.CBC
Dim PlainTextBytes As Byte() = New Byte(CipherTextBytes.Length - 1) {}
Dim ByteCount As Integer = 0
Using Decryptor As ICryptoTransform = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes)
Using MemStream As MemoryStream = New MemoryStream(CipherTextBytes)
Using CryptoStream As CryptoStream = New CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read)
ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length)
MemStream.Close()
CryptoStream.Close()
End Using
End Using
End Using
SymmetricKey.Clear()
Return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount)
End Function
Usage:
Code:
Dim encryptedString As String = AESEncrypt("This is a plain string!!!", "password", "xXEmoNinjaUnicornXx")
MessageBox.Show(encryptedString)
Dim decryptedString As String = AESDecrypt(encryptedString, "password", "xXEmoNinjaUnicornXx")
MessageBox.Show(decryptedString)
Re: Anyone know how to Encrypt a String via 128+-bit Encryption?
Ola,
I tried the latest code from n00bl3z, but since I have Option Strict On there are several issues and was wondering if anyone could help with this. I've already fixed some error, but a few remain:
vb.net Code:
Imports System.Security.Cryptography
Imports System.Security.Cryptography.CryptoStream
Imports System.Text
Imports System.IO
Function AESEncrypt(ByVal PlainText As String, ByVal Password As String, ByVal salt As String) As String
Dim HashAlgorithm As String = "SHA1" 'Can be SHA1 or MD5
Dim PasswordIterations As String = CStr(2)
Dim InitialVector As String = "OFRna73m*aze01xY" 'This should be a string of 16 ASCII characters.
Dim KeySize As Integer = 256 'Can be 128, 192, or 256.
If (String.IsNullOrEmpty(PlainText)) Then
Return ""
Exit Function
End If
Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector)
Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt)
Dim PlainTextBytes As Byte() = Encoding.UTF8.GetBytes(PlainText)
Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations)
'Error 1 Overload resolution failed because no accessible 'New' can be called with these arguments:
'Public Sub New(password() As Byte, salt() As Byte, hashName As String, iterations As Integer)': Value of type 'String' cannot be converted to '1-dimensional array of Byte'.
'Public Sub New(password() As Byte, salt() As Byte, hashName As String, iterations As Integer)': Option Strict On disallows implicit conversions from 'String' to 'Integer'.
'Public Sub New(strPassword As String, rgbSalt() As Byte, strHashName As String, iterations As Integer)': Option Strict On disallows implicit conversions from 'String' to 'Integer'.
Dim KeyBytes As Byte() = DerivedPassword.GetBytes(CInt(KeySize / 8))
'Warning 2 'Public Overrides Function GetBytes(cb As Integer) As Byte()' is obsolete: 'Rfc2898DeriveBytes replaces PasswordDeriveBytes for deriving key material from a password and is preferred in new applications.'.
Dim SymmetricKey As RijndaelManaged = New RijndaelManaged()
SymmetricKey.Mode = CipherMode.CBC
Dim CipherTextBytes As Byte() = Nothing
Using Encryptor As ICryptoTransform = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes)
Using MemStream As New MemoryStream()
Using CryptoStream As New CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)
CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length)
CryptoStream.FlushFinalBlock()
CipherTextBytes = MemStream.ToArray()
MemStream.Close()
CryptoStream.Close()
End Using
End Using
End Using
SymmetricKey.Clear()
Return Convert.ToBase64String(CipherTextBytes)
End Function
Thanks in advance.
Re: Anyone know how to Encrypt a String via 128+-bit Encryption?
A question:
Dim KeySize As Integer = 256 'Can be 128, 192, or 256.
I presume this is the encryption strength? Is this correct?
Re: Anyone know how to Encrypt a String via 128+-bit Encryption?
You have a combination lock with 2, 4 or 8 digits, which is harder to crack :). This article has an interesting point however.