jeudi 23 novembre 2017

Why is this occuring?

I have made a custom encryption program (Yes, I know encryption libraries exist...) but there's something weird going on. I'll explain after I post the code. (so you have something to reference) (Oh, and I'm sorry about the huge amounts of code)

from random import seed, sample


def strseed(string):
    a = 0
    n = 0
    for x in string:
        n = n + 1
        a = a + (ord(x) * n)
    return a

def index():
    out = []
    for w in range(0, 4):
        for x in range(0, 4):
            for y in range(0, 4):
                for z in range(0, 4):
                    out.append(chr(w + 97) + chr(x + 97) + chr(y + 97) + chr(z + 97))
    return out

def decode(quadrep):
    w = ((ord(quadrep[0]) - 97) * 64)
    x = ((ord(quadrep[1]) - 97) * 16)
    y = ((ord(quadrep[2]) - 97) * 4)
    z = (ord(quadrep[3]) - 97)
    return chr(w + x + y + z)

def encode(char):
    return index()[ord(char)]


def enchar(char, key):
    a = encode(char)
    b = []
    for char in a:
        b.append("")
    for x in range(len(a)):
        b[key[x]] = a[x]
    return decode(''.join(b))

def enstr(string, siid):
    seed(siid)
    c = []
    for x in string:
        c.append(enchar(x, sample(range(4), 4)))
    return ''.join(c)

def dechar(char, key):
    a = encode(char)
    b = []
    for char in a:
        b.append("")
    for x in range(len(a)):
        b[x] = a[key[x]]
    return decode(''.join(b))

def destr(string, siid):
    seed(siid)
    c = []
    for x in string:
        c.append(dechar(x, sample(range(4), 4)))
    return ''.join(c)

Just bare with me, we're almost done...

import sys
sys.path.append("C:/Users/basse/Desktop/Encryption Level II")
from resources import enstr, destr, strseed


a = input("Encrypt: ")
passw = input("Password: ")
gradient = int(input("Encryption Gradient: "))

for x in range(0,gradient):
    a = enstr(a, strseed(passw))

print("->" + a + "<-")

for x in range(0, gradient):
    a = destr(a, strseed(passw))

print("Decrypt test: " + a)

If you didn't understand what's happening here, in resources.py I have included a few functions. One of those is my own data format, which looks like this: abcd aaaa is 0, aaab is 1, aaba is 4, abaa is 16, baaa is 64, etc.

What encode does is convert characters to this format. for example:

"a" = chr(97) = decode("bacb")
index()[97] = "bcab" 

Get it?

Well,

enchr(char) 

takes the value produced by

 encode()

and scrambles it with a key.

so,

enchr("a", [1,0,3,2])

would return "abdc"

enstr(string, siid)

that does the same thing enchr does, but uses a seed to produce a set of random.sample()'s to produces an encrypted string using the siid parameter.

strseed(string)

does something helpful too, produces a random seed that's specific to that string, based on both the order of characters and what they are.

I'm sorry about the lengthy explanation, but there was a lot to explain.

Well, in my second code snippet, I discovered something. When it asks you for the "Gradient", it's really asking how many times to redundantly encrypt the same string (by encrypting the output of the previously executed enstr with the same seed). I put in 120. What I got was the same exact output as the input. For example,

Encrypt: Hello
Password: password
Gradient: 120
->Hello<- # this is what the encrypter outputs.
Decrypt test: Hello

In case you didn't pick up on it, ->Hello<- is supposed to be a string of randomly assorted characters from the ASCII set.

Encrypt: Hello
Password: password
Gradient: 119
->Hr·<-
Decrypt test: Hello

That's what it should look like. Go ahead and try it with any password, the output with a gradient of 120 is always the same as the input.

Why is this happening? Really, I have been racking my brain for hours, and can't even think up an explanation for this phenomenon. I thought that it was just a coincidence with the password. that one specific password, iterations, and string had produced that output. But, I changed around all of these variables except for the Gradient, and got the same thing. If you know why this is happening, please tell me how I can fix it as well. I'm just downright confused by this. Thanks for sticking with me to the end, I know it looks like I'm advertising my program but I'm really just hoping someone can explain this to me. (P.S, I know using a password as a seed is really insecure, but I'm not looking for criticism. I'm looking for an explanation)




Aucun commentaire:

Enregistrer un commentaire