def set1_6HelperMain(): #key = "haibaigai" #print(len(key)) #text = "Let KEYSIZE be the guessed length of the key; try values from 2 to (say) 40.\nWrite a function to compute the edit distance/Hamming distance between two strings. The Hamming distance is just the number of differing bits. The distance between:\nis 37. Make sure your code agrees before you proceed.\nFor each KEYSIZE, take the first KEYSIZE worth of bytes, and the second KEYSIZE worth of bytes, and find the edit distance between them. Normalize this result by dividing by KEYSIZE.\nThe KEYSIZE with the smallest normalized edit distance is probably the key. You could proceed perhaps with the smallest 2-3 KEYSIZE values. Or take 4 KEYSIZE blocks instead of 2 and average the distances." #encrypting the text and then b64 represent it #encryptedTextBytes = cyclicXor(bytes(text,'utf-8'), bytes(key,'utf-8')) fpath = "C:\\Programming\\pythonWorkspaces\\cryptopalsWorkspace\\set1\\6.txt" content64 = readAllLinesFromFile(fpath) content64OneLine = ''.join(content64) encryptedTextBytes = base64.b64decode(content64OneLine) #encryptedTextB64 = base64.b64encode(encryptedTextBytes) #print(encryptedTextB64) #cyphering the text by the alg in the question #encryptedTextBytest = base64.b64decode(encryptedTextB64) bestSize = getBestKeySize(encryptedTextBytes, 7)[0][1] chunksOfBestSize = getListOfChunks(encryptedTextBytes, bestSize) oneByteXorLettersList = getListOfColumns(chunksOfBestSize) #cypher the key by using 1ByteXor cypher keyletters = [] for oneByteXorLetters in oneByteXorLettersList: keyletters.append(cypherOneByteXor(oneByteXorLetters, 1)) cypheredKeyBytesList = [res[0][0] for res in keyletters] cypheredKeyBytes = bytearray() for keyByte in cypheredKeyBytesList: cypheredKeyBytes.append(keyByte[0]) print('') print(cypheredKeyBytes) cypheredText = cyclicXor(encryptedTextBytes, cypheredKeyBytes) print('') print(cypheredText)
def s1_8main(): fpath = "C:\\Programming\\pythonWorkspaces\\cryptopalsWorkspace\\set1\\8.txt" contentHex = readAllLinesFromFile(fpath) encryptedTextBytes = [ base64.b64decode(contentLineHex) for contentLineHex in contentHex ] allLinesBlockHisto = [] for encryptedTextLineBytes in encryptedTextBytes: allLinesBlockHisto.append(blocksHistogram(encryptedTextLineBytes, 16)) scores = [] #[(lineNumber, score), ...] for lineBlockHistoLine in range(len(allLinesBlockHisto)): lineBlockHisto = allLinesBlockHisto[lineBlockHistoLine] score = scoreECBblocksHistogram(lineBlockHisto) scores.append((lineBlockHistoLine, score)) scores.sort(key=lambda tup: tup[1]) print(scores[:5])
Attack this cryptosystem piecemeal: guess letters, use expected English language frequence to validate guesses, catch common English trigrams, and so on. Don't overthink it. Points for automating this, but part of the reason I'm having you do this is that I think this approach is suboptimal. """ from set1_4 import readAllLinesFromFile import base64 from s2_11 import generateNRandomeBytes from s3_18 import AES128CTRCipher from set1_6Helper import getListOfColumns from set1_3 import cypherOneByteXor from s3_18 import xorNotSameSize fpath = "C:\\Programming\\pythonWorkspaces\\cryptopalsWorkspace\\set1\\19.txt" contentList = list(map(base64.b64decode, readAllLinesFromFile(fpath))) key = generateNRandomeBytes(16) nonce = (0).to_bytes(8, byteorder='little') CTRCipher = AES128CTRCipher(key) encryptedContentList = [CTRCipher.encrypt(content, nonce) for content in contentList] #somewhat similar to challenge 6 (mostly copy and past from there) oneByteXorLettersList = getListOfColumns(encryptedContentList) #cypher the key by using 1ByteXor cypher keyletters=[] for oneByteXorLetters in oneByteXorLettersList: keyletters.append(cypherOneByteXor(oneByteXorLetters,1)) cypheredKeyBytesList = [res[0][0] for res in keyletters] keyStream = bytearray(); for keyByte in cypheredKeyBytesList:
# -*- coding: utf-8 -*- """ AES in ECB mode The Base64-encoded content in this file has been encrypted via AES-128 in ECB mode under the key "YELLOW SUBMARINE". (case-sensitive, without the quotes; exactly 16 characters; I like "YELLOW SUBMARINE" because it's exactly 16 bytes long, and now you do too). Decrypt it. You know the key, after all. Easiest way: use OpenSSL::Cipher and give it AES-128-ECB as the cipher. Do this with code. You can obviously decrypt this using the OpenSSL command-line tool, but we're having you get ECB working in code for a reason. You'll need it a lot later on, and not just for attacking ECB. """ from set1_4 import readAllLinesFromFile import base64 from Crypto.Cipher import AES key = bytes("YELLOW SUBMARINE", 'utf-8') fpath = "C:\\Programming\\pythonWorkspaces\\cryptopalsWorkspace\\set1\\7.txt" content64 = readAllLinesFromFile(fpath) content64OneLine = ''.join(content64) encryptedTextBytes = base64.b64decode(content64OneLine) decipher = AES.new(key, AES.MODE_ECB) print(decipher.decrypt(encryptedTextBytes))
def readAllFileToOneLine(fpath): content = readAllLinesFromFile(fpath) contentOneLine = ''.join(content) return contentOneLine