Contract con_rsa_encryption


Contract Code


  
1 random.seed()
2
3
4 @construct
5 def seed():
6 pass
7
8
9 def assert_int(var: int, name: str) -> None:
10 assert isinstance(var, int), "%s should be an integer, not %s" % (name, type(var))
11
12
13 def encrypt_int(message: int, ekey: int, n: int) -> int:
14 """Encrypts a message using encryption key 'ekey', working modulo n"""
15
16 assert_int(message, "message")
17 assert_int(ekey, "ekey")
18 assert_int(n, "n")
19
20 assert message >= 0, "Only non-negative numbers are supported"
21
22 assert message <= n, "The message %i is too long for n=%i" % (message, n)
23
24 return pow(message, ekey, n)
25
26
27 def bytes2int(raw_bytes: bytes) -> int:
28 r"""Converts a list of bytes or an 8-bit string to an integer.
29 When using unicode strings, encode it to some encoding like UTF8 first.
30 >>> (((128 * 256) + 64) * 256) + 15
31 8405007
32 >>> bytes2int(b'\x80@\x0f')
33 8405007
34 """
35 return int.from_bytes(raw_bytes, "big", signed=False)
36
37
38 def div_ceil(a: int, b: int) -> int:
39 return a//b + bool(a%b)
40
41
42 def int2bytes(number: int, fill_size: int = 0) -> bytes:
43 """
44 Convert an unsigned integer to bytes (big-endian)::
45 Does not preserve leading zeros if you don't specify a fill size.
46 :param number:
47 Integer value
48 :param fill_size:
49 If the optional fill size is given the length of the resulting
50 byte string is expected to be the fill size and will be padded
51 with prefix zero bytes to satisfy that length.
52 :returns:
53 Raw bytes (base-256 representation).
54 :raises:
55 ``OverflowError`` when fill_size is given and the number takes up more
56 bytes than fit into the block. This requires the ``overflow``
57 argument to this function to be set to ``False`` otherwise, no
58 error will be raised.
59 """
60
61 assert number >= 0, "Number must be an unsigned integer: %d" % number
62
63 bytes_required = max(1, div_ceil(number.bit_length(), 8))
64
65 if fill_size > 0:
66 return number.to_bytes(fill_size, "big")
67
68 return number.to_bytes(bytes_required, "big")
69
70
71 def bit_size(num: int) -> int:
72 """
73 Number of bits needed to represent a integer excluding any prefix
74 0 bits.
75 Usage::
76 >>> bit_size(1023)
77 10
78 >>> bit_size(1024)
79 11
80 >>> bit_size(1025)
81 11
82 :param num:
83 Integer value. If num is 0, returns 0. Only the absolute value of the
84 number is considered. Therefore, signed integers will be abs(num)
85 before the number's bit length is determined.
86 :returns:
87 Returns the number of bits in the integer.
88 """
89
90 assert isinstance(num, int), "bit_size(num) only supports integers, not %r" % type(num)
91 return num.bit_length()
92
93
94 def byte_size(number: int) -> int:
95 """
96 Returns the number of bytes required to hold a specific long number.
97 The number of bytes is rounded up.
98 Usage::
99 >>> byte_size(1 << 1023)
100 128
101 >>> byte_size((1 << 1024) - 1)
102 128
103 >>> byte_size(1 << 1024)
104 129
105 :param number:
106 An unsigned integer
107 :returns:
108 The number of bytes required to hold a specific long number.
109 """
110 if number == 0:
111 return 1
112 return div_ceil(bit_size(number), 8)
113
114
115 def pad_for_encryption(message: bytes, target_length: int) -> bytes:
116 r"""Pads the message for encryption, returning the padded message.
117 :return: 00 02 RANDOM_DATA 00 MESSAGE
118 >>> block = pad_for_encryption(b'hello', 16)
119 >>> len(block)
120 16
121 >>> block[0:2]
122 b'\x00\x02'
123 >>> block[-6:]
124 b'\x00hello'
125 """
126
127 max_msglength = target_length - 11
128 msglength = len(message)
129
130 assert msglength <= max_msglength, "%i bytes needed for message, but there is only space for %i" % (msglength, max_msglength)
131
132 # Get random padding
133 padding = b""
134 padding_length = target_length - msglength - 3
135
136 # We remove 0-bytes, so we'll end up with less padding than we've asked for,
137 # so keep adding data until we're at the correct length.
138 while len(padding) < padding_length:
139 needed_bytes = padding_length - len(padding)
140
141 # Always read at least 8 bytes more than we need, and trim off the rest
142 # after removing the 0-bytes. This increases the chance of getting
143 # enough bytes, especially when needed_bytes is small
144 random_bits = random.getrandbits((needed_bytes + 5) * 8)
145 new_padding = int2bytes(random_bits, fill_size=needed_bytes+5)
146 new_padding = new_padding.replace(b"\x00", b"")
147 padding = padding + new_padding[:needed_bytes]
148
149 assert len(padding) == padding_length, "Invalid padding length: %i != %i" % (len(padding), padding_length)
150
151 return b"".join([b"\x00\x02", padding, b"\x00", message])
152
153
154 @export
155 def encrypt(message_str: str, n: int, e: int) -> str:
156 """Encrypts the given message using PKCS#1 v1.5
157 :param message: the message to encrypt. Must be a byte string no longer than
158 ``k-11`` bytes, where ``k`` is the number of bytes needed to encode
159 the ``n`` component of the public key.
160 :param pub_key: the :py:class:`rsa.PublicKey` to encrypt with.
161 :raise OverflowError: when the message is too large to fit in the padded
162 block.
163 >>> from rsa import key, common
164 >>> (pub_key, priv_key) = key.newkeys(256)
165 >>> message = b'hello'
166 >>> crypto = encrypt(message, pub_key)
167 The crypto text should be just as long as the public key 'n' component:
168 >>> len(crypto) == common.byte_size(pub_key.n)
169 True
170 """
171
172 message = message_str.encode()
173
174 keylength = byte_size(n)
175 padded = pad_for_encryption(message, keylength)
176
177 payload = bytes2int(padded)
178 encrypted = encrypt_int(payload, e, n)
179 block = int2bytes(encrypted, keylength)
180
181 return block.hex()
182

