[2005] String Conversions / BASIC encryption
Hello...
I'm creating my own encryption-of-sorts on my application. The application is VERY simplistic so I don't need any hugely complicated encryption.
Basically,
I want my program to automatically convert characters in a string. I already know the conversion method that I'm going to use I just need help with the syntax.
So, in plain english, what i need is code that will
For each letter in STRING.
Replace A with B (case statement?)
Until the end of the string.
Here's a basic example. Say I want to use the following encryption (we'll just use numbers for now, since there's only 10 of them).
User enters "1234567890" into a textbox and presses encrypt.
My application goes through the string, one character at a time and checks for the 'switching' rule. In this example i'll use
1 becomes 2
2 becomes 3
...etc...
9 becomes 0
0 becomes 1
So, in textbox two the "encrypted" string would be: 234567890
Can someone help me with code on this please? I"m a newbie and I think my theory works for what I want to do, just not sure the most efficient way to do this.
Re: [2005] String Conversions / BASIC encryption
do you want to encrypt numbers, letters or both? what kind of string characters your string will contain?
Re: [2005] String Conversions / BASIC encryption
I've created a conversion chart for all possible character entries. So, to answer your question... I'm going to be converting both/everything.... all letters, numbers, special characters.
I already know what conversions I'm going to use... just need to know how to do it.
Because I've got conversions for every possible character, this should be a simple "for each character, if it's THIS, make it THAT, move to the next character, until there are no more characters". And i'll use the reverse for my decryption.
Does this make sense?
Re: [2005] String Conversions / BASIC encryption
Use Select Case statements if the encryption keys are not random. I mean for all 0, it is 1, for all 1 is 2 and so on.
VB Code:
Dim chr() As Char
chr = TextBox1.Text
Dim i As Int16
For i = 0 To chr.Length() - 1
Select Case chr(i)
Case "0" : chr(i) = "1"
Case "1" : chr(i) = "2"
Case "2" : chr(i) = "3"
Case "3" : chr(i) = "4"
Case "4" : chr(i) = "5"
Case "5" : chr(i) = "6"
Case "6" : chr(i) = "7"
Case "7" : chr(i) = "8"
Case "8" : chr(i) = "9"
Case "9" : chr(i) = "0"
End Select
Next
TextBox2.Text = chr
Re: [2005] String Conversions / BASIC encryption
Not very pretty, but nicer than using a bunch of Case statements.
VB Code:
Public Class Crypto
Public Function Cypher(ByVal msg As String) As String
Dim ALPHA As String = "abcdefghijklmnopqrstuvwxyz "
Dim CODE As String = "fstyzudeqrvwghijabcklmnopx "
Dim ch As Char = Nothing
Dim pos As Integer = 0
Dim sReturn As String = Nothing
For i As Integer = 0 To msg.Length - 1
ch = CChar(msg.Substring(i, 1).ToLower)
pos = ALPHA.IndexOf(ch)
If (pos >= 0 And (pos <= 26)) Then
sReturn = sReturn & CODE.Substring(pos, 1)
End If
Next i
Return sReturn
End Function
Public Function UnCypher(ByVal msg As String) As String
Dim ALPHA As String = "fstyzudeqrvwghijabcklmnopx "
Dim CODE As String = "abcdefghijklmnopqrstuvwxyz "
Dim ch As Char = Nothing
Dim pos As Integer = 0
Dim sReturn As String = Nothing
For i As Integer = 0 To msg.Length - 1
ch = CChar(msg.Substring(i, 1).ToLower)
pos = ALPHA.IndexOf(ch)
If (pos >= 0 And (pos <= 26)) Then
sReturn = sReturn & CODE.Substring(pos, 1)
End If
Next i
Return sReturn
End Function
End Class
Re: [2005] String Conversions / BASIC encryption
thanks both. looks like i'm on the right track!
1 Attachment(s)
Re: [2005] String Conversions / BASIC encryption
Edited: I put some code together too. You just need to use the same string key to encrypt and decrypt the string. Make the key at least with six chars including letters and numbers for more security. It is simple and very hard to break. :D
VB Code:
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
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
Re: [2005] String Conversions / BASIC encryption
OK, I'm being VERY N00B right now.
I want to take a combination of ALL of these.
If I create a class for Encryption and then two Functions : Encrypt and Decrypt.
How would I then go about passing my textbox elements TO the encryptor and from the encryptor back... I haven't worked with classes at all...
Re: [2005] String Conversions / BASIC encryption
you juct declar the class variable and use it. for example
VB Code:
Public Class Form1
Dim cryption As New ARGHendeCrypt
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myString As String = TextBox2.Text
Dim ent, dc As String
ent = cryption.Encrypt(myString, TextBox1.Text)
dc = cryption.Decrypt(ent, TextBox5.Text)
TextBox3.Text = ent
TextBox4.Text = dc
End Sub
End Class
and this is the class
VB Code:
'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
If you will use this class then you need to save or hold the encrypted text in a variable or in the text file.