Take the 2-minute tour ×
Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It's 100% free, no registration required.

I decided to program a relatively trivial encryption algorithm in my downtime. This algorithm takes a seed from a user (supports floating point seeds), and then adds 1 to the seed for every iteration of each character of a message, performing a bitwise xor with a pseudorandomly generated value. As Python uses Mersenne twister, a non-cryptographic pseudorandom generator, I figured that this encryption algorithm must have some vulnerabilities, as Mersenne twister is not cryptographically secure.

To name a few:

  1. Can an encrypted message be plaintext attacked like a regular low byte key XOR encryption?

  2. Can an attacker guess the value of the seed based upon patterns in the encrypted message?

  3. Can multiple inputted seeds end up with the same encrypted message?

    import random
    import codecs
    import binascii
    import math
    def encrypt(x):
        y = float(input("Enter the encryption key  -->"))
        random.seed(y)
        i = 0
        x = list(x)
        while i < len(x):
            y += 1
            rand = random.randint(1,random.randint(200,400))
            random.seed(rand)
            x[i] = chr(ord(x[i]) ^ rand ^ abs((~int(rand/2))))
            i += 1
        x = "".join(x)
        x = str(binascii.hexlify(x.encode("utf-8")))
        x = list(x)
        x.pop(x.index("'"));x.pop(x.index("b"));x.pop(x.index("'"))
        return "".join(x)
    def decrypt(x):
        x = bytes.fromhex(x).decode('utf-8')
        y = float(input("Enter the decryption key -->"))
        random.seed(y)
        i = 0
        x = list(x)
        while i < len(x):
            y += 1
            rand = random.randint(1,random.randint(200,400))
            random.seed(rand)
            x[i] = chr(ord(x[i]) ^ rand ^ abs((~int(rand/2))))
            i += 1
        x = "".join(x)
        return x
    def instructions():
        print("Welcome to the RBES3 (Randomized Bitwise Encryption Standard 3) symmetric key encryption program.")
        print("Choose to either decrypt or encrypt your string in this program. \n")
    def main():
        x = input("Would you like decrypt encrypt a string? (d for decrypt, e for encrypt) -->")
        if x == "d":
            args = input("Would you like to decrypt a string or a file? (s/f) -->").lower()
            if args == "f":
                filename = input("Enter the path to the file that you want to decrypt. -->")
                file = open(filename,"r+")
                dcrypt = open("_"+filename[-(len(filename)//3+1)::]+"_decrypted", "w") 
                try:
                    done = decrypt(file.readlines()[0])
                    print("The decrypted string is --> "+done)
                    dcrypt.write(done)
                    file.close()
                except:
                    print("\nOne or more of your parameters were invalid.  Please check your input and try again.  If you are using an ASCII encoding, check your key, as ASCII does not support special characters.")
            if args == "s":
                string = input("Enter a string to be decrypted here. -->")
                try:
                    print("The decrypted string is --> "+decrypt(string))
                except:
                    print("\nOne or more of your parameters were invalid.  Please check your input and try again.")
        if x == "e":
            args = input("Would you like to encrypt a string or a file? (s/f) -->").lower()
            if args == "f":
                filename = input("Enter the path to the file that you want to encrypt. -->")
                file = open(filename,"r+")
                ecrypt = open("_"+filename[-(len(filename)//3+1)::]+"_encrypted", "w") 
                try:
                    done = encrypt(file.readlines()[0])
                    print("The decrypted string is --> "+done)
                    ecrypt.write(done)
                    file.close()
                except:
                    print("\nOne or more of your parameters were invalid.  Please check your input and try again.")
          if args == "s":
                string = input("Enter a string to be encrypted here. -->")
                try:
                    print("The encrypted string is --> "+encrypt(string))
                except:
                    print("\nOne or more of your parameters were invalid.  Please check your input and try again.")
    main()
    

Examples:

    "This is a test message" (no quotes)
    seed = 29
    encrypted message: 670967c2ad34345ac6a053c295c2827cc3bbc7ac6ac68a1ec291c2b978c3afc7bd
share

migration rejected from security.stackexchange.com May 5 at 7:07

This question came from our site for Information security professionals. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as off-topic by CodesInChaos, poncho, yyyyyyy, Reid, cygnusv May 5 at 7:07

  • This question does not appear to be about cryptography within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

14  
Vulnerability #1 - you're rolling your own crypto. –  Iszi Apr 1 at 4:20
3  
@lszi I suspect the author already knows that. And that's a cause of vulnerabilities, but it's not actually a vulnerability. –  immibis Apr 1 at 7:53
4  
I'm voting to close this question as off-topic because it's about breaking a homebrew cipher. –  CodesInChaos May 4 at 14:32

Browse other questions tagged or ask your own question.