日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python写接口自动化需要rsa加密_RSA加密,请问如何用Python实现该加密过程

發布時間:2025/3/19 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python写接口自动化需要rsa加密_RSA加密,请问如何用Python实现该加密过程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

展開全部

^import?random

def?gcd(a,?b):

while?b?!=?0:

a,?b?=?b,?a?%?b

return?a

def?multiplicative_inverse(e,?phi):

d?=?0

x1?=?0

x2?=?1

y1?=?1

temp_phi?=?phi

while?e?>?0:

temp1?=?temp_phi/e

temp2?=?temp_phi?-?temp1?*?e

temp_phi?=?e

e?=?temp2

x?=?x2-?temp1*?x1

y?=?d?-?temp1?*?y1

x2?=?x1

x1?=?x

d?=?y1

y1?=?y

if?temp_phi?==?1:

return?d?+?phi

'''

Tests?to?see?if?a?number?is?prime.

'''

def?is_prime(num):

if?num?==?2:

return?True

if?num?

return?False

for?n?in?xrange(3,?int(num**0.5)+2,?2):

if?num?%?n?==?0:

return?False

return?True

def?generate_keypair(p,?q):

if?not?(is_prime(p)?and?is_prime(q)):

raise?ValueError('Both?numbers?must?be?prime.')

elif?p?==?q:

raise?ValueError('p?and?q?cannot?be?equal')

#n?=?pq

n?=?p?*?q

#Phi?is?the?totient?of?n

phi?=?(p-1)?*?(q-1)

#Choose?an?integer?e?such?that?e?and?phi(n)?are?coprime

e?=?random.randrange(1,?phi)

#Use?Euclid's?Algorithm?to?verify?that?e?and?phi(n)?are?comprime

g?=?gcd(e,?phi)

while?g?!=?1:

e?=?random.randrange(1,?phi)

g?=?gcd(e,?phi)

#Use?Extended?Euclid's?Algorithm?to?generate?the?private?key

d?=?multiplicative_inverse(e,?phi)

#Return?public?and?private?keypair

#Public?key?is?(e,?n)?and?private?key?is?(d,?n)

return?((e,?n),?(d,?n))

def?encrypt(pk,?plaintext):

#Unpack?the?key?into?it's?components

key,?n?=?pk

#Convert?each?letter?in?the?plaintext?to?numbers?based?on?the?character?using?a^32313133353236313431303231363533e78988e69d8331333363353866b?mod?m

cipher?=?[(ord(char)?**?key)?%?n?for?char?in?plaintext]

#Return?the?array?of?bytes

return?cipher

def?decrypt(pk,?ciphertext):

#Unpack?the?key?into?its?components

key,?n?=?pk

#Generate?the?plaintext?based?on?the?ciphertext?and?key?using?a^b?mod?m

plain?=?[chr((char?**?key)?%?n)?for?char?in?ciphertext]

#Return?the?array?of?bytes?as?a?string

return?''.join(plain)

if?__name__?==?'__main__':

print?"RSA?Encrypter/?Decrypter"

p?=?int(raw_input("Enter?a?prime?number?(17,?19,?23,?etc):?"))

q?=?int(raw_input("Enter?another?prime?number?(Not?one?you?entered?above):?"))

print?"Generating?your?public/private?keypairs?now?.?.?."

public,?private?=?generate_keypair(p,?q)

print?"Your?public?key?is?",?public?,"?and?your?private?key?is?",?private

message?=?raw_input("Enter?a?message?to?encrypt?with?your?private?key:?")

encrypted_msg?=?encrypt(private,?message)

print?"Your?encrypted?message?is:?"

print?''.join(map(lambda?x:?str(x),?encrypted_msg))

print?"Decrypting?message?with?public?key?",?public?,"?.?.?."

print?"Your?message?is:"

print?decrypt(public,?encrypted_msg)

總結

以上是生活随笔為你收集整理的python写接口自动化需要rsa加密_RSA加密,请问如何用Python实现该加密过程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。