Jump to content

Simple encryption algorithm


Recommended Posts

Since I do not remember any secure hashing or encryption algorithms in LUA, I made one myself, based on the idea of the blowfish algorithm (but less complicated).

iaEncryptionArray = {}; 
iaDecryptionArray = {}; 
caPassword = ""; 
caPasswordLength = 0; 
caText = ""; 
caEncryptedText = ""; 
iPasswordPosition = 1; 
  
function addEncryptionPasswordChar(value) 
    iPasswordPosition = iPasswordPosition + 1; 
    if (iPasswordPosition > caPasswordLength) then 
        iPasswordPosition = 1; 
    end 
    output = value + string.byte(caPassword,position); 
    if (output > 999) then 
        output = output - 1000; 
    end 
    return output; 
end 
  
function subsEncryptionPasswordChar(value) 
    iPasswordPosition = iPasswordPosition + 1; 
    if (iPasswordPosition > caPasswordLength) then 
        iPasswordPosition = 1; 
    end 
    output = value - string.byte(caPassword,position); 
    if (output < 0) then 
        output = output + 1000; 
    end 
    return output; 
end 
  
function generateEncryptionPassword() 
    iPasswordHash = 0; 
    caOutput = ""; 
    for i=1, caPasswordLength, 1 do 
        iPasswordHash = iPasswordHash + string.byte(caPassword,i); -- char! 
    end 
    for i=1, caPasswordLength, 1 do 
        caOutput = caOutput .. string.char(iPasswordHash % string.byte(caPassword,i)); -- char! char! 
    end 
    return caOutput; 
end 
  
function stringSetChar(string,char,pos) 
    length = string.len(string); 
    if pos > length then 
        return string; 
    end 
    string = string.sub(string,1,pos) .. char .. string.sub(string,pos+1,length); 
    return string; 
end 
  
function generateEncryptionArray() 
    generateDecryptionArray(); 
end 
  
function generateDecryptionArray() 
    caEncryptionPassword = generateEncryptionPassword(caPassword); 
    for i=0, 999, 1 do 
        iaEncryptionArray[i]=NIL; 
    end 
    iPosition = 0; 
    n=1; 
    for i=0, 999, 1 do 
        n = n + 1; 
        if (string.byte(caPassword,n) == NIL) then 
            n=1; 
        end 
        iPosition = iPosition + string.byte(caEncryptionPassword,n) + string.byte(caPassword,n); 
        iPosition = iPosition % 1000; 
        while (iaEncryptionArray[iPosition]) do 
            iPosition = iPosition + 1; 
            if (iPosition >= 1000) then 
                iPosition = iPosition - 1000; 
            end 
        end 
        iaEncryptionArray[iPosition] = i; 
        iaDecryptionArray[i] = iPosition; 
    end 
end 
  
function encrypt() 
    generateEncryptionArray(); 
    iTextSize = string.len(caText); 
    iEncryptedTextSize = iTextSize*3; 
    caEncryptedText = ""; 
    aEncryptedText = {}; 
    iPasswordPosition = 1; 
    for i=1, iTextSize, 1 do 
        value = string.byte(caText,i); 
        n=i*3; 
        if (value < 10) then 
            value = string.format("00%i",value); 
        elseif (value < 100) then 
            value = string.format("0%i",value); 
        else 
            value = string.format("%i",value); 
        end 
        aEncryptedText[(i-1)*3+1] = string.byte(value,1) - 48; 
        aEncryptedText[(i-1)*3+2] = string.byte(value,2) - 48; 
        aEncryptedText[(i-1)*3+3] = string.byte(value,3) - 48; 
    end 
    for i=1,iEncryptedTextSize,1 do 
        caEncryptedText = caEncryptedText .. string.format("%i",aEncryptedText[i]); 
    end 
    for runs=1, 32, 1 do 
        for i=1, iEncryptedTextSize, 1 do 
            pos1 = i; 
            pos2 = i+1; 
            pos3 = i+2; 
            if (pos2 > iEncryptedTextSize) then 
                pos2 = pos2 - iEncryptedTextSize; 
                pos3 = pos3 - iEncryptedTextSize; 
            elseif (pos3 > iEncryptedTextSize) then 
                pos3 = pos3 - iEncryptedTextSize; 
            end 
            h = aEncryptedText[pos1]; 
            z = aEncryptedText[pos2]; 
            e = aEncryptedText[pos3]; 
            value = 100*h + 10*z + e; 
            value = iaEncryptionArray[value]; 
            value = subsEncryptionPasswordChar(value,i); 
            h = math.floor(value / 100); 
            value = value - 100*h; 
            z = math.floor(value / 10); 
            value = value - 10*z; 
            e = value; 
            aEncryptedText[pos1] = h; 
            aEncryptedText[pos2] = z; 
            aEncryptedText[pos3] = e; 
        end 
    end 
    caEncryptedText = ""; 
    for i=1,iEncryptedTextSize,1 do 
        caEncryptedText = caEncryptedText .. string.format("%i",aEncryptedText[i]); 
    end 
