Results 1 to 2 of 2

Thread: [resolved]String Encrypter help

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    47

    Resolved [resolved]String Encrypter help

    I am making a string encrypter. This is what i have so far:

    Code:
        Public Function stringEncrypt(ByVal input As String, ByVal key As String, Optional ByVal encrypt As Boolean = True, Optional ByVal passes As Integer = 1)
            Dim inputNumber(input.Length - 1) As Integer
            Dim keyNumber(key.Length - 1) As Integer
            Dim outputNumber(input.Length - 1) As Integer
            Dim output As String = ""
            Dim letters() As Char = {"a", "b", "c", "d", "e", "f", "g", "h", _
                                    "i", "j", "k", "l", "m", "n", "o", "p", _
                                    "q", "r", "s", "t", "u", "v", "w", "x", _
                                    "y", "z", " ", ".", ",", "!", ""} 'must have extra char
            Dim keyCount As Integer = 0
            Dim i As Integer, j As Integer
    
            For i = 0 To input.Length - 1 'converts input to number representation
                For j = 0 To letters.Length - 1
                    If input.ElementAt(i) = letters(j) Then 'see below
                        inputNumber(i) = j
                        Continue For
                    End If
                    If j = letters.Length - 1 Then MsgBox(input.Length)
                Next
            Next
    
            For i = 0 To key.Length - 1 'converts key to number representation
                For j = 0 To letters.Length - 1
                    If key.ElementAt(i) = letters(j) Then 'if equal replace letter with number
                        keyNumber(i) = j
                        Continue For
                    End If
                Next
            Next
    
            Select Case encrypt
                Case True 'encrypt
    
                    ' add keys together, if over max must subract
                    For i = 0 To inputNumber.Length - 1
                        outputNumber(i) = inputNumber(i) + keyNumber(keyCount)
    
                        If outputNumber(i) > (letters.Length - 1) Then outputNumber(i) -= (letters.Length - 1)
                        keyCount += 1
                        If keyCount > (keyNumber.Length - 1) Then keyCount = 0
                    Next
    
    
                Case False 'decrypt
                    'subtract keys
    
                    For i = 0 To inputNumber.Length - 1
                        outputNumber(i) = inputNumber(i) - keyNumber(keyCount)
    
                        If outputNumber(i) < 0 Then outputNumber(i) += (letters.Length - 1)
    
                        keyCount += 1
                        If keyCount > keyNumber.Length - 1 Then keyCount = 0
                    Next
            End Select
    
            'convert back to letters
            For i = 0 To outputNumber.Length - 1
                output = output & letters(outputNumber(i))
            Next
    
            Dim rand As New Random(keyNumber(0))
    
            Return output
        End Function
    I was wondering is there a much easier way to do this, and is that a fairly safe encryption method?

    Also the above code only works sometimes, does anyone have an idea why. The encryptes strings are often cut short, and the last character in the array is replaced with the first when decrypting.
    Last edited by qazwsx; Apr 19th, 2008 at 10:09 AM. Reason: Resolved

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: String Encrypter help

    VB.NET has built in encryption libraries. If you're interested, check out the link in my signature on Encryption.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

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