RAS加密

获取密钥步骤:

①取两个质数 p,q p , q ,他们的乘积为 n n
②随便选一个与φ(n)互质的数 a a ,并且求出a在模 φ(n) φ ( n ) 的情况下的逆元 d d

这样就得到了密钥
公钥:(n,a)
私钥:(n,d)

加密

对一个整数m进行加密,比如 m m 是某个字母的ASCII

C=ma % n C = m a   %   n

只需要把 C C 发给对方

解密

m=Cd % n

为什么呢?
由加密那里得: C=ma+kn C = m a + k n
所以:
Cd % n C d   %   n
=(ma+kn)d % n = ( m a + k n ) d   %   n
展开之后只有首项不含因子 n n ,所以
=mad % n
=mad%φ(n) % n = m a d % φ ( n )   %   n
=m1 % n = m 1   %   n
=m = m

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,似乎是RSA加密算法,而不是RAS加密算法。以下是Python实现RSA加密算法的步骤和代码: 1. 生成公钥和私钥 ```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 def generate_keypair(p, q): n = p * q phi = (p-1) * (q-1) e = random.randrange(1, phi) g = gcd(e, phi) while g != 1: e = random.randrange(1, phi) g = gcd(e, phi) d = multiplicative_inverse(e, phi) return ((e, n), (d, n)) ``` 2. 加密和解密 ```python def encrypt(pk, plaintext): key, n = pk cipher = [pow(ord(char), key, n) for char in plaintext] return cipher def decrypt(pk, ciphertext): key, n = pk plain = [chr(pow(char, key, n)) for char in ciphertext] return ''.join(plain) ``` 3. 使用示例 ```python p = 61 q = 53 public_key, private_key = generate_keypair(p, q) print("Public key: ", public_key) print("Private key: ", private_key) message = "Hello, World!" encrypted_message = encrypt(public_key, message) print("Encrypted message: ", ''.join(map(lambda x: str(x), encrypted_message))) decrypted_message = decrypt(private_key, encrypted_message) print("Decrypted message: ", decrypted_message) ``` 输出: ``` Public key: (1913, 3233) Private key: (1783, 3233) Encrypted message: 246811111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值