| #!/usr/bin/env python3 |
| |
| """ |
| Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF licenses this file |
| to you under the Apache License, Version 2.0 (the |
| "License"); you may not use this file except in compliance |
| with the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, |
| software distributed under the License is distributed on an |
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| KIND, either express or implied. See the License for the |
| specific language governing permissions and limitations |
| under the License. |
| """ |
| |
| import os |
| import sys |
| |
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
| |
| from amcl import core_utils, rsa_WWW |
| |
| seed_hex = "78d0fb6705ce77dee47d03eb5b9c5d30" |
| |
| e = 0x10001 |
| |
| if __name__ == "__main__": |
| seed = bytes.fromhex(seed_hex) |
| rng = core_utils.create_csprng(seed) |
| |
| m = b'test message' |
| |
| print('Generate key pair') |
| public_key, private_key = rsa_WWW.generate_key_pair(rng, e) |
| |
| print(f"\nEncode message '{m.decode('utf-8')}'") |
| pt, rc = rsa_WWW.oaep_encode(rng, rsa_WWW.SHA256, m) |
| assert rc == 0, 'Failure OAEP padding message' |
| |
| print(f"\nEncrypt plaintext '{pt.hex()}'") |
| ct = rsa_WWW.encrypt(public_key, pt) |
| |
| print(f"\nDecrypt cyphertext {ct.hex()}") |
| dec_pt = rsa_WWW.decrypt(private_key, ct) |
| |
| print(f"\nDecode plaintext '{dec_pt.hex()}'") |
| dec_m, rc = rsa_WWW.oaep_decode(rsa_WWW.SHA256, dec_pt) |
| assert rc == 0, 'Failure OAEP unpadding message' |
| |
| print(f"Recovered message '{dec_m.decode('utf-8')}'") |