christian bender date 21 12 2017 class xorcipher this class implements the xorcipher algorithm and provides some useful methods for encrypting and decrypting strings and files overview about methods encrypt list of char decrypt list of char encryptstring str decryptstring str encryptfile boolean decryptfile boolean simple constructor that receives a key or uses default key 0 private field input content of type string and key of type int output encrypted string content as a list of chars if key not passed the method uses the key by the constructor otherwise key 1 empty list xorcipher encrypt 5 one key xorcipher encrypthallo welt 1 i m m n v d m u normal key xorcipher encrypthallo welt 32 h a l l o x00 w e l t key greater than 255 xorcipher encrypthallo welt 256 h a l l o w e l t precondition make sure key is an appropriate size input content of type list and key of type int output decrypted string content as a list of chars if key not passed the method uses the key by the constructor otherwise key 1 empty list xorcipher decrypt 5 one key xorcipher decrypthallo welt 1 i m m n v d m u normal key xorcipher decrypthallo welt 32 h a l l o x00 w e l t key greater than 255 xorcipher decrypthallo welt 256 h a l l o w e l t precondition make sure key is an appropriate size input content of type string and key of type int output encrypted string content if key not passed the method uses the key by the constructor otherwise key 1 empty list xorcipher encryptstring 5 one key xorcipher encryptstringhallo welt 1 immn vdmu normal key xorcipher encryptstringhallo welt 32 hallox00welt key greater than 255 xorcipher encryptstringhallo welt 256 hallo welt precondition make sure key is an appropriate size this will be returned input content of type string and key of type int output decrypted string content if key not passed the method uses the key by the constructor otherwise key 1 empty list xorcipher decryptstring 5 one key xorcipher decryptstringhallo welt 1 immn vdmu normal key xorcipher decryptstringhallo welt 32 hallox00welt key greater than 255 xorcipher decryptstringhallo welt 256 hallo welt precondition make sure key is an appropriate size this will be returned input filename str and a key int output returns true if encrypt process was successful otherwise false if key not passed the method uses the key by the constructor otherwise key 1 precondition make sure key is an appropriate size actual encryptprocess input filename str and a key int output returns true if decrypt process was successful otherwise false if key not passed the method uses the key by the constructor otherwise key 1 precondition make sure key is an appropriate size actual encryptprocess tests crypt xorcipher key 67 test encrypt printcrypt encrypthallo welt key test decrypt printcrypt decryptcrypt encrypthallo welt key key test encryptstring printcrypt encryptstringhallo welt key test decryptstring printcrypt decryptstringcrypt encryptstringhallo welt key key if crypt encryptfiletest txt key printencrypt successful else printencrypt unsuccessful if crypt decryptfileencrypt out key printdecrypt successful else printdecrypt unsuccessful simple constructor that receives a key or uses default key 0 private field input content of type string and key of type int output encrypted string content as a list of chars if key not passed the method uses the key by the constructor otherwise key 1 empty list xorcipher encrypt 5 one key xorcipher encrypt hallo welt 1 i m m n v d m u normal key xorcipher encrypt hallo welt 32 h a l l o x00 w e l t key greater than 255 xorcipher encrypt hallo welt 256 h a l l o w e l t precondition make sure key is an appropriate size input content of type list and key of type int output decrypted string content as a list of chars if key not passed the method uses the key by the constructor otherwise key 1 empty list xorcipher decrypt 5 one key xorcipher decrypt hallo welt 1 i m m n v d m u normal key xorcipher decrypt hallo welt 32 h a l l o x00 w e l t key greater than 255 xorcipher decrypt hallo welt 256 h a l l o w e l t precondition make sure key is an appropriate size input content of type string and key of type int output encrypted string content if key not passed the method uses the key by the constructor otherwise key 1 empty list xorcipher encrypt_string 5 one key xorcipher encrypt_string hallo welt 1 i mmn vdmu normal key xorcipher encrypt_string hallo welt 32 hallo x00welt key greater than 255 xorcipher encrypt_string hallo welt 256 hallo welt precondition make sure key is an appropriate size this will be returned input content of type string and key of type int output decrypted string content if key not passed the method uses the key by the constructor otherwise key 1 empty list xorcipher decrypt_string 5 one key xorcipher decrypt_string hallo welt 1 i mmn vdmu normal key xorcipher decrypt_string hallo welt 32 hallo x00welt key greater than 255 xorcipher decrypt_string hallo welt 256 hallo welt precondition make sure key is an appropriate size this will be returned input filename str and a key int output returns true if encrypt process was successful otherwise false if key not passed the method uses the key by the constructor otherwise key 1 precondition make sure key is an appropriate size actual encrypt process input filename str and a key int output returns true if decrypt process was successful otherwise false if key not passed the method uses the key by the constructor otherwise key 1 precondition make sure key is an appropriate size actual encrypt process tests crypt xorcipher key 67 test encrypt print crypt encrypt hallo welt key test decrypt print crypt decrypt crypt encrypt hallo welt key key test encrypt_string print crypt encrypt_string hallo welt key test decrypt_string print crypt decrypt_string crypt encrypt_string hallo welt key key if crypt encrypt_file test txt key print encrypt successful else print encrypt unsuccessful if crypt decrypt_file encrypt out key print decrypt successful else print decrypt unsuccessful
from __future__ import annotations


class XORCipher:
    def __init__(self, key: int = 0):
        

        
        self.__key = key

    def encrypt(self, content: str, key: int) -> list[str]:
        

        
        assert isinstance(key, int)
        assert isinstance(content, str)

        key = key or self.__key or 1

        
        key %= 256

        return [chr(ord(ch) ^ key) for ch in content]

    def decrypt(self, content: str, key: int) -> list[str]:
        

        
        assert isinstance(key, int)
        assert isinstance(content, str)

        key = key or self.__key or 1

        
        key %= 256

        return [chr(ord(ch) ^ key) for ch in content]

    def encrypt_string(self, content: str, key: int = 0) -> str:
        

        
        assert isinstance(key, int)
        assert isinstance(content, str)

        key = key or self.__key or 1

        
        key %= 256

        
        ans = ""

        for ch in content:
            ans += chr(ord(ch) ^ key)

        return ans

    def decrypt_string(self, content: str, key: int = 0) -> str:
        
        assert isinstance(key, int)
        assert isinstance(content, str)

        key = key or self.__key or 1

        
        key %= 256

        
        ans = ""

        for ch in content:
            ans += chr(ord(ch) ^ key)

        return ans

    def encrypt_file(self, file: str, key: int = 0) -> bool:
        
        assert isinstance(file, str)
        assert isinstance(key, int)

        
        key %= 256

        try:
            with open(file) as fin, open("encrypt.out", "w+") as fout:
                
                for line in fin:
                    fout.write(self.encrypt_string(line, key))

        except OSError:
            return False

        return True

    def decrypt_file(self, file: str, key: int) -> bool:
        
        assert isinstance(file, str)
        assert isinstance(key, int)

        
        key %= 256

        try:
            with open(file) as fin, open("decrypt.out", "w+") as fout:
                
                for line in fin:
                    fout.write(self.decrypt_string(line, key))

        except OSError:
            return False

        return True


if __name__ == "__main__":
    from doctest import testmod

    testmod()