locked
How shall i implement encryption RRS feed

  • Question

  • Hello,
    i am using vb.net 2008
    this is the code

    Dim str1(100) As Char
            str1 = "`1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./?><MNBVCXZ:LKJHGFDSA}{POIUYTREWQ+_)(*&^%$#@!~| "
            Dim STR2(100) As Char
            STR2 = " /.,MNBVCXZ';LKJHGFDSA\][POIUYTREWQ=-0987654321?><mnbvcxz:lkjhgfdsa|}{poiuytrewq~!@#$%^&*()_+"
            Dim STR3(20) As Char
            STR3 = "peter parker"
            Dim str4(20) As Char
            Dim i, j As Integer
            For j = 0 To STR3.Length
                For i = 0 To str1.Length - 1
                    If (STR3(j) = str1(i)) Then
                        str4.Concat("213")
                        'str4 = STR2(i)
                    End If
                Next
            Next
            MsgBox(str4)
        End Sub


    it gives me index array out of range exception

    through this code i am trying to do is that

    i want to encrypt text in str3
    i am getting an character frm str3 then searching it in str1 if found then putting an character frm str2 using the same index of str1 at which character was found in str4   

    kindly help me
    Thursday, March 5, 2009 1:08 PM

Answers

  • Hello fireblade123!,

    Heslacher,

    I was thinking the same thing that it was probably the "STR3.Length - 1", but if we look closer we find that "str1" is 94 chars long while "STR2" is 93.  This will cause an error because the char " " is at the very end of "str1" and tries to find it in STR2 at an index which again is not there.  I went ahead and just made this a Function to make things a-bit organized, here is what I came up with:

    Public Class Form1  
     
        Private encryptKey As Char() = "`1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./?><MNBVCXZ:LKJHGFDSA}{POIUYTREWQ+_)(*&^%$#@!~| " 
        Private decryptKey As Char() = " /.,MNBVCXZ';LKJHGFDSA\][POIUYTREWQ=-0987654321?><mnbvcxz:lkjhgfdsa|}{poiuytrewq~!@#$%^&*()_+~" 
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            MessageBox.Show(encrypt("peter parker"))  
        End Sub 
     
        Private Function encrypt(ByVal str3() As CharAs Char()  
     
            Dim str4(str3.Length - 1As Char 
            Dim i As Integer = 0  
     
            For Each str03 As Char In str3  
     
                For Each str01 As Char In encryptKey  
                    If str01 = str03 Then 
     
                        str4(i) = decryptKey(Array.IndexOf(encryptKey, str01))  
     
                    End If 
                Next 
                i += 1  
            Next 
     
            Return str4  
        End Function 
    End Class 

    Basically this is a cipher, in which the program looks for a Char in a Char array and replaces that Char with a new Char from a different Key.  You could actually make more functions like this and use different decrypt Keys.

    NOTE: I added the "~" in the "decryptKey" to make both Keys the same Length.

    I hope this helps you.  Any questions, feel free to post.

    Best Regards,

    Adjutor
    • Edited by Adjutor Thursday, March 5, 2009 2:20 PM ...
    • Proposed as answer by Adjutor Thursday, March 5, 2009 2:51 PM
    • Marked as answer by fireblade123 Friday, March 6, 2009 8:57 AM
    Thursday, March 5, 2009 1:59 PM

All replies

  • fireblade123 said:

    Hello,

    i am using vb.net 2008
    this is the code

    Dim str1(100) As Char
            str1 = "`1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./?><MNBVCXZ:LKJHGFDSA}{POIUYTREWQ+_)(*&^%$#@!~| "
            Dim STR2(100) As Char
            STR2 = " /.,MNBVCXZ';LKJHGFDSA\][POIUYTREWQ=-0987654321?><mnbvcxz:lkjhgfdsa|}{poiuytrewq~!@#$%^&*()_+"
            Dim STR3(20) As Char
            STR3 = "peter parker"
            Dim str4(20) As Char
            Dim i, j As Integer
            For j = 0 To STR3.Length -1
                For i = 0 To str1.Length - 1
                    If (STR3(j) = str1(i)) Then
                        str4.Concat("213")
                        'str4 = STR2(i)
                    End If
                Next
            Next
            MsgBox(str4)
        End Sub


    it gives me index array out of range exception

    through this code i am trying to do is that

    i want to encrypt text in str3
    i am getting an character frm str3 then searching it in str1 if found then putting an character frm str2 using the same index of str1 at which character was found in str4   

    kindly help me



    Look at the marker !
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    Thursday, March 5, 2009 1:22 PM
  • if i remove -1 then error comes
    Thursday, March 5, 2009 1:54 PM
  • Hi,

    not remove ! add it !
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    Thursday, March 5, 2009 1:55 PM
  • Hello fireblade123!,

    Heslacher,

    I was thinking the same thing that it was probably the "STR3.Length - 1", but if we look closer we find that "str1" is 94 chars long while "STR2" is 93.  This will cause an error because the char " " is at the very end of "str1" and tries to find it in STR2 at an index which again is not there.  I went ahead and just made this a Function to make things a-bit organized, here is what I came up with:

    Public Class Form1  
     
        Private encryptKey As Char() = "`1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./?><MNBVCXZ:LKJHGFDSA}{POIUYTREWQ+_)(*&^%$#@!~| " 
        Private decryptKey As Char() = " /.,MNBVCXZ';LKJHGFDSA\][POIUYTREWQ=-0987654321?><mnbvcxz:lkjhgfdsa|}{poiuytrewq~!@#$%^&*()_+~" 
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            MessageBox.Show(encrypt("peter parker"))  
        End Sub 
     
        Private Function encrypt(ByVal str3() As CharAs Char()  
     
            Dim str4(str3.Length - 1As Char 
            Dim i As Integer = 0  
     
            For Each str03 As Char In str3  
     
                For Each str01 As Char In encryptKey  
                    If str01 = str03 Then 
     
                        str4(i) = decryptKey(Array.IndexOf(encryptKey, str01))  
     
                    End If 
                Next 
                i += 1  
            Next 
     
            Return str4  
        End Function 
    End Class 

    Basically this is a cipher, in which the program looks for a Char in a Char array and replaces that Char with a new Char from a different Key.  You could actually make more functions like this and use different decrypt Keys.

    NOTE: I added the "~" in the "decryptKey" to make both Keys the same Length.

    I hope this helps you.  Any questions, feel free to post.

    Best Regards,

    Adjutor
    • Edited by Adjutor Thursday, March 5, 2009 2:20 PM ...
    • Proposed as answer by Adjutor Thursday, March 5, 2009 2:51 PM
    • Marked as answer by fireblade123 Friday, March 6, 2009 8:57 AM
    Thursday, March 5, 2009 1:59 PM
  • Hello Adjutor, fireblade123,

    yes you are right, almost. If the str3().length > 21 then there is also an indexoutofrange.

    You need to change Dim str4(20) As Char  in Dim str4(str3.Length-1) As Char 
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    Thursday, March 5, 2009 2:14 PM
  • Hello,

    Ah, yes, trying to keep the code the same, I left out that detail.

    Regards,

    Adjutor
    Thursday, March 5, 2009 2:19 PM
  • in the code which u have posted

    u used 

    Private Function encrypt(ByVal str3() As CharAs Char()  
     
            Dim str4(str3.Length - 1As Char 
            Dim i As Integer = 0  
     
            For Each str03 As Char In str3  
     
                For Each str01 As Char In encryptKey  
                    If str01 = str03 Then 
     
                        str4(i) = decryptKey(Array.IndexOf(encryptKey, str01))  
     
                    End If 
                Next 
                i += 1  
            Next 
     
            Return str4  
        End Function 

    would u plz explain to me what is str03 and str01 here........



    • Marked as answer by fireblade123 Friday, March 6, 2009 8:56 AM
    • Unmarked as answer by fireblade123 Friday, March 6, 2009 8:56 AM
    Thursday, March 5, 2009 5:58 PM
  • I would also like to tell u that there is nothing like decrypt key here
    this is only encryption algorithm program.....

    u should take a look at this algorithm

    1. get a character from str1
    2. search for that character in str2.
    3. if found then note the index of that character in temp
    4. go to the index stored in temp in str3 and read the character on that index.
    5. copy that character in str4
    6. repeat the above procedure for all characters in str1
    7. final encrypted text will be in str4
     
    Thursday, March 5, 2009 6:04 PM
  • Hello fireblade123,

    The str03 and str01 are like temporary variables used to store the Char that was found in the encryptKey Char array.  The program I wrote was modified based off of what you were trying to do.  The program is an encryption algorithm, based off the idea of the cipher.

    The algorithm that you posted is the same algorithm that the code I posted will do, just using a different approach to keep things more orderly.  Check the output of the program, it should be what you desire, if not explain what output you expect.

    Best Regards,

    Adjutor
    • Edited by Adjutor Thursday, March 5, 2009 6:10 PM ...
    Thursday, March 5, 2009 6:09 PM
  • Hello,
    Thanks. I tried u r code. It works........
    Thank you very much sir for solving my problem....
     
    Friday, March 6, 2009 8:56 AM