Results 1 to 9 of 9

Thread: [2005] String Conversions / BASIC encryption

  1. #1

    Thread Starter
    Fanatic Member mateo107's Avatar
    Join Date
    Jan 2005
    Posts
    547

    [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.


    -Matthew-

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] String Conversions / BASIC encryption

    do you want to encrypt numbers, letters or both? what kind of string characters your string will contain?

  3. #3

    Thread Starter
    Fanatic Member mateo107's Avatar
    Join Date
    Jan 2005
    Posts
    547

    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?


    -Matthew-

  4. #4
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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:
    1. Dim chr() As Char
    2.         chr = TextBox1.Text
    3.         Dim i As Int16
    4.         For i = 0 To chr.Length() - 1
    5.             Select Case chr(i)
    6.                 Case "0" : chr(i) = "1"
    7.                 Case "1" : chr(i) = "2"
    8.                 Case "2" : chr(i) = "3"
    9.                 Case "3" : chr(i) = "4"
    10.                 Case "4" : chr(i) = "5"
    11.                 Case "5" : chr(i) = "6"
    12.                 Case "6" : chr(i) = "7"
    13.                 Case "7" : chr(i) = "8"
    14.                 Case "8" : chr(i) = "9"
    15.                 Case "9" : chr(i) = "0"
    16.             End Select
    17.         Next
    18.         TextBox2.Text = chr
    Show Appreciation. Rate Posts.

  5. #5
    New Member
    Join Date
    Oct 2006
    Posts
    5

    Re: [2005] String Conversions / BASIC encryption

    Not very pretty, but nicer than using a bunch of Case statements.

    VB Code:
    1. Public Class Crypto
    2.  
    3.     Public Function Cypher(ByVal msg As String) As String
    4.         Dim ALPHA As String = "abcdefghijklmnopqrstuvwxyz "
    5.         Dim CODE As String = "fstyzudeqrvwghijabcklmnopx "
    6.         Dim ch As Char = Nothing
    7.         Dim pos As Integer = 0
    8.         Dim sReturn As String = Nothing
    9.  
    10.         For i As Integer = 0 To msg.Length - 1
    11.             ch = CChar(msg.Substring(i, 1).ToLower)
    12.             pos = ALPHA.IndexOf(ch)
    13.             If (pos >= 0 And (pos <= 26)) Then
    14.                 sReturn = sReturn & CODE.Substring(pos, 1)
    15.             End If
    16.         Next i
    17.         Return sReturn
    18.     End Function
    19.  
    20.  
    21.     Public Function UnCypher(ByVal msg As String) As String
    22.         Dim ALPHA As String = "fstyzudeqrvwghijabcklmnopx "
    23.         Dim CODE As String = "abcdefghijklmnopqrstuvwxyz "
    24.         Dim ch As Char = Nothing
    25.         Dim pos As Integer = 0
    26.         Dim sReturn As String = Nothing
    27.  
    28.         For i As Integer = 0 To msg.Length - 1
    29.             ch = CChar(msg.Substring(i, 1).ToLower)
    30.             pos = ALPHA.IndexOf(ch)
    31.             If (pos >= 0 And (pos <= 26)) Then
    32.                 sReturn = sReturn & CODE.Substring(pos, 1)
    33.             End If
    34.         Next i
    35.         Return sReturn
    36.     End Function
    37. End Class

  6. #6

    Thread Starter
    Fanatic Member mateo107's Avatar
    Join Date
    Jan 2005
    Posts
    547

    Re: [2005] String Conversions / BASIC encryption

    thanks both. looks like i'm on the right track!


    -Matthew-

  7. #7
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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.
    VB Code:
    1. Public Function Encrypt(ByVal _string As String, ByVal key As String) As String
    2.         Dim encryptString As String = ""
    3.         Dim i, i1, ascii As Integer
    4.  
    5.         If _string <> "" AndAlso key <> "" Then
    6.             i1 = key.Length - 1
    7.             For Each ch As Char In _string
    8.                 While i <= i1
    9.                     ascii = AscW(ch) Xor AscW(key.Substring(i))
    10.                     ch = ChrW(ascii)
    11.                     i += 1
    12.                 End While
    13.                 i = 0
    14.                 i1 -= 1
    15.                 If i1 < 0 Then
    16.                     i1 = key.Length - 1
    17.                 End If
    18.                 encryptString &= ChrW(ascii)
    19.             Next
    20.         End If
    21.         Return encryptString
    22.     End Function
    23.  
    24.     Public Function Decrypt(ByVal _string As String, ByVal key As String) As String
    25.         Dim decryptString As String = ""
    26.         Dim i, i1, ascii As Integer
    27.  
    28.         If _string <> "" AndAlso key <> "" Then
    29.             For Each ch As Char In _string
    30.                 i = key.Length - 1
    31.                 While i - i1 >= 0
    32.                     ascii = AscW(ch) Xor AscW(key.Substring(i - i1))
    33.                     ch = ChrW(ascii)
    34.                     i -= 1
    35.                 End While
    36.                 i1 += 1
    37.                 If i1 > key.Length - 1 Then
    38.                     i1 = 0
    39.                 End If
    40.                 decryptString &= ChrW(ascii)
    41.             Next
    42.         End If
    43.         Return decryptString
    44.     End Function
    Attached Images Attached Images  

  8. #8

    Thread Starter
    Fanatic Member mateo107's Avatar
    Join Date
    Jan 2005
    Posts
    547

    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...


    -Matthew-

  9. #9
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] String Conversions / BASIC encryption

    you juct declar the class variable and use it. for example
    VB Code:
    1. Public Class Form1
    2.     Dim cryption As New ARGHendeCrypt
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         Dim myString As String = TextBox2.Text
    6.         Dim ent, dc As String
    7.         ent = cryption.Encrypt(myString, TextBox1.Text)
    8.         dc = cryption.Decrypt(ent, TextBox5.Text)
    9.  
    10.         TextBox3.Text = ent
    11.         TextBox4.Text = dc
    12.     End Sub
    13. End Class
    and this is the class
    VB Code:
    1. 'Author: Arman G.
    2. 'Created date: 10/04/2006
    3. Public Class ARGHendeCrypt
    4.     ''' <summary>
    5.     ''' Encrypts a string expression (Unicode also) and returns the encrypted string.
    6.     ''' </summary>
    7.     ''' <param name="_string">A string expression that will be encrypted.</param>
    8.     ''' <param name="key">A string key for encryption.</param>
    9.     Public Function Encrypt(ByVal _string As String, ByVal key As String) As String
    10.         Dim encryptString As String = ""
    11.         Dim i, i1, ascii As Integer
    12.  
    13.         If _string <> "" AndAlso key <> "" Then
    14.             i1 = key.Length - 1
    15.             For Each ch As Char In _string
    16.                 While i <= i1
    17.                     ascii = AscW(ch) Xor AscW(key.Substring(i))
    18.                     ch = ChrW(ascii)
    19.                     i += 1
    20.                 End While
    21.                 i = 0
    22.                 i1 -= 1
    23.                 If i1 < 0 Then
    24.                     i1 = key.Length - 1
    25.                 End If
    26.                 encryptString &= ChrW(ascii)
    27.             Next
    28.         End If
    29.         Return encryptString
    30.     End Function
    31.  
    32.     ''' <summary>
    33.     ''' Decrypts an encrypted string expression (Unicode also) and returns the decrypted string.
    34.     ''' </summary>
    35.     ''' <param name="_string">An encrypted string that will be decrypted.</param>
    36.     ''' <param name="key">A string key for decryption.</param>
    37.     Public Function Decrypt(ByVal _string As String, ByVal key As String) As String
    38.         Dim decryptString As String = ""
    39.         Dim i, i1, ascii As Integer
    40.  
    41.         If _string <> "" AndAlso key <> "" Then
    42.             For Each ch As Char In _string
    43.                 i = key.Length - 1
    44.                 While i - i1 >= 0
    45.                     ascii = AscW(ch) Xor AscW(key.Substring(i - i1))
    46.                     ch = ChrW(ascii)
    47.                     i -= 1
    48.                 End While
    49.                 i1 += 1
    50.                 If i1 > key.Length - 1 Then
    51.                     i1 = 0
    52.                 End If
    53.                 decryptString &= ChrW(ascii)
    54.             Next
    55.         End If
    56.         Return decryptString
    57.     End Function
    58. 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width