about summary refs log tree commit diff
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2022-01-04T20·44-0800
committerclbot <clbot@tvl.fyi>2022-01-08T05·51+0000
commitfad38387af3b6a5ed0d8f3042e2ac0a6ceabf45a (patch)
treeb68d2a4e633715a11db98028f82863ceed5ec452
parent7aaddb3d31dabff196eb9ef27666099c00dd5338 (diff)
feat(wpcarro/cryptopals): Support repeating key cipher r/3548
I think this is a form of symmetric encryption where the passphrase (e.g. "ICE")
is repeated (e.g. "ICEICEICEICEI...") until it matches the length of the
cleartext string; after that, the two byte-strings are XOR'd against one another
and then encoded as hexadecimal creating the ciphertext.

Change-Id: Ib148f06d6c42a41377d1df1f0738d77da935a9f6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/4789
Tested-by: BuildkiteCI
Reviewed-by: wpcarro <wpcarro@gmail.com>
Autosubmit: wpcarro <wpcarro@gmail.com>
-rw-r--r--users/wpcarro/scratch/cryptopals/set1/c5.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/users/wpcarro/scratch/cryptopals/set1/c5.py b/users/wpcarro/scratch/cryptopals/set1/c5.py
new file mode 100644
index 000000000000..a098dfe74afa
--- /dev/null
+++ b/users/wpcarro/scratch/cryptopals/set1/c5.py
@@ -0,0 +1,16 @@
+def encrypt_repeating_key(x, key):
+    result = b""
+    for i in range(len(x)):
+        b = ord(x[i]) ^ ord(key[i % len(key)])
+        result += b.to_bytes(1, 'big')
+    return result.hex()
+
+cleartext = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"
+expected = "0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f"
+
+run_tests = False
+if run_tests:
+    ciphertext = encrypt_repeating_key(cleartext, "ICE")
+    print(ciphertext)
+    assert ciphertext == expected
+    print("Success!")