土办法快速求密码学RSA中的d值

  RSA加密算法是一种非对称加密算法。可用于数字签名、加密/解密、密钥交换。现在讲讲其加密/解密部分。

  在加密/解密算法中,需要使用到的数字有素数p、素数q、e、d;其中,
  e满足gcd((p-1)*(q-1),e)= 1,即e与(p-1)*(q-1)互质。
  d则满足e*d mod(p-1)*(q-1)= 1。


  双方已知n=p*q,发送方已知e,接收方已知d。加密算法为C=M^e mod n,解密算法为M=C^d mod n,即M=M^ed mod n。(*C为密文,M为明文)

  d的取值可用扩展欧几里德算法求出。(以下为扩展欧几里德算法例子)


  将(p-1)*(q-1)代入到a,将e代入到b,当求到b=0时,取x=1,y=0;然后开始向上推导x、y:楼上x=楼下y,楼上y=楼上x-((楼上a/b向上取值)*(楼下y));最终求到第一个y(以y0表示)时结束。此时,d=y0 mod a。

 然而,用此方法求d有些麻烦。鄙人有一个土办法可以快速求出大部分的d值。

 利用e*d mod (p-1)(q-1)=1;我们可以知道:e*d=((p-1)*(q-1))的倍数+1。所以只要使用((p-1)*(q-1))的倍数+1除以e,能整除时,商便是d值。(一般在2倍就能求出d值)

  这个土办法比扩展欧几里德算法快得多,但正确率不能保证100%。所以还是适当用用就行。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是Java实现RSA算法的示例代码: ```java import java.math.BigInteger; import java.security.SecureRandom; public class RSA { private BigInteger modulus; private BigInteger privateKey; private BigInteger publicKey; // 初始化RSA算法 public RSA(int bitLength) { SecureRandom random = new SecureRandom(); BigInteger p = BigInteger.probablePrime(bitLength / 2, random); BigInteger q = BigInteger.probablePrime(bitLength / 2, random); BigInteger phi = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE)); this.modulus = p.multiply(q); this.publicKey = new BigInteger("65537"); // 一般取65537 this.privateKey = this.publicKey.modInverse(phi); } // 加密明文 public BigInteger encrypt(BigInteger plaintext) { return plaintext.modPow(publicKey, modulus); } // 解密密文 public BigInteger decrypt(BigInteger ciphertext) { return ciphertext.modPow(privateKey, modulus); } public static void main(String[] args) { RSA rsa = new RSA(1024); BigInteger plaintext = new BigInteger("123456"); System.out.println("明文:" + plaintext); BigInteger ciphertext = rsa.encrypt(plaintext); System.out.println("密文:" + ciphertext); BigInteger decryptedText = rsa.decrypt(ciphertext); System.out.println("解密后的明文:" + decryptedText); } } ``` 在上述代码,我们首先生成了一个RSA对象,并指定了密钥长度为1024位。接着,我们可以使用encrypt方法对明文进行加密,使用decrypt方法对密文进行解密。最后,我们在main方法对一个简单的明文进行加密、解密并输出结果。 需要注意的是,RSA算法是一种公钥加密算法,因此我们只需要保护好私钥即可,公钥可以公开,不需要保密。在实际应用,我们可以将公钥告知通信对方,让对方使用公钥对数据进行加密,然后再使用私钥进行解密。这样可以保证数据的安全性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值