Answered by:
How shall i implement encryption

Question
-
Hello,i am using vb.net 2008this is the codeDim str1(100) As Charstr1 = "`1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./?><MNBVCXZ:LKJHGFDSA}{POIUYTREWQ+_)(*&^%$#@!~| "Dim STR2(100) As CharSTR2 = " /.,MNBVCXZ';LKJHGFDSA\][POIUYTREWQ=-0987654321?><mnbvcxz:lkjhgfdsa|}{poiuytrewq~!@#$%^&*()_+"Dim STR3(20) As CharSTR3 = "peter parker"Dim str4(20) As CharDim i, j As IntegerFor j = 0 To STR3.LengthFor i = 0 To str1.Length - 1If (STR3(j) = str1(i)) Thenstr4.Concat("213")'str4 = STR2(i)End IfNextNextMsgBox(str4)End Subit gives me index array out of range exceptionthrough this code i am trying to do is thati want to encrypt text in str3i 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 str4kindly help meThursday, 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.Object, ByVal e As System.EventArgs) Handles MyBase.Load MessageBox.Show(encrypt("peter parker")) End Sub Private Function encrypt(ByVal str3() As Char) As Char() Dim str4(str3.Length - 1) As 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 2008this is the codeDim str1(100) As Charstr1 = "`1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./?><MNBVCXZ:LKJHGFDSA}{POIUYTREWQ+_)(*&^%$#@!~| "Dim STR2(100) As CharSTR2 = " /.,MNBVCXZ';LKJHGFDSA\][POIUYTREWQ=-0987654321?><mnbvcxz:lkjhgfdsa|}{poiuytrewq~!@#$%^&*()_+"Dim STR3(20) As CharSTR3 = "peter parker"Dim str4(20) As CharDim i, j As IntegerFor j = 0 To STR3.Length -1For i = 0 To str1.Length - 1If (STR3(j) = str1(i)) Thenstr4.Concat("213")'str4 = STR2(i)End IfNextNextMsgBox(str4)End Subit gives me index array out of range exceptionthrough this code i am trying to do is thati want to encrypt text in str3i 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 str4kindly 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 comesThursday, 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.Object, ByVal e As System.EventArgs) Handles MyBase.Load MessageBox.Show(encrypt("peter parker")) End Sub Private Function encrypt(ByVal str3() As Char) As Char() Dim str4(str3.Length - 1) As 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,
AdjutorThursday, March 5, 2009 2:19 PM -
in the code which u have postedu used
Private Function encrypt(ByVal str3() As Char) As Char() Dim str4(str3.Length - 1) As 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 herethis is only encryption algorithm program.....u should take a look at this algorithm1. get a character from str12. search for that character in str2.3. if found then note the index of that character in temp4. go to the index stored in temp in str3 and read the character on that index.5. copy that character in str46. repeat the above procedure for all characters in str17. final encrypted text will be in str4Thursday, 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