def _generate_keys(self): """Generates RSA 2048 public and private key and returns them.""" RSAfactory = RSAImplementation() self.logger.debug(u'Generating new RSA keypair...') keypair = RSAfactory.generate(2048) pub_key = keypair.publickey().exportKey() pvt_key = keypair.exportKey() self.logger.debug(u'RSA keypair generated.') self.logger.debug(u'Public key generated:\n %s' % pub_key) return pub_key, pvt_key
def rsa_encrypt(text, n, e): impl = RSAImplementation() n = int(n, 16) if isinstance(n, str) else n e = int(e, 16) if isinstance(e, str) else e public_key = impl.construct((n, e)) random_number = 2 # 随便填 result = public_key.encrypt(encrypt_string(public_key, text), random_number)[0] return dec2hex(result)
beta = self.Hash(pi).digest() return beta def verify(self, PK, alpha, pi): s = OS2IP(pi) m = RSAVP1(PK, s) EM = I2OSP(m, PK.k - 1) EM_ = MGF1(alpha, PK.k - 1, self.Hlen, self.Hash) if OS2IP(EM) == OS2IP(EM_): return True else: return False if __name__ == "__main__": if len(sys.argv) < 2: print "USAGE: python vrf-impl.py [plaintext]" exit(1) kf = RSAImplementation() keyPair = kf.generate(1024) pubkey = PublicKeyPrim(keyPair.e, keyPair.n) privkey = PrivateKeyPrim(keyPair.d, keyPair.n) alpha = ''.join(sys.argv[1:]) VRF = RSAFDHVRF() pi = VRF.prove(privkey, alpha) beta = VRF.proof2hash(pi) if VRF.verify(pubkey, alpha, pi): print("It works") else: print("Something went wrong")
def rsa_pub_key_to_pem(n, e): rsa = RSAImplementation() rsa_key = rsa.construct((n, e)) pem = rsa_key.exportKey() return pem
cfg = Configuration() cfg.endpoint_process = Endpoint.url('http://site2.enigmabridge.com:11180') cfg.endpoint_enroll = Endpoint.url('http://site2.enigmabridge.com:11182') cfg.api_key = 'API_TEST' cfg.retry = SimpleRetry(max_retry=1, jitter_base=1000, jitter_rand=250) def create_rsa(cfg): cou = CreateUO(configuration=cfg, tpl={TemplateFields.environment: Environment.DEV}) rsa_key = cou.create_rsa(2048) return rsa_key rsa = RSAImplementation() base_path = '/tmp' for idx in range(84, 100): print "Generating key %02d" % idx key = create_rsa(cfg) rsa_key = rsa.construct((key.n, key.e)) pem = rsa_key.exportKey() file_name = os.path.join(base_path, 'pubkey_%02d.pem' % idx) with open(file_name, 'w') as hnd: hnd.write(pem) pass if __name__ == "__main__": print "ok"
@staticmethod def import_key(data): try: return RSA_.importKey(data).exportKey() except (ValueError, IndexError): pass if len(data) < BLOBHEADER.sizeof(): return buf = io.BytesIO(data) header = BLOBHEADER.parse(buf.read(BLOBHEADER.sizeof())) if header.bType not in BlobTypes: return if header.aiKeyAlg not in RSA.algorithms: return obj = BlobTypes[header.bType]() obj.parse(buf) return obj.export_key() @staticmethod def export_key(n, e, d=None, p=None, q=None, crt=None): wrap = lambda x: None if x is None else long(x) tup = wrap(n), wrap(e), wrap(d), wrap(p), wrap(q), wrap(crt) return RSA_.construct(tup).exportKey() RSA_ = RSAImplementation(use_fast_math=False)
os.system("clear") print "**********************************************" print "**************** CryptoComms *****************" print "******************* v0.7 *********************" print "**********************************************" print "**** Encrypted P2P communcation by DaKnOb ****" print "**********************************************" from Crypto.PublicKey.RSA import RSAImplementation as RSA import random expo = [3,5,7,9,13,15,17,257,65537] # A List of Public Exponents to use for your key KEYSIZE = 2048 # Your key size f = RSA() myKey = f.generate(KEYSIZE, e=expo[random.randint(0,len(expo)-1)]) from SimpleXMLRPCServer import SimpleXMLRPCServer # We are using an HTTP server to accept messages from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler # We are using an HTTP browser to send messages # Restrict to a particular path. class RequestHandler(SimpleXMLRPCRequestHandler): rpc_paths = ('/RPC2',) # Create server ip = raw_input("IP Address: ") # Get the remote party IP Address hostport = int(input("Host Port: ")) # Get the local port to listen to
from Crypto.PublicKey.RSA import RSAImplementation def get_backend(backend=None): return default_backend() if backend is None else backend def load_pem_private_key(data, password=None, backend=None): return serialization.load_pem_private_key(data, None, get_backend(backend)) def load_pem_private_key_pycrypto(data, password=None): return RSA.importKey(data, passphrase=password) rsa = RSAImplementation() base_path = '/Users/dusanklinec/Downloads/EC2_keys' for i in range(0, 200): file_name = os.path.join(base_path, 'test%d.pem' % i) if not os.path.exists(file_name): continue with open(file_name, 'r') as hnd: key = hnd.read() t = load_pem_private_key_pycrypto(key) rsa_key = rsa.construct((t.n, t.e)) pem = rsa_key.exportKey() file_name = os.path.join(base_path, 'pubkey_test_%02d.pem' % i)