Reaper
Reaper

Reputation: 33

Failing to decrypt Encrypted Data lua

Hello guys atm i am stuck with my script i am trying to encrypt some data and decrpyt it when i want but not able to

i use

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

so it prints

Encryption: #c)*J}s-Mj!=[f3`7AfW{XCW*.EI!c0,i4Y:3Z~{ 
Decryption: Hello World ! 

now what i want to do is just decrpyt my encrypted string , have a program which calls data from server so i want it to be encrypted and decrpyt it once it reaches my program i tried do

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

which should basically decrypt that string which i encrypted but that dont do it it just pritns same string again any help on this??

Upvotes: 3

Views: 1392

Answers (1)

Yu Hao
Yu Hao

Reputation: 122363

In your second code snippet, you called crypt on the already encrypted string str. So depending on what you want, either don't encrypt it twice:

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

Or decrypt it twice:

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

Upvotes: 3

Related Questions