Facebook
From Arash, 2 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 113
  1. import random
  2. import math as floor
  3.  
  4.  
  5. def genKys(length, choice):
  6.     key = [0] * length
  7.     for i in range(choice):
  8.         key[floor.floor(random.random() * (length - 1))] = 1
  9.     return key
  10.  
  11. def enc(plain, key):
  12.     length = len(key)
  13.     cipher = [None] * length
  14.     for i in range(length):
  15.         cipher[i] = 1 if key[i] == plain[i] else 0
  16.     return cipher
  17.  
  18. def getBts(plain):
  19.     length = len(plain)
  20.     bits = [None] * length * 8
  21.     for i in range(length):
  22.         for j in range(8):
  23.             bits[8 * i + 7 - j] = (int)(ord(plain[i]) / pow(2, j)) % 2
  24.     return bits
  25.  
  26. def main():
  27.     txt = input("Enter cipher text : ")
  28.     length = len(txt)
  29.     key = genKys(length * 8, floor.floor(random.random() * length * 8))
  30.     plain = getBts(txt)
  31.     print("plaintext : ")
  32.     print(plain)
  33.     cipher = enc(plain, key)
  34.     print("ciphertext : ")
  35.     print(cipher)
  36.     deciphered = enc(cipher, key)
  37.     print("deciphered : ")
  38.     print(deciphered)
  39.  
  40. if __name__ == "__main__":
  41.     main()

Replies to modules rss

Title Name Language When
Re: modules Arash python 2 Years ago.