I am making a string encrypter. This is what i have so far:
I was wondering is there a much easier way to do this, and is that a fairly safe encryption method?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
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.




Reply With Quote