diff --git a/base64_cipher.py b/base64_cipher.py new file mode 100644 index 0000000..f95403c --- /dev/null +++ b/base64_cipher.py @@ -0,0 +1,88 @@ +def encode_base64(text): + r""" + >>> encode_base64('WELCOME to base64 encoding 😁') + 'V0VMQ09NRSB0byBiYXNlNjQgZW5jb2Rpbmcg8J+YgQ==' + >>> encode_base64('AĆ…įƒš€šŸ¤“') + 'QcOF4ZCD8JCAj/CfpJM=' + >>> encode_base64('A'*60) + 'QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB\r\nQUFB' + """ + base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" + + byte_text = bytes(text, "utf-8") # put text in bytes for unicode support + r = "" # the result + c = -len(byte_text) % 3 # the length of padding + p = "=" * c # the padding + s = byte_text + b"\x00" * c # the text to encode + + i = 0 + while i < len(s): + if i > 0 and ((i / 3 * 4) % 76) == 0: + r = r + "\r\n" # for unix newline, put "\n" + + n = (s[i] << 16) + (s[i + 1] << 8) + s[i + 2] + + n1 = (n >> 18) & 63 + n2 = (n >> 12) & 63 + n3 = (n >> 6) & 63 + n4 = n & 63 + + r += base64_chars[n1] + base64_chars[n2] + base64_chars[n3] + base64_chars[n4] + i += 3 + + return r[0 : len(r) - len(p)] + p + + +def decode_base64(text): + r""" + >>> decode_base64('V0VMQ09NRSB0byBiYXNlNjQgZW5jb2Rpbmcg8J+YgQ==') + 'WELCOME to base64 encoding 😁' + >>> decode_base64('QcOF4ZCD8JCAj/CfpJM=') + 'AĆ…įƒš€šŸ¤“' + >>> decode_base64("QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB\r\nQUFB") + 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + """ + base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" + s = "" + + for i in text: + if i in base64_chars: + s += i + c = "" + else: + if i == "=": + c += "=" + + p = "" + if c == "=": + p = "A" + else: + if c == "==": + p = "AA" + + r = b"" + s = s + p + + i = 0 + while i < len(s): + n = ( + (base64_chars.index(s[i]) << 18) + + (base64_chars.index(s[i + 1]) << 12) + + (base64_chars.index(s[i + 2]) << 6) + + base64_chars.index(s[i + 3]) + ) + + r += bytes([(n >> 16) & 255]) + bytes([(n >> 8) & 255]) + bytes([n & 255]) + + i += 4 + + return str(r[0 : len(r) - len(p)], "utf-8") + + +def main(): + print(encode_base64("WELCOME to base64 encoding 😁")) + print(decode_base64(encode_base64("WELCOME to base64 encoding 😁"))) + + +if __name__ == "__main__": + main() diff --git a/base85.py b/base85.py new file mode 100644 index 0000000..ebfd048 --- /dev/null +++ b/base85.py @@ -0,0 +1,13 @@ +import base64 + + +def main(): + inp = input("->") + encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object) + a85encoded = base64.a85encode(encoded) # a85encoded the encoded string + print(a85encoded) + print(base64.a85decode(a85encoded).decode("utf-8")) # decoded it + + +if __name__ == "__main__": + main() diff --git a/brute_force_caesar_cipher.py b/brute_force_caesar_cipher.py new file mode 100644 index 0000000..2586803 --- /dev/null +++ b/brute_force_caesar_cipher.py @@ -0,0 +1,56 @@ +def decrypt(message): + """ + >>> decrypt('TMDETUX PMDVU') + Decryption using Key #0: TMDETUX PMDVU + Decryption using Key #1: SLCDSTW OLCUT + Decryption using Key #2: RKBCRSV NKBTS + Decryption using Key #3: QJABQRU MJASR + Decryption using Key #4: PIZAPQT LIZRQ + Decryption using Key #5: OHYZOPS KHYQP + Decryption using Key #6: NGXYNOR JGXPO + Decryption using Key #7: MFWXMNQ IFWON + Decryption using Key #8: LEVWLMP HEVNM + Decryption using Key #9: KDUVKLO GDUML + Decryption using Key #10: JCTUJKN FCTLK + Decryption using Key #11: IBSTIJM EBSKJ + Decryption using Key #12: HARSHIL DARJI + Decryption using Key #13: GZQRGHK CZQIH + Decryption using Key #14: FYPQFGJ BYPHG + Decryption using Key #15: EXOPEFI AXOGF + Decryption using Key #16: DWNODEH ZWNFE + Decryption using Key #17: CVMNCDG YVMED + Decryption using Key #18: BULMBCF XULDC + Decryption using Key #19: ATKLABE WTKCB + Decryption using Key #20: ZSJKZAD VSJBA + Decryption using Key #21: YRIJYZC URIAZ + Decryption using Key #22: XQHIXYB TQHZY + Decryption using Key #23: WPGHWXA SPGYX + Decryption using Key #24: VOFGVWZ ROFXW + Decryption using Key #25: UNEFUVY QNEWV + """ + LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + for key in range(len(LETTERS)): + translated = "" + for symbol in message: + if symbol in LETTERS: + num = LETTERS.find(symbol) + num = num - key + if num < 0: + num = num + len(LETTERS) + translated = translated + LETTERS[num] + else: + translated = translated + symbol + print("Decryption using Key #%s: %s" % (key, translated)) + + +def main(): + message = input("Encrypted message: ") + message = message.upper() + decrypt(message) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + main()