So I want to make a function that can Encrypt and Decrypt to Base64 and then XOR it with a key.
Here's my code so far:
Imports System.Text
Module Module1
Private key As String = "37526"
Sub Main()
Dim test As String = Console.ReadLine
test = Decrypt(test)
Console.WriteLine(test)
Main()
End Sub
Public Function Decrypt(CipherText As String) As String
Dim decoded = Convert.FromBase64String(CipherText)
Dim dexored = [xor](decoded, key)
Return Encoding.UTF8.GetString(dexored)
End Function
Private Function [xor](text As Byte(), key As String) As Byte()
Dim res As Byte() = New Byte(text.Length - 1) {}
For c As Integer = 0 To text.Length - 1
res(c) = CByte((text(c)) Xor CUInt(Val(c Mod key.Length)))
Next
Return res
End Function
Public Function Encrypt(Plaintext As String)
Dim encoded = Encoding.UTF8.GetBytes(Plaintext)
Dim xored = [xor](encoded, key)
Return Convert.ToBase64String(xored)
End Function
End Module
So here's my problem: this code works fine, but I'm trying to decrypt this string "X1hZXVoCBQY=", the decryption of that string should be "lolol123". I don't know if my code is wrong or if the key is incorrect. Could someone check my code or give me the correct key? I tried to bruteforce the key but that dind't work out for me. If I try to decrypt the string I don't get "lolol123", I get "_Y[^^"
It's going wrong in the xor
function. Trying to do too much in one line can make it too complicated to immediately see what is going on, so I started re-writing it more as how I would have done and a quick check showed it to be working before I started looking at anything else:
Option Infer On
Option Strict On
' ...
Private Function [xor](text As Byte(), key As String) As Byte()
Dim res As Byte() = New Byte(text.Length - 1) {}
Dim keyBytes = Encoding.UTF8.GetBytes(key)
For c = 0 To text.Length - 1
res(c) = CByte((text(c)) Xor keyBytes(c Mod keyBytes.Length))
Next
Return res
End Function