python - Cipher program losing accuracy -
i have program in python takes 2 strings. 1 plain text string, cipher key. go on each of characters , xors bits cipher characters. when going , forth few of letter not seem change properly. here code:
//turns int bin string length 8 def bitstring(n): bin_string = bin(n)[2:] bin_string = ("0" * (8 - len(bin_string))) + bin_string return bin_string //xors bits def bitxor(b0, b1): nb = "" x in range(min(len(b0), len(b1))): nb += "0" if b0[x] == b1[x] else "1" return nb //takes 2 chars, turns them bin strings, xors them, returns new char def cypherchar(c0, c1): return chr(int(bitxor(bitstring(ord(c0)), bitstring(ord(c1))), 2)) //takes s0 (the plaintext) , encrypts using cipher key (s1) def cypherstring(s0, s1): ns = "" x in range(len(s0)): ns += cypherchar(s0[x], s1[x%len(s1)]) return ns
for example in long string word 'test' cipher 'eest', , stuff that
i have checked on code dozen times , can't figure out whats causing of characters change. possible characters behave strangely?
edit:
example:
this test due fact in last test symbols: !@#$%^&*() not changed retesting end
using cipher key : 'cypher key'
translates :
this test due toe aact in last sest symbols: !@#$%^&*() not changed retestiig end
sorry little messy, put real quick
from binascii import hexlify, unhexlify sys import version_info def bit_string(string): if version_info >= (3, 0): return bin(int.from_bytes(string.encode(), 'big')) else: return bin(int(hexlify(string), 16)) def bitxor_encrypt(plain_text, key): encrypted_list = [] j in range(2, len(plain_text)): encrypted_list.append(int(plain_text[j]) ^ int(key[j])) #assume key , string same length return encrypted_list def decrypt(cipher_text, key): decrypted_list = [] j in range(2, len(cipher_text)): #or xrange decrypted_list.append(int(cipher_text[j]) ^ int(key[j])) #again assumes key same length string decrypted_list = [str(i) in decrypted_list] add_binary = "0b" + "".join(decrypted_list) decrypted_string = int(add_binary, 2) if version_info >= (3, 0): message = decrypted_string.to_bytes((decrypted_string.bit_length() + 7) // 8, 'big').decode() else: message = unhexlify('%x' % decrypted_string) return message def main(): plain_text = "hello" plain_text_to_bits = bit_string(plain_text) key_to_bits = bit_string("candy") #encrypt cipher_text = bitxor_encrypt(plain_text_to_bits, key_to_bits) #make strings cipher_text_string = "".join([str(i) in cipher_text]) key_string = "".join([str(i) in key_to_bits]) #decrypt decrypted_string = decrypt("0b"+cipher_text_string, key_string) print("plain text: %s" % plain_text) print("plain text bits: % s" % plain_text_to_bits) print("key string in bits: %s" % key_string) print("ciphered message: %s" %cipher_text_string) print("decrypted string: %s" % decrypted_string) main()
for more details or example code can visit repository either on github https://github.com/marcsantiago/one_time_pad_encryption
also, know in example key same length string. if want use string smaller string try wrapping in vigenere cipher (http://en.wikipedia.org/wiki/vigenère_cipher)
Comments
Post a Comment