end 
  
function decrypt() 
    generateDecryptionArray(); 
    iEncryptedTextSize = string.len(caEncryptedText); 
    aEncryptedText = {}; 
    for i=1,iEncryptedTextSize,1 do 
        aEncryptedText[i] = string.byte(caEncryptedText,i) - 48; 
    end 
    caEncryptedText = ""; 
    iPasswordPosition = 1; 
    for runs=1, 32, 1 do 
        for i=iEncryptedTextSize, 1, -1 do 
            pos1 = i; 
            pos2 = i+1; 
            pos3 = i+2; 
            if (pos2 > iEncryptedTextSize) then 
                pos2 = pos2 - iEncryptedTextSize; 
                pos3 = pos3 - iEncryptedTextSize; 
            elseif (pos3 > iEncryptedTextSize) then 
                pos3 = pos3 - iEncryptedTextSize; 
            end 
            h = aEncryptedText[pos1]; 
            z = aEncryptedText[pos2]; 
            e = aEncryptedText[pos3]; 
            value = 100*h + 10*z + e; 
            value = addEncryptionPasswordChar(value,i); 
            value = iaDecryptionArray[value]; 
            h = math.floor(value / 100); 
            value = value - 100*h; 
            z = math.floor(value / 10); 
            value = value - 10*z; 
            e = value; 
            aEncryptedText[pos1] = h; 
            aEncryptedText[pos2] = z; 
            aEncryptedText[pos3] = e; 
        end 
    end 
    iTextSize = iEncryptedTextSize / 3 
    caText = ""; 
    for i=1,iEncryptedTextSize,1 do 
        caEncryptedText = caEncryptedText .. string.format("%i",aEncryptedText[i]); 
    end 
    for i=1, iTextSize, 1 do 
        n = (i-1)*3+1; 
        char = aEncryptedText[n]*100 + aEncryptedText[n+1]*10 + aEncryptedText[n+2]; 
        if char > 255 then 
            caText = "wrong password!"; 
            return; 
        end 
        caText = caText .. string.char(char); 
    end 
end 
  
function encryptString(caTextInput,caPasswordInput) 
    caText = caTextInput; 
    caPassword = caPasswordInput; 
    caPasswordLength = string.len(caPassword); 
    encrypt(); 
    return (caEncryptedText); 
end 
  
function decryptString(caTextInput,caPasswordInput) 
    caEncryptedText = caTextInput; 
    caPassword = caPasswordInput; 
    caPasswordLength = string.len(caPassword); 
    decrypt(); 
    return (caText); 
end 
  
verschl = encryptString("Test","Password"); 
plaintext = decryptString(verschl,"Password"); 
print(addEncryptionPasswordChar(subsEncryptionPasswordChar(1,1),1)); 
print(plaintext); 

The only problem about it is that the output is three times larger then the input, since I found no way of doing a bitwise operations in LUA.

Using this algorithm i encrypted:

A simple LUA encryption

to:

888953271697817971988875429150003339473916115385183229145025698478333

and

123 test

to

589213431067573308035761

and

1234567890

to

599957627210660816794255945607

and

abcdefghijklmnopqrstuvwxyz

to

363695469390506373834224316510624908352602626076666225352477454426305393478617

The password is 7 chars long.

The algorithm is known, the plain text is known, if you are able to find out the password, I consider this algorithm to be a failure.

Link to comment
So where's this encrypted version of the 7 char length password, that you want us to decrypt?

with that secret password i encrypted:

A simple LUA encryption

to:

888953271697817971988875429150003339473916115385183229145025698478333

and

123 test

to

589213431067573308035761

and

1234567890

to

599957627210660816794255945607

and

abcdefghijklmnopqrstuvwxyz

to

363695469390506373834224316510624908352602626076666225352477454426305393478617

I just want you to BREAK the algorithm.

All of these lines use the same password.

If you want to encrypt/decrypt something yourself, be sure to use ASCII text and an ASCII password.

# verschl = encryptString("Test","Password");

