diff --git a/challenge.py b/challenge.py index 5f5cac1..7ffee9a 100644 --- a/challenge.py +++ b/challenge.py @@ -14,6 +14,7 @@ from public_key_cryptography import ( Euler_totient, private_key, decode, + hash_and_encrypt, ) if __name__ == "__main__": @@ -39,4 +40,5 @@ if __name__ == "__main__": print(Euler_totient(857504083339712752489993810777, 1029224947942998075080348647219)) print(private_key(857504083339712752489993810777, 1029224947942998075080348647219, 0x10001)) print(decode(857504083339712752489993810777, 1029224947942998075080348647219, 0x10001, 77578995801157823671636298847186723593814843845525223303932)) + print(hash_and_encrypt("crypto{Immut4ble_m3ssag1ng}")) # fmt: on diff --git a/public_key_cryptography.py b/public_key_cryptography.py index f53dc9c..2e1a2c0 100644 --- a/public_key_cryptography.py +++ b/public_key_cryptography.py @@ -1,3 +1,7 @@ +from hashlib import sha256 +from Crypto.Util.number import bytes_to_long + + # Challenge PK-crypto-1 def modular_exponentiation() -> int: return pow(101, 17, 22663) @@ -22,3 +26,12 @@ def private_key(p: int, q: int, exp: int) -> int: def decode(p: int, q: int, exp: int, c: int): pk = private_key(p, q, exp) return pow(c, pk, p * q) + + +# Challenge PK-crypto-6 +def hash_and_encrypt(m: str): + d = 11175901210643014262548222473449533091378848269490518850474399681690547281665059317155831692300453197335735728459259392366823302405685389586883670043744683993709123180805154631088513521456979317628012721881537154107239389466063136007337120599915456659758559300673444689263854921332185562706707573660658164991098457874495054854491474065039621922972671588299315846306069845169959451250821044417886630346229021305410340100401530146135418806544340908355106582089082980533651095594192031411679866134256418292249592135441145384466261279428795408721990564658703903787956958168449841491667690491585550160457893350536334242689 + N = 15216583654836731327639981224133918855895948374072384050848479908982286890731769486609085918857664046075375253168955058743185664390273058074450390236774324903305663479046566232967297765731625328029814055635316002591227570271271445226094919864475407884459980489638001092788574811554149774028950310695112688723853763743238753349782508121985338746755237819373178699343135091783992299561827389745132880022259873387524273298850340648779897909381979714026837172003953221052431217940632552930880000919436507245150726543040714721553361063311954285289857582079880295199632757829525723874753306371990452491305564061051059885803 + m_bytes = m.encode("UTF-8") + hash = bytes_to_long(sha256(m_bytes).digest()) + return pow(hash, d, N)