Byte Code

e3000000000000000000000000060000004000000073be00000065006a01830001006400640184005a0265036504640264039c036404640584045a05650365036503650364069c046407640884045a066507650364099c02640a640b84045a08650365036503640c9c03640d640e84045a09642065036503650764109c036411641284055a0a6503650364139c026414641584045a0b6503650364169c026417641884045a0c65076503650764199c03641a641b84045a0d650e641c83016504650365036504641d9c04641e641f840483015a0f64025300292163000000000000000000000000010000004300000073040000006400530029014ea900720100000072010000007201000000da00da045f5f5f5f040000007302000000000172030000004e2903da03766172da046e616d65da0672657475726e630200000000000000020000000500000043000000732200000074007c0074018302731e740264017c0174037c00830166021600830182016400530029024e7a1f25732073686f756c6420626520616e20696e74656765722c206e6f742025732904da0a6973696e7374616e6365da03696e74da0e417373657274696f6e4572726f72da0474797065290272040000007205000000720100000072010000007202000000da0c5f5f6173736572745f696e7408000000730400000000011001720b0000002904da076d657373616765da04656b6579da016e7206000000630300000000000000030000000400000043000000735200000074007c0064018302010074007c0164028302010074007c026403830201007c0064046b05732e74016405830182017c007c026b017346740164067c007c02660216008301820174027c007c017c028303530029077a40456e6372797074732061206d657373616765207573696e6720656e6372797074696f6e206b65792027656b6579272c20776f726b696e67206d6f64756c6f206e720c000000720d000000720e000000e9000000007a274f6e6c79206e6f6e2d6e65676174697665206e756d626572732061726520737570706f727465647a23546865206d65737361676520256920697320746f6f206c6f6e6720666f72206e3d25692903720b0000007209000000da03706f772903720c000000720d000000720e000000720100000072010000007202000000da0d5f5f656e63727970745f696e740d000000730c00000000020a010a010a011001180172110000002902da097261775f62797465737206000000630100000000000000010000000500000043000000731000000074006a017c006401640264038d03530029047aeb436f6e76657274732061206c697374206f66206279746573206f7220616e20382d62697420737472696e6720746f20616e20696e74656765722e0a202020205768656e207573696e6720756e69636f646520737472696e67732c20656e636f646520697420746f20736f6d6520656e636f64696e67206c696b6520555446382066697273742e0a202020203e3e3e20282828313238202a2032353629202b20363429202a2032353629202b2031350a20202020383430353030370a202020203e3e3e20627974657332696e742862275c783830405c78306627290a20202020383430353030370a20202020da03626967462901da067369676e656429027208000000da0a66726f6d5f627974657329017212000000720100000072010000007202000000da0b5f5f627974657332696e74170000007302000000000872160000002903da0161da0162720600000063020000000000000002000000040000004300000073140000007c007c011a0074007c007c01160083011700530029014e2901da04626f6f6c290272170000007218000000720100000072010000007202000000da0a5f5f6469765f6365696c2200000073020000000001721a000000720f0000002903da066e756d626572da0966696c6c5f73697a65720600000063020000000000000003000000050000004300000073480000007c0064016b057314740064027c001600830182017401640374027c006a0383006404830283027d027c0164016b04723c7c006a047c016405830253007c006a047c02640583025300290661c90200000a20202020436f6e7665727420616e20756e7369676e656420696e746567657220746f20627974657320286269672d656e6469616e293a3a0a20202020446f6573206e6f74207072657365727665206c656164696e67207a65726f7320696620796f7520646f6e2774207370656369667920612066696c6c2073697a652e0a202020203a706172616d206e756d6265723a0a2020202020202020496e74656765722076616c75650a202020203a706172616d2066696c6c5f73697a653a0a2020202020202020496620746865206f7074696f6e616c2066696c6c2073697a6520697320676976656e20746865206c656e677468206f662074686520726573756c74696e670a20202020202020206279746520737472696e6720697320657870656374656420746f206265207468652066696c6c2073697a6520616e642077696c6c206265207061646465640a20202020202020207769746820707265666978207a65726f20627974657320746f20736174697366792074686174206c656e6774682e0a202020203a72657475726e733a0a20202020202020205261772062797465732028626173652d32353620726570726573656e746174696f6e292e0a202020203a7261697365733a0a202020202020202060604f766572666c6f774572726f726060207768656e2066696c6c5f73697a6520697320676976656e20616e6420746865206e756d6265722074616b6573207570206d6f72650a20202020202020206279746573207468616e2066697420696e746f2074686520626c6f636b2e2054686973207265717569726573207468652060606f766572666c6f7760600a2020202020202020617267756d656e7420746f20746869732066756e6374696f6e20746f2062652073657420746f20606046616c73656060206f74686572776973652c206e6f0a20202020202020206572726f722077696c6c206265207261697365642e0a20202020720f0000007a264e756d626572206d75737420626520616e20756e7369676e656420696e74656765723a202564e901000000e908000000721300000029057209000000da036d6178721a000000da0a6269745f6c656e677468da08746f5f62797465732903721b000000721c000000da0e62797465735f7265717569726564720100000072010000007202000000da0b5f5f696e7432627974657326000000730a00000000121401140108010c0172230000002902da036e756d7206000000630100000000000000010000000400000043000000732200000074007c0074018302731a7402640174037c0083011600830182017c006a0483005300290261f40100000a202020204e756d626572206f662062697473206e656564656420746f20726570726573656e74206120696e7465676572206578636c7564696e6720616e79207072656669780a202020203020626974732e0a2020202055736167653a3a0a20202020202020203e3e3e206269745f73697a652831303233290a202020202020202031300a20202020202020203e3e3e206269745f73697a652831303234290a202020202020202031310a20202020202020203e3e3e206269745f73697a652831303235290a202020202020202031310a202020203a706172616d206e756d3a0a2020202020202020496e74656765722076616c75652e204966206e756d20697320302c2072657475726e7320302e204f6e6c7920746865206162736f6c7574652076616c7565206f66207468650a20202020202020206e756d62657220697320636f6e736964657265642e205468657265666f72652c207369676e656420696e7465676572732077696c6c20626520616273286e756d290a20202020202020206265666f726520746865206e756d626572277320626974206c656e6774682069732064657465726d696e65642e0a202020203a72657475726e733a0a202020202020202052657475726e7320746865206e756d626572206f66206269747320696e2074686520696e74656765722e0a202020207a2c6269745f73697a65286e756d29206f6e6c7920737570706f72747320696e7465676572732c206e6f742025722905720700000072080000007209000000720a000000722000000029017224000000720100000072010000007202000000da0a5f5f6269745f73697a653f000000730600000000120c010e0172250000002902721b0000007206000000630100000000000000010000000300000043000000731a0000007c0064016b02720c64025300740074017c008301640383025300290461900100000a2020202052657475726e7320746865206e756d626572206f6620627974657320726571756972656420746f20686f6c642061207370656369666963206c6f6e67206e756d6265722e0a20202020546865206e756d626572206f6620627974657320697320726f756e6465642075702e0a2020202055736167653a3a0a20202020202020203e3e3e20627974655f73697a652831203c3c2031303233290a20202020202020203132380a20202020202020203e3e3e20627974655f73697a65282831203c3c203130323429202d2031290a20202020202020203132380a20202020202020203e3e3e20627974655f73697a652831203c3c2031303234290a20202020202020203132390a202020203a706172616d206e756d6265723a0a2020202020202020416e20756e7369676e656420696e74656765720a202020203a72657475726e733a0a2020202020202020546865206e756d626572206f6620627974657320726571756972656420746f20686f6c642061207370656369666963206c6f6e67206e756d6265722e0a20202020720f000000721d000000721e0000002902721a00000072250000002901721b000000720100000072010000007202000000da0b5f5f627974655f73697a6556000000730600000000100801040172260000002903720c000000da0d7461726765745f6c656e677468720600000063020000000000000009000000050000004300000073c60000007c01640118007d0274007c0083017d037c037c026b017328740164027c037c02660216008301820164037d047c017c031800640418007d05785a74007c0483017c056b0072927c0574007c04830118007d0674026a037c06640517006406140083017d0774047c077c066405170064078d027d087c086a056408640383027d087c047c0864097c068502190017007d04713a570074007c0483017c056b0273b47401640a74007c0483017c05660216008301820164036a06640b7c0464087c00670483015300290c7aff5061647320746865206d65737361676520666f7220656e6372797074696f6e2c2072657475726e696e672074686520706164646564206d6573736167652e0a202020203a72657475726e3a2030302030322052414e444f4d5f44415441203030204d4553534147450a202020203e3e3e20626c6f636b203d207061645f666f725f656e6372797074696f6e28622768656c6c6f272c203136290a202020203e3e3e206c656e28626c6f636b290a2020202031360a202020203e3e3e20626c6f636b5b303a325d0a2020202062275c7830305c783032270a202020203e3e3e20626c6f636b5b2d363a5d0a2020202062275c78303068656c6c6f270a20202020e90b0000007a3b2569206279746573206e656564656420666f72206d6573736167652c20627574207468657265206973206f6e6c7920737061636520666f72202569f300000000e903000000e905000000721e0000002901721c000000f301000000004e7a20496e76616c69642070616464696e67206c656e6774683a20256920213d202569730200000000022907da036c656e7209000000da0672616e646f6dda0b67657472616e64626974737223000000da077265706c616365da046a6f696e2909720c0000007227000000da0d6d61785f6d73676c656e677468da096d73676c656e677468da0770616464696e67da0e70616464696e675f6c656e677468da0c6e65656465645f6279746573da0b72616e646f6d5f62697473da0b6e65775f70616464696e67720100000072010000007202000000da145f5f7061645f666f725f656e6372797074696f6e6b0000007320000000000b080108010c010c0104010c010e010c01120110010c01140106010c010e017239000000da12636f6e5f7273615f656e6372797074696f6e2904da0b6d6573736167655f737472720e000000da0165720600000063030000000000000009000000040000004300000073400000007c006a0083007d0374017c0183017d0474027c037c0483027d0574037c0583017d0674047c067c027c0183037d0774057c077c0483027d087c086a0683005300290161bd020000456e6372797074732074686520676976656e206d657373616765207573696e6720504b435323312076312e350a202020203a706172616d206d6573736167653a20746865206d65737361676520746f20656e63727970742e204d7573742062652061206279746520737472696e67206e6f206c6f6e676572207468616e0a202020202020202060606b2d313160602062797465732c2077686572652060606b606020697320746865206e756d626572206f66206279746573206e656564656420746f20656e636f64650a20202020202020207468652060606e606020636f6d706f6e656e74206f6620746865207075626c6963206b65792e0a202020203a706172616d207075625f6b65793a20746865203a70793a636c6173733a607273612e5075626c69634b65796020746f20656e637279707420776974682e0a202020203a7261697365204f766572666c6f774572726f723a207768656e20746865206d65737361676520697320746f6f206c6172676520746f2066697420696e20746865207061646465640a2020202020202020626c6f636b2e0a202020203e3e3e2066726f6d2072736120696d706f7274206b65792c20636f6d6d6f6e0a202020203e3e3e20287075625f6b65792c20707269765f6b657929203d206b65792e6e65776b65797328323536290a202020203e3e3e206d657373616765203d20622768656c6c6f270a202020203e3e3e2063727970746f203d20656e6372797074286d6573736167652c207075625f6b6579290a202020205468652063727970746f20746578742073686f756c64206265206a757374206173206c6f6e6720617320746865207075626c6963206b657920276e2720636f6d706f6e656e743a0a202020203e3e3e206c656e2863727970746f29203d3d20636f6d6d6f6e2e627974655f73697a65287075625f6b65792e6e290a20202020547275650a202020202907da06656e636f646572260000007239000000721600000072110000007223000000da036865782909723b000000720e000000723c000000720c000000da096b65796c656e677468da06706164646564da077061796c6f6164da09656e63727970746564da05626c6f636b720100000072010000007202000000da07656e637279707488000000730e0000000011080108010a0108010c010a0172440000002901720f0000002910722e000000da047365656472030000007208000000da03737472720b0000007211000000da0562797465737216000000721a0000007223000000722500000072260000007239000000da085f5f6578706f727472440000007201000000720100000072010000007202000000da083c6d6f64756c653e010000007316000000080308041205140a100b1204141910171015121d0601