2014-05-24 58 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