本文整理汇总了Python中Crypto.PublicKey.ElGamal.generate方法的典型用法代码示例。如果您正苦于以下问题:Python ElGamal.generate方法的具体用法?Python ElGamal.generate怎么用?Python ElGamal.generate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto.PublicKey.ElGamal
的用法示例。
在下文中一共展示了ElGamal.generate方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _test_random_key
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import generate [as 别名]
def _test_random_key(self, bits):
elgObj = ElGamal.generate(bits, Random.new().read)
self._check_private_key(elgObj)
self._exercise_primitive(elgObj)
pub = elgObj.publickey()
self._check_public_key(pub)
self._exercise_public_primitive(elgObj)
示例2: get_key
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import generate [as 别名]
def get_key():
key = ElGamal.generate(256, get_random_bytes)
comps = ('p','g','y','x')
out = '\n'.join(["{} = {}".format(comp,getattr(key, comp)) for comp in comps])
with open('./key', 'w+') as k:
k.write(out)
key.set_key(user, out['y'],out['x'])
return key
示例3: generate_elgamal_keypair
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import generate [as 别名]
def generate_elgamal_keypair():
"""Return an ElGamal object with newly generated keypair"""
if 'SFLVAULT_IN_TEST' in os.environ:
print "WARNING: IN TEST MODE, EVERY KEYPAIR GENERATION IS BYPASSED AND USES A PRE-GENERATED AND WORLD-KNOWN KEYPAIR. REMOVE 'SFLVAULT_IN_TEST' FROM YOUR ENVIRONMENT IF YOU ARE DOING THIS ON PRODUCTION"
eg = ElGamal.ElGamalobj()
keys = [(177089723724552644256797243527295142469255734138493329314329932362154457094059269514887621456192343485606008571733849784882603220703971587460034382850082611103881050702039214183956206957248098956098183898169452181835193285526486693996807247957663965314452283162788463761928354944430848933147875443419511844733534867714246125293090881680286010834371853006350372947758409794906981881841508329191534306452090259107460058479336274992461969572007575859837L, 2368830913657867259423174096782984007672147302922056255072161233714845396747550413964785336340342087070536608406864241095864284199288769810784864221075742905057068477336098276284927890562488210509136821440679916802167852789973929164278286140181738520594891315446533462206307248550944558426698389577513200698569512147125339722576147002382255876258436727504192479647579172625910816774587488928783787624267035610900290120258307919121453927670441700811482181614216947L, 5861471316007038922650757021308043193803646029154275389954930654765928019938681282006482343772842302607960473277926921384673235972813815577111985557701858831111694263179407993690846841997398288866685890418702914928188654979371728552059661796422031090374692580710906447170464105162673344042938184790777466702148445760745296149876416417949678454708511011740073066144877868339403040477747772225977519821312965207L, 1169412825199936698700035513185825593893938895474876750007859746409857305379860678064015124546593449912724002752383066585681624318254362438491372548721947497497739043831382430104856590871057670575051579668363576657397472353061812950884556034822611307705562237354213497368218843244103113882159981178841442771150519161251285978446459307942619668439466357674240712609844734284943761543870187004331653216116937988266963743961096619840352159665738163357566198583064435L),
(363126185715790250119395282425017818083421673278440118808474954552806007436370887232958142538070938460903011757636551318850215594111019699633958587914824512339681573953775134121488999147928038505883131989323638021781157246124428000084118138446325126739005521403114471077697023469488488105229388102971903306007555362613775010306064798678761753948810755236011346132218974049446116094394433461746597812371697367173395113014824646850943586174124632464143L, 1989666736598081965365973787349938627625613245335951894925228395719349924579514682166704542464221001327015131231101009506582078440087637470784673000661958376397578391397303146171320274531265903747455382524598808613766406694744319576824028880703970997080651320662468590292703565426391011134523391035995750230341849776175803186815053305823053143914398318121693692044542134832809759905437953710838534372887584358442203447387293183908262967797038874535690090799742911L, 133850088107174975861015682594827971956767368440585898108600141692889215241539178575381178799995195531301157505453120993980045956642227472649664668888717884598815932243844750408878011387532720932159839454554017574665882963054750224693505390054364096154711586190837517112644639757613967217614109546151313073865262488626822109764294618345504453742784825659007630866924661811701179640013729327586347L, 742665583685283032188129474839034185107068199926583417281240975739235100098517297493350864258177674271267050862217567671938790648634008735784684115797768392310253433978502694449565453913758801583487678024491118014887051643096970952295790434950566748516670079663712282848262006606082748685002561868381598918739708181310245226480020229450553192469536632519293406262550081671717685585065331112633947328611250435010734072352883491446355872734313855711051794348490960L)]
eg.g, eg.p, eg.x, eg.y = keys[random.randint(0, 1)]
return eg
# Otherwise, generate, really :)
return ElGamal.generate(1536, randfunc)
示例4: generate_elgamal
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import generate [as 别名]
def generate_elgamal(klength, privatekey):
key = ElGamal.generate(klength, Random.new().read)
h = SHA.new(privatekey).digest()
while 1:
k = random.StrongRandom().randint(1,key.p-1)
if GCD(k, key.p-1) == 1: break
sig = key.sign(h,k)
if key.verify(h,sig):
print "OK"
return key, h , sig
else:
print "Incorrect signature"
return None
示例5: make_key_objects
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import generate [as 别名]
def make_key_objects(pub_algorithm_type, key_size):
if pub_algorithm_type == 17:
secret_key = DSA.generate(key_size)
elif pub_algorithm_type in (1, 3):
secret_key = RSA.generate(key_size)
elif pub_algorithm_type == 20:
# TODO: This should not be allowed except for testing purposes.
# XXX: This can take a really long time
secret_key = ElGamal.generate(key_size, Random.new().read)
else:
# TODO: complete
raise ValueError
public_key = secret_key.publickey()
return secret_key, public_key
示例6: subscribe
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import generate [as 别名]
def subscribe(self):
print "Subscribing please wait..."
self.rsa = RSA_gen(4096)
self.n, self.e, self.d = RSA_keys(self.rsa)
self.ElGkey = ElGamal.generate(256, Random.new().read)
self.rsa_sign = RSA_gen(1024)
self.n_sign, self.e_sign, self.d_sign = RSA_keys(self.rsa_sign)
self.ust = UST(self.server_pk_n, self.server_pk_e)
self.ust_lock.acquire()
self.ust.prepare()
args = {"blinded_nonce" : self.ust.blinded_nonce,
"client_username" : self.username,
"client_pk_n" : self.n,
"client_pk_e" : self.e,
"client_sign_pk_n" : self.n_sign,
"client_sign_pk_e" : self.e_sign}
r = send_request(SUBSCRIBE, args)
if r == ERROR:
print "ERROR: could not subscribe"
sys.exit(0)
self.ust.receive(r['blinded_sign'])
self.ust_lock.release()
user = r['user']
if user['client_pk_n'] == self.n and user['client_pk_e'] == self.e \
and user['client_sign_pk_n'] == self.n_sign \
and user['client_sign_pk_e'] == self.e_sign:
pass
else:
print "Username is taken, please try again"
sys.exit(0)
self.user_id = user['client_user_id']
self.user_table_ptr = 0
self.client_new_conversations_table_ptr = 0
return
示例7: generate_elgamal_keypair
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import generate [as 别名]
def generate_elgamal_keypair():
"""Return an ElGamal object with newly generated keypair"""
return ElGamal.generate(1536, randfunc)
示例8: open
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import generate [as 别名]
from Crypto.PublicKey import ElGamal
from Crypto import Random
import cPickle as Pickle
v_priv_key = ElGamal.generate(128, Random.new().read)
v_pub_key = v_priv_key.publickey()
s_priv_key = ElGamal.generate(128, Random.new().read)
s_pub_key = s_priv_key.publickey()
Pickle.dump(v_priv_key, open("vending_key.priv", 'w'))
Pickle.dump(v_pub_key, open("vending_key.pub", 'w'))
Pickle.dump(s_priv_key, open("server_key.priv", 'w'))
Pickle.dump(s_pub_key, open("server_key.pub", 'w'))
示例9: genk
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import generate [as 别名]
def genk(self):
self.k = ElGamal.generate(self.bits, Random.new().read)
return self.k
示例10: decode_message
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import generate [as 别名]
def decode_message(m):
return number.long_to_bytes(m)
def encrypt(m):
pt = encode_message(m)
return pubkey.encrypt(pt, K)
def recvline(req):
buf = b""
while not buf.endswith(b"\n"):
buf += req.recv(1)
return buf
print("Now generating keys...")
privkey = ElGamal.generate(384, urandom)
pubkey = privkey.publickey()
(p, g, y) = (pubkey.p, pubkey.g, pubkey.y)
K = number.getPrime(128, urandom)
while number.GCD(K, p-1) != 1:
print("[-] K not relatively prime with {}".format(p-1))
K = number.getPrime(128, urandom)
print("Encrypting FLAG...")
c1, c2 = encrypt(FLAG)
print("Done!")
示例11: __init__
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import generate [as 别名]
def __init__(self):
super(ELGamal, self).__init__()
self.key = ElGamal.generate(1024, Random.new().read)
self.k = random.StrongRandom().randint(1, self.key.p - 1)
示例12: gen_key
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import generate [as 别名]
def gen_key(self, key_len=4096):
self.key = ElGamal.generate(key_len, Random.new().read)