# plaintext = decryptString(verschl,"Password");

I tested it with random text several times.

When you find out the password (which is 7 chars in size), my algorithm fails (plain text attack).

Imagining storing data client side and having the client find out the decryption password, i.e. by saving its position at a specified place!

I know the password by heart, so, if you want me to encrypt MOAR, then I'll do it.

I also have this algorithm in C, so, if you want to have the sourcecode for that, I should be able to provide it (even though i changed some things in the LUA sourcecode, i.e. adding the password in every step of the encryption).

Edit:

I found out there may be a bug.

The reason is the "iPasswordPosition" integer.

This bug should be solved easily.

Edit2:

Even though there is a "bug", it works, and I have no idea why.

iPasswordPosition is set to 1 every time.

Then, it is increased.

Why the hack does it decrypt the same way it encrypts and still work?

Link to comment

Sorry for double posting, sorry for failing so hard:

I made a mistake in the implementation.

The password wasn't added (line 15 and line 27)

  
iaEncryptionArray = {}; 
iaDecryptionArray = {}; 
caPassword = ""; 
iPasswordLength = 0; 
caText = ""; 
caEncryptedText = ""; 
iPasswordPosition = 1; 
  
function addEncryptionPasswordChar(value) 
    iPasswordPosition = iPasswordPosition + 1; 
    if (iPasswordPosition > iPasswordLength) then 
        iPasswordPosition = 1; 
    end 
    output = value + string.byte(caPassword,iPasswordPosition); 
    if (output > 999) then 
        output = output - 1000; 
    end 
    return output; 
end 
  
function subsEncryptionPasswordChar(value) 
    iPasswordPosition = iPasswordPosition - 1; 
    if (iPasswordPosition < 1) then 
        iPasswordPosition = iPasswordLength; 
    end 
    output = value - string.byte(caPassword,iPasswordPosition); 
    if (output < 0) then 
        output = output + 1000; 
    end 
    return output; 
end 
  
function generateEncryptionPassword() 
    iPasswordHash = 0; 
    caOutput = ""; 
    for i=1, iPasswordLength, 1 do 
        iPasswordHash = iPasswordHash + string.byte(caPassword,i); -- char! 
    end 
    for i=1, iPasswordLength, 1 do 
        caOutput = caOutput .. string.char(iPasswordHash % string.byte(caPassword,i)); -- char! char! 
    end 
    return caOutput; 
end 
  
function stringSetChar(string,char,pos) 
    length = string.len(string); 
    if pos > length then 
        return string; 
    end 
    string = string.sub(string,1,pos) .. char .. string.sub(string,pos+1,length); 
    return string; 
end 
  
function generateEncryptionArray() 
    generateDecryptionArray(); 
end 
  
function generateDecryptionArray() 
    caEncryptionPassword = generateEncryptionPassword(caPassword); 
    for i=0, 999, 1 do 
        iaEncryptionArray[i]=NIL; 
    end 
    iPosition = 0; 
    n=1; 
    for i=0, 999, 1 do 
        n = n + 1; 
        if (string.byte(caPassword,n) == NIL) then 
            n=1; 
        end 
        iPosition = iPosition + string.byte(caEncryptionPassword,n) + string.byte(caPassword,n); 
        iPosition = iPosition % 1000; 
        while (iaEncryptionArray[iPosition]) do 
            iPosition = iPosition + 1; 
            if (iPosition >= 1000) then 
                iPosition = iPosition - 1000; 
            end 
        end 
        iaEncryptionArray[iPosition] = i; 
        iaDecryptionArray[i] = iPosition; 
    end 
end 
  
