for c1, c2 in combinations(MSGS, 2): xm = strxor(bytes.fromhex(c1), bytes.fromhex(c2)) print('{:2} {:2}'.format(MSGS.index(c1), MSGS.index(c2)), ''.join(magic(i) for i in xm))
然后就是繁琐的猜测明文过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# KEY = strxor(bytes.fromhex(MSGS[10]), b'The ') # KEY = strxor(bytes.fromhex(MSGS[5]), b'There are ') # KEY = strxor(bytes.fromhex(MSGS[3]), b'The ciphertext ') # KEY = strxor(bytes.fromhex(MSGS[10]), b'The secret message is ') # The end is not correct # KEY = strxor(bytes.fromhex(MSGS[5]), b'There are two types of ') # KEY = strxor(bytes.fromhex(MSGS[7]), b'We can see the point where ') # KEY = strxor(bytes.fromhex(MSGS[7]), b' The Concise OxfordDictionaries ') # Interesting; Step back # KEY = strxor(bytes.fromhex(MSGS[5]), b'There are two types of crypto') # KEY = strxor(bytes.fromhex(MSGS[6]), b'There are two types of cyptography') # KEY = strxor(bytes.fromhex(MSGS[0]), b'We can factor the number 15 with quantum') # KEY = strxor(bytes.fromhex(MSGS[3]), b'The ciphertext produced by a weak encryption ') # KEY = strxor(bytes.fromhex(MSGS[10]), b'The secret message is: When using a stream cipher') # KEY = strxor(bytes.fromhex(MSGS[3]), b'The ciphertext produced by a weak encryption algorithm ') # KEY = strxor(bytes.fromhex(MSGS[10]), b'The secret message is: When using a stream cipher, never use ') # KEY = strxor(bytes.fromhex(MSGS[6]), b'There are two types of cyptography: one that allows the Government ') # KEY = strxor(bytes.fromhex(MSGS[0]), b'We can factor the number 15 with quantum computers. We can also factor ') # KEY = strxor(bytes.fromhex(MSGS[10]), b'The secret message is: When using a stream cipher, never use the key more than ') # KEY = strxor(bytes.fromhex(MSGS[3]), b'The ciphertext produced by a weak encryption algorithm looks as good as ciphertext ')
最后得到最长的明文是There are two types of cyptography: one that allows the Government to use brute force to break the code, and one that requires the Government to use brute force to break you通过亦或得到密钥最后解出最终答案。
The secret message is: When using a stream cipher, never use the key more than once
defstrxor(a, b): iflen(a) > len(b): returnbytes([x ^ y for (x, y) inzip(a[:len(b)], b)]) else: returnbytes([x ^ y for (x, y) inzip(a, b[:len(a)])])
defmagic(c): if c == 0: # same ('A' xor 'A' = '\x00') return'*' ifchr(c) in string.ascii_letters: # space xor letter returnchr(c) return'_'
defmain():
for c1, c2 in combinations(MSGS, 2): xm = strxor(bytes.fromhex(c1), bytes.fromhex(c2)) print('{:2} {:2}'.format(MSGS.index(c1), MSGS.index(c2)), ''.join(magic(i) for i in xm))
print('\n开始猜明文~(很显然先猜大部分都是_的字母,然后疯狂填字)\n') KEY = strxor(bytes.fromhex(MSGS[6]), b'There are two types of cyptography: one that allows the Government to use brute force to break the code, and one that requires the Government to use brute force to break you') for x, m inenumerate(MSGS): print(x, strxor(bytes.fromhex(m), KEY).decode(encoding= 'utf-8',errors='ignore'))
print(KEY.hex()) if __name__ == '__main__': main()
2. PA1 option
Write a program that allows you to “crack” ciphertexts generated using a Vigenere-like cipher, where byte-wise XOR is used instead of addition modulo 26.
Cryptography is the practice and study of techniques for, among other things, secure communication in the presence of attackers. Cryptography has been used for hundreds, if not thousands, of years, but traditional cryptosystems were designed and evaluated in a fairly ad hoc manner. For example, the Vigenere encryption scheme was thought to be secure for decades after it was invented, but we now know, and this exercise demonstrates, that it can be broken very easily.
defxor(plaintext, key): repeated_key = (key * (len(plaintext) // len(key))) + key[:len(plaintext) % len(key)] encrypted_decrypted = bytes([p ^ k for p, k inzip(plaintext.encode(), repeated_key.encode())]) return encrypted_decrypted.hex()
plaintext = '''Burning 'em, if you ain't quick and nimble I go crazy when I hear a cymbal''' key = "ICE" encrypted1 = xor(plaintext, key) print("Encrypted:", encrypted1)
I'm back and I'm ringin' the bell A rockin' on the mike while the fly girls yell In ecstasy in the back of me Well that's my DJ Deshay cuttin' all them Z's Hittin' hard and the girlies goin' crazy Vanilla's on the mike, man I'm not lazy.
I'm lettin' my drug kick in It controls my mouth and I begin To just let it flow, let my concepts go My posse's to the side yellin', Go Vanilla Go!
Smooth 'cause that's the way I will be And if you don't give a damn, then Why you starin' at me So get off 'cause I control the stage There's no dissin' allowed I'm in my own phase The girlies sa y they love me and that is ok And I can dance better than any kid n' play
Stage 2 -- Yea the one ya' wanna listen to It's off my head so let the beat play through So I can funk it up and make it sound good 1-2-3 Yo -- Knock on some wood For good luck, I like my rhymes atrocious Supercalafragilisticexpialidocious I'm an effect and that you can bet I can take a fly girl and make her wet.
I'm like Samson -- Samson to Delilah There's no denyin', You can try to hang But you'll keep tryin' to get my style Over and over, practice makes perfect But not if you're a loafer.
You'll get nowhere, no place, no time, no girls Soon -- Oh my God, homebody, you probably eat Spaghetti with a spoon! Come on and say it!
VIP. Vanilla Ice yep, yep, I'm comin' hard like a rhino Intoxicating so you stagger like a wino So punks stop trying and girl stop cryin' Vanilla Ice is sellin' and you people are buyin' 'Cause why the freaks are jockin' like Crazy Glue Movin' and groovin' trying to sing along All through the ghetto groovin' this here song Now you're amazed by the VIP posse.
Steppin' so hard like a German Nazi Startled by the bases hittin' ground There's no trippin' on mine, I'm just gettin' down Sparkamatic, I'm hangin' tight like a fanatic You trapped me once and I thought that You might have it So step down and lend me your ear '89 in my time! You, '90 is my year.
You're weakenin' fast, YO! and I can tell it Your body's gettin' hot, so, so I can smell it So don't be mad and don't be sad 'Cause the lyrics belong to ICE, You can call me Dad You're pitchin' a fit, so step back and endure Let the witch doctor, Ice, do the dance to cure So come up close and don't be square You wanna battle me -- Anytime, anywhere
You thought that I was weak, Boy, you're dead wrong So come on, everybody and sing this song
Say -- Play that funky music Say, go white boy, go white boy go play that funky music Go white boy, go white boy, go Lay down and boogie and play that funky music till you die.
Play that funky music Come on, Come on, let me hear Play that funky music white boy you say it, say it Play that funky music A little louder now Play that funky music, white boy Come on, Come on, Come on Play that funky music