XOR Cipher | The same function is used to encrypt and decrypt ; Define XOR key Any character value will work ; calculate length of input string ; perform XOR operation of key with every character in string ; Driver Code ; Encrypt the string ; Decrypt the string
def encryptDecrypt ( inpString ) : NEW_LINE INDENT xorKey = ' P ' ; NEW_LINE length = len ( inpString ) ; NEW_LINE for i in range ( length ) : NEW_LINE INDENT inpString = ( inpString [ : i ] + chr ( ord ( inpString [ i ] ) ^ ord ( xorKey ) ) + inpString [ i + 1 : ] ) ; NEW_LINE print ( inpString [ i ] , end = " " ) ; NEW_LINE DEDENT return inpString ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT sampleString = " GeeksforGeeks " ; NEW_LINE print ( " Encrypted ▁ String : ▁ " , end = " " ) ; NEW_LINE sampleString = encryptDecrypt ( sampleString ) ; NEW_LINE print ( " " ) ; NEW_LINE print ( " Decrypted ▁ String : ▁ " , end = " " ) ; NEW_LINE encryptDecrypt ( sampleString ) ; NEW_LINE DEDENT