function encrypt() 
    generateEncryptionArray(); 
    iTextSize = string.len(caText); 
    iEncryptedTextSize = iTextSize*3; 
    caEncryptedText = ""; 
    aEncryptedText = {}; 
    for i=1, iTextSize, 1 do 
        value = string.byte(caText,i); 
        n=i*3; 
        if (value < 10) then 
            value = string.format("00%i",value); 
        elseif (value < 100) then 
            value = string.format("0%i",value); 
        else 
            value = string.format("%i",value); 
        end 
        aEncryptedText[(i-1)*3+1] = string.byte(value,1) - 48; 
        aEncryptedText[(i-1)*3+2] = string.byte(value,2) - 48; 
        aEncryptedText[(i-1)*3+3] = string.byte(value,3) - 48; 
    end 
    iCounter = 1; 
    iPasswordPosition = 1; 
    for runs=1, 32, 1 do 
        for i=1, iEncryptedTextSize, 1 do 
            iCounter = iCounter + 1; 
            pos1 = i; 
            pos2 = i+1; 
            pos3 = i+2; 
            if (pos2 > iEncryptedTextSize) then 
                pos2 = pos2 - iEncryptedTextSize; 
                pos3 = pos3 - iEncryptedTextSize; 
            elseif (pos3 > iEncryptedTextSize) then 
                pos3 = pos3 - iEncryptedTextSize; 
            end 
            h = aEncryptedText[pos1]; 
            z = aEncryptedText[pos2]; 
            e = aEncryptedText[pos3]; 
            value = 100*h + 10*z + e; 
            value = iaEncryptionArray[value]; 
            value = subsEncryptionPasswordChar(value); 
            h = math.floor(value / 100); 
            value = value - 100*h; 
            z = math.floor(value / 10); 
            value = value - 10*z; 
            e = value; 
            aEncryptedText[pos1] = h; 
            aEncryptedText[pos2] = z; 
            aEncryptedText[pos3] = e; 
        end 
    end 
    caEncryptedText = ""; 
    for i=1,iEncryptedTextSize,1 do 
        caEncryptedText = caEncryptedText .. string.format("%i",aEncryptedText[i]); 
    end 
end 
  
function decrypt() 
    generateDecryptionArray(); 
    iEncryptedTextSize = string.len(caEncryptedText); 
    aEncryptedText = {}; 
    for i=1,iEncryptedTextSize,1 do 
        aEncryptedText[i] = string.byte(caEncryptedText,i) - 48; 
    end 
    caEncryptedText = ""; 
    iPasswordPosition = - (iEncryptedTextSize*32) - 1; 
    iPasswordPosition = iPasswordPosition % iPasswordLength + 1; 
    for runs=1, 32, 1 do 
        for i=iEncryptedTextSize, 1, -1 do 
            pos1 = i; 
            pos2 = i+1; 
            pos3 = i+2; 
            if (pos2 > iEncryptedTextSize) then 
                pos2 = pos2 - iEncryptedTextSize; 
                pos3 = pos3 - iEncryptedTextSize; 
            elseif (pos3 > iEncryptedTextSize) then 
                pos3 = pos3 - iEncryptedTextSize; 
            end 
            h = aEncryptedText[pos1]; 
            z = aEncryptedText[pos2]; 
            e = aEncryptedText[pos3]; 
            value = 100*h + 10*z + e; 
            value = addEncryptionPasswordChar(value); 
            value = iaDecryptionArray[value]; 
            h = math.floor(value / 100); 
            value = value - 100*h; 
            z = math.floor(value / 10); 
            value = value - 10*z; 
            e = value; 
            aEncryptedText[pos1] = h; 
            aEncryptedText[pos2] = z; 
            aEncryptedText[pos3] = e; 
        end 
    end 
    iTextSize = iEncryptedTextSize / 3 
    caText = ""; 
    for i=1,iEncryptedTextSize,1 do 
        caEncryptedText = caEncryptedText .. string.format("%i",aEncryptedText[i]); 
    end 
    for i=1, iTextSize, 1 do 
        n = (i-1)*3+1; 
        char = aEncryptedText[n]*100 + aEncryptedText[n+1]*10 + aEncryptedText[n+2]; 
        if char > 255 then 
            caText = "wrong password!"; 
            return; 
        end 
        caText = caText .. string.char(char); 
    end 
end 
  
function encryptString(caTextInput,caPasswordInput) 
    if caTextInput == nil or caPasswordInput == nil then 
        return nil; 
    end 
    caText = caTextInput; 
    caPassword = caPasswordInput; 
    iPasswordLength = string.len(caPassword); 
    encrypt(); 
    return (caEncryptedText); 
end 
  
function decryptString(caTextInput,caPasswordInput) 
    if caTextInput == nil or caPasswordInput == nil then 
        return nil; 
    end 
    caEncryptedText = caTextInput; 
    caPassword = caPasswordInput; 
    iPasswordLength = string.len(caPassword); 
    decrypt(); 
    return (caText); 
end 
  

The string:

A simple LUA encryption

Was encrypted to:

682119734823703509077954997710015337233693472802500191840149774364729

The string:

1234567890

Was encrypted to:

933712879701133814501918158559

The string:

abcdefghijklmnopqrstuvwxyz

Was encrypted to:

530526185999974651768906379994835597147588329912489932067818658885112960517611

The string:

TEST

Was encrypted to:

627098568592

The string:

one

Was encrypted to:

454931977

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...