VBDT
Oct 5th, 2006, 02:28 AM
Hi guys,
Since this forum is for all kinds of tasting I like to test this code before I post it in the code Bank. The code is in VB.Net and it has two functions Encrypt and Decrypt. I like to here your opinions about it and also if this code is good enough to encrypt a string expression that will be hard enough to crack down or not. In my opinion the encryption is pretty good. Basically, it xors the expression character with the key character at the time. Note it doesn’t xor the expression characters with the same key character but every time with deferent one. Since it does not have the same pattern with every char it makes encryption secure. For example: string “word” with the key “s2y” will be encrypted this way: letter ‘w’ will be xored with s,2,y, letter ‘o’ will be xored with s,2, letter ‘r’ will be xored with s, and letter ‘d’ will be xored with s,2,y. In addition it might have null characters as well so the text editors will not even show the text after the null char. And also to break the key is very difficult because the key can be any character including Unicode. Thus a key which has seven characters should be checked against 84,431,259,000 different combinations with only 126 ASCII characters. I can’t even imagine what would be the number with 254 or Unicode ASCII characters.
'Author: Arman G.
'Created date: 10/04/2006
Public Class ARGHendeCrypt
''' <summary>
''' Encrypts a string expression (Unicode also) and returns the encrypted string.
''' </summary>
''' <param name="_string">A string expression that will be encrypted.</param>
''' <param name="key">A string key for encryption.</param>
Public Function Encrypt(ByVal _string As String, ByVal key As String) As String
Dim encryptString As String = ""
Dim i, i1, ascii As Integer
If _string <> "" AndAlso key <> "" Then
i1 = key.Length - 1
For Each ch As Char In _string
While i <= i1
ascii = AscW(ch) Xor AscW(key.Substring(i))
ch = ChrW(ascii)
i += 1
End While
i = 0
i1 -= 1
If i1 < 0 Then
i1 = key.Length - 1
End If
encryptString &= ChrW(ascii)
Next
End If
Return encryptString
End Function
''' <summary>
''' Decrypts an encrypted string expression (Unicode also) and returns the decrypted string.
''' </summary>
''' <param name="_string">An encrypted string that will be decrypted.</param>
''' <param name="key">A string key for decryption.</param>
Public Function Decrypt(ByVal _string As String, ByVal key As String) As String
Dim decryptString As String = ""
Dim i, i1, ascii As Integer
If _string <> "" AndAlso key <> "" Then
For Each ch As Char In _string
i = key.Length - 1
While i - i1 >= 0
ascii = AscW(ch) Xor AscW(key.Substring(i - i1))
ch = ChrW(ascii)
i -= 1
End While
i1 += 1
If i1 > key.Length - 1 Then
i1 = 0
End If
decryptString &= ChrW(ascii)
Next
End If
Return decryptString
End Function
End Class
Since this forum is for all kinds of tasting I like to test this code before I post it in the code Bank. The code is in VB.Net and it has two functions Encrypt and Decrypt. I like to here your opinions about it and also if this code is good enough to encrypt a string expression that will be hard enough to crack down or not. In my opinion the encryption is pretty good. Basically, it xors the expression character with the key character at the time. Note it doesn’t xor the expression characters with the same key character but every time with deferent one. Since it does not have the same pattern with every char it makes encryption secure. For example: string “word” with the key “s2y” will be encrypted this way: letter ‘w’ will be xored with s,2,y, letter ‘o’ will be xored with s,2, letter ‘r’ will be xored with s, and letter ‘d’ will be xored with s,2,y. In addition it might have null characters as well so the text editors will not even show the text after the null char. And also to break the key is very difficult because the key can be any character including Unicode. Thus a key which has seven characters should be checked against 84,431,259,000 different combinations with only 126 ASCII characters. I can’t even imagine what would be the number with 254 or Unicode ASCII characters.
'Author: Arman G.
'Created date: 10/04/2006
Public Class ARGHendeCrypt
''' <summary>
''' Encrypts a string expression (Unicode also) and returns the encrypted string.
''' </summary>
''' <param name="_string">A string expression that will be encrypted.</param>
''' <param name="key">A string key for encryption.</param>
Public Function Encrypt(ByVal _string As String, ByVal key As String) As String
Dim encryptString As String = ""
Dim i, i1, ascii As Integer
If _string <> "" AndAlso key <> "" Then
i1 = key.Length - 1
For Each ch As Char In _string
While i <= i1
ascii = AscW(ch) Xor AscW(key.Substring(i))
ch = ChrW(ascii)
i += 1
End While
i = 0
i1 -= 1
If i1 < 0 Then
i1 = key.Length - 1
End If
encryptString &= ChrW(ascii)
Next
End If
Return encryptString
End Function
''' <summary>
''' Decrypts an encrypted string expression (Unicode also) and returns the decrypted string.
''' </summary>
''' <param name="_string">An encrypted string that will be decrypted.</param>
''' <param name="key">A string key for decryption.</param>
Public Function Decrypt(ByVal _string As String, ByVal key As String) As String
Dim decryptString As String = ""
Dim i, i1, ascii As Integer
If _string <> "" AndAlso key <> "" Then
For Each ch As Char In _string
i = key.Length - 1
While i - i1 >= 0
ascii = AscW(ch) Xor AscW(key.Substring(i - i1))
ch = ChrW(ascii)
i -= 1
End While
i1 += 1
If i1 > key.Length - 1 Then
i1 = 0
End If
decryptString &= ChrW(ascii)
Next
End If
Return decryptString
End Function
End Class