2014-05-24 85 views
3

好傢伙ATM我堅持我的腳本我試圖加密一些數據和decrpyt它時,我想,但不能未能解密加密數據盧阿

我使用

local function convert(chars, dist, inv) 
    return string.char((string.byte(chars) - 32 + (inv and -dist or dist)) % 95 + 32) 
end 

local function crypt(str,k,inv) 
    local enc= ""; 
    for i=1,#str do 
     if(#str-k[5] >= i or not inv)then 
     for inc=0,3 do 
      if(i%4 == inc)then 
       enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv); 
       break; 
      end 
     end 
     end 
    end 
    if(not inv)then 
     for i=1,k[5] do 
     enc = enc .. string.char(math.random(32,126)); 
     end 
    end 
    return enc; 
end 

local enc1 = {29, 58, 93, 28 ,27}; 
local str = "Hello World !"; 
local crypted = crypt(str,enc1) 
print("Encryption: " .. crypted); 
print("Decryption: " .. crypt(crypted,enc1,true)); 

所以打印

Encryption: #c)*J}s-Mj!=[f3`7AfW{XCW*.EI!c0,i4Y:3Z~{ 
Decryption: Hello World ! 
現在我想要做的只是decrpyt我的加密字符串,有一個程序,所以我希望它被加密,並且一旦達到我的計劃,我試圖做decrpyt它從服務器調用數據
local enc1 = {29, 58, 93, 28 ,27}; 
local str = "#c)*J}s-Mj!=[f3`7AfW{XCW*.EI!c0,i4Y:3Z~{"; 
local crypted = crypt(str,enc1) 

print("Decryption: " .. crypt(crypted,enc1,true)); 

應該基本上解密串,我加密,但不這樣做,它只是再次pritns相同的字符串有這方面的幫助?

+0

刪除加密步驟'本地加密後=隱窩(STR,ENC1)'碼。 –

+0

我想你想用'crypt(str,enc1,true)'而不是 – hjpotter92

回答

3

在你的第二個代碼片段中,你在已經加密的字符串str上調用crypt。所以,這取決於你想要什麼,要麼不加密兩次是:

local enc1 = {29, 58, 93, 28 ,27}; 
local str = "#c)*J}s-Mj!=[f3`7AfW{XCW*.EI!c0,i4Y:3Z~{"; 
print("Decryption: " .. crypt(crypted,enc1,true)); 

或解密兩次:從你的第二個

local enc1 = {29, 58, 93, 28 ,27}; 
local str = "#c)*J}s-Mj!=[f3`7AfW{XCW*.EI!c0,i4Y:3Z~{"; 
local crypted = crypt(str,enc1) 
print("Decryption: " .. crypt(crypt(crypted,enc1,true), enc1, true)) 
+0

完美工作,感謝您的幫助 – Reaper