ghost423543

AES128-CTR_(fail_setup)

May 3rd, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.29 KB | None | 0 0
  1. import sys
  2. import base64
  3. import itertools
  4. from Crypto.Cipher import AES
  5. from Crypto import Random
  6.  
  7.  
  8. class Nonce(object):
  9.     def __init__(self):
  10.         self.value = Random.new().read(AES.block_size)
  11.     def __call__(self):## reason why each block same-key
  12.         return self.value
  13.  
  14. def encrypt_line(cipher, line):
  15.     return base64.b64encode(cipher.encrypt(line))
  16.  
  17. def encrypt_file(filename):
  18.     key = Random.new().read(16)
  19.     nonce = Nonce()
  20.     ## same_Nonce; same_key
  21.     f_in = open(filename, "r")
  22.     f_out = open(filename + ".crypt", "w")
  23.    
  24.     for line in f_in.read().split("\n"):
  25.         if len(line) > 1:
  26.             cipher = AES.new(key, AES.MODE_CTR, counter=nonce)
  27.             line_enc = encrypt_line(cipher, line)
  28.             f_out.write(line_enc + "\n")
  29.        
  30.    
  31. # def main(argv):
  32.     # if len(argv) < 2:
  33.         # sys.stderr.write("Usage : %s <file>\n" % (argv[0]))
  34.         # return 1
  35.    
  36.     # encrypt_file(argv[1])
  37.     # print "File encrypted : " + argv[1] + ".crypt"
  38.    
  39.     # return 0
  40.  
  41. class Hacker:
  42.     def heuristic(self,input_bytes):
  43.         character_frequencies = {
  44.             ' ': 13, 'e': 12.70, 't': 9.06, 'a': 8.17, 'o': 7.51, 'i': 6.97,
  45.             'n': 6.75, 's': 6.33, 'h': 6.09, 'r': 5.99, 'd': 4.25, 'l': 4.03,
  46.             'c': 2.78, 'u': 2.76, 'm': 2.41, 'w': 2.36, 'f': 2.23, 'g': 2.02,
  47.             'y': 1.97, 'p': 1.93, 'b': 1.29, 'v': 0.98, 'k': 0.77, 'j': 0.15,
  48.             'x': 0.15, 'q': 0.10, 'z': 0.07
  49.             }
  50.         return sum([character_frequencies.get(chr(byte), 0) for byte in input_bytes.lower()])
  51.        
  52.     def __init__(self,each_line):
  53.         self.each_line=each_line
  54.         self.key_stream = b''#b'\xd03\xb1%\x0cG\x1b,w\xdd\xc9\xff\xc1qC\xf6\xd03\xb1%\x0cG\x1b,w\xdd\xc9\xff\xc1qC\xf6\xd03\xb1%\x0cG'
  55.         self.len_key = len(self.key_stream)
  56.    
  57.     def decrypt__(self):
  58.         for ciphertext in self.each_line:
  59.             print(bytes([a^b for a,b in zip(ciphertext,itertools.cycle(self.key_stream))]))
  60.    
  61.     def gen_key_ctf_2(self):
  62.         know_plain_text = b"There is another group of people who loudly call themselves hackers, but aren't. These are people (mainly adolescent males) who get a kick out of breaking into computers and phreaking the phone system. Real hackers call these people crackers and want nothing to do with them. Real hackers mostly think crackers are lazy, irresponsible, and not very bright, and object that being able to break security doesn't make you a hacker any more than being able to hotwire cars makes you an automotive engineer. Unfortunately, many journalists and writers have been fooled into using the word hacker to describe crackers; this irritates real hackers no end."
  63.         self.key_stream = bytes([a^b for a,b in zip(know_plain_text,self.each_line[4])])[:16]
  64.         self.len_key = len(self.key_stream)
  65.    
  66.     def gen_key_ctr(self):
  67.         for i in range(self.len_key,max(map(len, self.each_line))):
  68.             column = b''
  69.             for each_cipher_text in self.each_line:
  70.                 column += bytes([each_cipher_text[i]]) if i < len(each_cipher_text) else b''
  71.             self.key_stream += self.get_keystream_byte(column) 
  72.        
  73.     def get_keystream_byte(self,same_col):
  74.         # print(string);input()
  75.         the_char,best_score = 0,0
  76.         for c in range(256):
  77.             cur_score = self.heuristic("".join([chr(c^same_col[i]) for i in range(len(same_col))]).encode('latin-1'))
  78.             if cur_score > best_score:
  79.                 the_char = c
  80.                 best_score = cur_score
  81.         return bytes([the_char])
  82.    
  83. ## https://www2.cs.duke.edu/courses/cps004/spring01/readings/hacker.html
  84. ## nOOBcRYPTOaLWAysFail
  85. if __name__ == "__main__":
  86.     # sys.exit(main(sys.argv))
  87.     cipher_text=b'''h1vQUSwOaAwW/YGeohomhO8=
  88. hFvUBUYmaUsYs+m5qB0m1rNc31FtLnVfV7zpnbQfIJ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text.split(b'\n')]
  96.     hacker = Hacker(each_line)
  97.     hacker.gen_key_ctf_2()
  98.     # hacker.gen_key_ctr()
  99.     hacker.decrypt__()
  100. '''
  101. b'What Is a Hacker?'
  102. b'The Jargon File contains a bunch of definitions of the term "hacker", most having to do with technical adeptness and a delight in solving problems and overcoming limits. If you want to know how to become a hacker, though, only two are really relevant.'
  103. b'There is a community, a shared culture, of expert programmers and networking wizards that traces its history back through decades to the first time-sharing minicomputers and the earliest ARPAnet experiments. The members of this culture originated the term "hacker". Hackers built the Internet. Hackers made the Unix operating system what it is today. Hackers make the World Wide Web work. If you are part of this culture, if you have contributed to it and other people in it know who you are and call you a hacker, you\'re a hacker.'
  104. b'The hacker mind-set is not confined to this software-hacker culture. There are people who apply the hacker attitude to other things, like electronics or music actually, you can find it at the highest levels of any science or art. Software hackers recognize these kindred spirits elsewhere and may call them "hackers" too and some claim that the hacker nature is really independent of the particular medium the hacker works in. But in the rest of this document we will focus on the skills and attitudes of software hackers, and the traditions of the shared culture that originated the term hacker.'
  105. b"There is another group of people who loudly call themselves hackers, but aren't. These are people (mainly adolescent males) who get a kick out of breaking into computers and phreaking the phone system. Real hackers call these people crackers and want nothing to do with them. Real hackers mostly think crackers are lazy, irresponsible, and not very bright, and object that being able to break security doesn't make you a hacker any more than being able to hotwire cars makes you an automotive engineer. Unfortunately, many journalists and writers have been fooled into using the word hacker to describe crackers; this irritates real hackers no end."
  106. b'The basic difference is this: hackers build things, crackers break them.'
  107. b"If you want to be a hacker, keep reading. If you want to be a cracker, go read the alt.2600 newsgroup and get ready to do five to ten in the slammer after finding out you aren't as smart as you think you are. And that's all I'm going to say about crackers."
  108. b'You can validate the challenge with : nOOBcRYPTOaLWAysFail'
  109. b''
  110. '''
Add Comment
Please, Sign In to add comment