2017-10-07 66 views
0

所以我想做一個函數,可以加密和解密到Base64,然後用一個鍵XOR它。VB.net Base64 + XOR

這裏是我到目前爲止的代碼:

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 

因此,這裏是我的問題:這個代碼工作正常,但我試圖解密此字符串「X1hZXVoCBQY =」,該字符串的解密應該是「lolol123 」。我不知道我的代碼是否有錯,或者密鑰不正確。有人可以檢查我的代碼或給我正確的密鑰嗎?我試圖暴力破解這個關鍵,但這個結果對我來說並不合適。如果我嘗試解密字符串,我沒有得到「lolol123」,我得到「_Y [^^」

+0

Base64不是加密。 XORing的東西不是加密 - 它最多是模糊的 – Plutonix

+0

@Plutonix,thnkx爲了澄清 – MikeWe

+0

仍然沒有解決;( – MikeWe

回答

0

xor函數出錯了。試圖在一行中做太多的事情可能會使事情變得太複雜而無法立即看到正在發生的事情,所以我開始重寫它,因爲我會怎麼做,並且在我開始查看任何事情之前,快速檢查顯示它正在工作否則:

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 
+0

哇!男人!你是最棒的! – MikeWe