无法解密加密数据 lua。

大家好,我目前卡在了我的脚本中。我试图加密一些数据并在需要时进行解密,但无法实现。

我使用的是:

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 !

现在我想要做的就是解密我的加密字符串,我有一个从服务器调用数据的程序,希望它被加密并在到达我的程序时进行解密。我尝试执行以下操作:

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));

它应该基本上解密我加密的字符串,但它没有实现它,它只是再次打印相同的字符串。对此有任何帮助吗?

点赞
用户1009479
用户1009479

在你的第二段代码片段中,你对已经加密的字符串 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))
2014-05-24 02:00:37