Jump to content

Python Cipher


Recommended Posts

I made a low cipher for my autologin system to encrypt user passwords.

I will make it harder to decrypt with autokey methods etc.

Feel free to use this version if you want.:

class VIGENERE:
    def __init__(self):
        self.table = []

    def CreateTable(self, start_char="a", end_char="z", shift=0):
        try:
            ord(start_char)
        except:
            start_char = chr(start_char)
        try:
            ord(end_char)
        except:
            end_char = chr(end_char)
        start, end = ord(start_char)-(shift+1), ord(end_char)+1
        for j in range(end-start):
            j += shift
            self.table.append([])
            for i in range(start+1, end):
                self.table[j-shift].append(chr(i+j if i+j < end else i+j-(end-start)))
                #print(chr(i+j if i+j < end else i+j-(end-start)), end=" ")
                #print("")

    def Check(self, value, row):
        for j in enumerate(row):
            if j[1] == value:
                return j[0]
        else:
            return False

    def CreateKey(self, data, key):
        long_key = ""
        x = -1
        #print("Creating long key from '{0}' to '{1}'...".format(key, data))
        for k in range(len(data)):
            x += 1
            try:
                long_key += key[x]
            except:
                x = 0
                long_key += key[x]

        #print("Created long key : '{0}'".format(long_key))
        return long_key

    def Encrypt(self, data, key):
        encrypted = ""
        err = ""
        key = self.CreateKey(data, key)
        for char in enumerate(data):
            col_char_pos = self.Check(char[1], self.table[0])
            row_char_pos = self.Check(key[char[0]], self.table[0])
            if col_char_pos != None and row_char_pos != None:
                #print("Encrypting '{0}' to '{1}'...".format(char[1], self.table[row_char_pos][col_char_pos]))
                encrypted += self.table[col_char_pos][row_char_pos]
            else:
                if not col_char_pos:
                    err += "This character: '{0}' is not in this row: '{1}'".format(char[1], self.table[0]) + "\n"
                if not row_char_pos:
                    err += "This character: '{0}' is not in this row: '{1}'".format(key[char[0]], self.table[0]) + "\n"
                return err
                
        return encrypted


    def Decrypt(self, data, key):
        decrypted = ""
        err = ""
        key = self.CreateKey(data, key)
        for char in enumerate(data):
            col_char_pos = self.Check(char[1], self.table[0])
            row_char_pos = self.Check(key[char[0]], self.table[0])
            if col_char_pos != None and row_char_pos != None:
                #print("Decrypting '{0}' to '{1}'".format(char[1], self.table[0][self.Check(self.table[row_char_pos][self.Check(char[1], self.table[row_char_pos])], self.table[row_char_pos])]))
                decrypted += self.table[0][self.Check(self.table[row_char_pos][self.Check(char[1], self.table[row_char_pos])], self.table[row_char_pos])]
            else:
                if not col_char_pos:
                    err += "This character: '{0}' is not in this row: '{1}'".format(char[1], self.table[0]) + "\n"
                if not row_char_pos:
                    err += "This character: '{0}' is not in this row: '{1}'".format(key[char[0]], self.table[0]) + "\n"
                return err
        return decrypted

if __name__ == "__main__":
    cipher = VIGENERE()
    cipher.CreateTable("a", "z", 70)
    encrypted = cipher.Encrypt("abcdyz", "tfkusgfhzenmaue")
    decrypted = cipher.Decrypt(encrypted, "tfkusgfhzenmaue")
    print(encrypted, decrypted)

 

Edited by pixelshasHUN
i corrected the error handling. The "a" letter is equal to 0 in the table and 0 is equal to False, so the first letter in the table never encrypted.
Link to comment
Share on other sites

Why did you overcomplicate this? 

There are shorter ways like the following:

def xor(data, key): 
    input = str(data)
    no_of_itr = len(input) 
    output = ""
    for i in range(no_of_itr):
        current = input[i]
        current_key = key[i%len(key)]
        output += chr(ord(current) ^ ord(current_key))
    return output

secretkey = "mysecretkey" 
secretpassword = "mysecretpassword"

ciphertext = xor(secretpassword, secretkey) 
print ciphertext 

decrypted = xor(ciphertext, secretkey) 
print decrypted

print secretpassword == str(decrypted)

Anyway good post. 

  • Think 1
  • Scream 1

All wolves are gray in the dark.

Link to comment
Share on other sites

This is an other cipher method what you wrote. Your method is XOR, but yeah XOR is shorter than Vigenère.  Thanks, I think i will use this method for make Vigenère harder.

The big difference between the two methods that Vigenère cant encrypt and decrypt the text with the same function.

Edited by pixelshasHUN
Link to comment
Share on other sites

On 6/26/2021 at 1:33 PM, pixelshasHUN said:

 

...

 

It does no matter on current situation.

If we want a rly secure encrypter, we have to go into c++ source, you can make a rly productive system with cython. But it does not need for a simple autologin system, because the login data (password) is always on player's computer, so in a normal situation that is not touchable by 2nd person to steal it. BUT if someone wants crypt his player's password on their pc, here is a posible way like your work. 🙂

All wolves are gray in the dark.

Link to comment
Share on other sites

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.