I want to encrypt using caesar cipher with VB.net. I am successful when I input 'ABC' the result is 'def', but when I input 'XYZ' the result is still 'xyz'. When I input 'XYZ' the result should be 'abc'. Can you guys help me please?
Source code
Public Function EncCaesar(ByVal s As String) As String
Dim charSet1 As String = " ABCDEFGHIJKLMNOPQRSTUVWXYZ" 'my input string
Dim charSet2 As String = " abcdefghijklmnopqrstuvwxyz" 'my encrypt key
Dim i As Integer
Dim pos, pos2 As Integer, encryptedChar, encryptedText
For i = 1 To Len(s)
pos = InStr(charSet1, Mid(s, i, 1))
pos = pos + 3
pos2 = InStr(charSet1, Mid(s, i, 1))
pos2 = pos - 3
If pos > 0 Then
If pos2 > 24 Then
encryptedChar = Mid(charSet2, pos2, 1)
encryptedText = encryptedText + encryptedChar
Else
encryptedChar = Mid(charSet2, pos, 1)
encryptedText = encryptedText + encryptedChar
End If
End If
Next i
EncCaesar = encryptedText
End Function
Try this, I have manipulated Ascii codes for this function.
Public Function encCaesar(ByVal s As String) As String
'abc 97 to 122
'ABC 65 to 90
Dim i As Integer
Dim myString As String = Trim(s)
Dim myAscii As Integer
Dim myChar, myEncString As String
myEncString = ""
For i = 1 To myString.Length
myChar = ""
myAscii = Asc(Mid(myString, i, 1))
Select Case myAscii
Case 97 To 119 'smaller case a to w
myChar = Chr(myAscii + 3)
Case 65 To 87 'upper case A to W
myChar = myAscii + 3
Case 120 To 122 'smaller case x to z
myChar = Chr((myAscii + 3) - 26)
Case 88 To 90 ' upper case X to Z
myChar = Chr((myAscii + 3) - 26)
Case Else
myChar = " "
End Select
myEncString = myEncString & myChar
Next
Return myEncString
End Function
Modify your code like below. This code won't work for lower case.
Public Function EncCaesar(ByVal s As String) As String
's = UCase(s)
Dim charSet1 As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZABC"
Dim charSet2 As String = "abcdefghijklmnopqrstuvwxyzabc"
Dim i As Integer
Dim pos, pos2 As Integer, encryptedChar, encryptedText
For i = 1 To Len(s)
pos = InStr(charSet1, Mid(s, i, 1))
pos = pos + 3
pos2 = InStr(charSet1, Mid(s, i, 1))
pos2 = pos - 3
If pos > 0 Then
encryptedChar = Mid(charSet2, pos, 1)
encryptedText = encryptedText + encryptedChar
End If
Next i
EncCaesar = encryptedText
End Function
You could try my approach.
Encryption:
(code cleaned)
''' <summary>
''' Encrypts a string using Caesar's substitution technique.
''' </summary>
''' <remarks>
''' http://en.wikipedia.org/wiki/Caesar_cipher
''' </remarks>
''' <param name="text">The text to encrypt.</param>
''' <param name="shift">The character shifting.</param>
''' <param name="charSet">The character set to use in encoding.</param>
''' <returns>The encrypted string.</returns>
Public Shared Function CaesarEncrypt(ByVal text As String,
ByVal shift As Integer,
Optional ByVal charSet As String =
"abcdefghijklmnopqrstuvwxyz" &
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" & " ") As String
Dim sb As New System.Text.StringBuilder With {.Capacity = text.Length}
For Each c As Char In text
Dim charIndex As Integer = charSet.IndexOf(c)
If charIndex = -1 Then
Throw New Exception(String.Format("Character '{0}' not found in character set '{1}'.", c, charSet))
Else
Do Until (charIndex + shift) < (charSet.Length)
charIndex -= charSet.Length
Loop
sb.Append(charSet(charIndex + shift))
End If
Next c
Return sb.ToString
End Function
Decryption:
''' <summary>
''' Decrypts a string using Caesar's substitution technique.
''' </summary>
''' <remarks>
''' http://en.wikipedia.org/wiki/Caesar_cipher
''' </remarks>
''' <param name="text">The encrypted text to decrypt.</param>
''' <param name="shift">The character shifting to reverse the encryption.</param>
''' <param name="charSet">The character set to use in decoding.</param>
''' <returns>The decrypted string.</returns>
Public Shared Function CaesarDecrypt(ByVal text As String,
ByVal shift As Integer,
Optional ByVal charSet As String =
"abcdefghijklmnopqrstuvwxyz" &
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" & " ") As String
Return CaesarEncrypt(text, shift, String.Join("", charSet.Reverse))
End Function
Test usage and results:
Dim value As String = "Hello World"
Dim encrypted As String = CaesarEncrypt(value, shift:=15)
Dim decrypted As String = CaesarDecrypt(encrypted, shift:=15)
Debug.WriteLine(String.Format("Unmodified string: {0}", value))
Debug.WriteLine(String.Format("Encrypted string: {0}", encrypted))
Debug.WriteLine(String.Format("Decrypted string: {0}", decrypted))
Unmodified string: Hello World
Encrypted string: WtAADokDGAs
Decrypted string: Hello World
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With