问答

这个rsa算法哪里出错了呢? 为什么算的不对

作者:admin 2021-05-12 我要评论

public static void main(String[] args) { int x = 3; int y = 11; int n = x * y; int m = (x-1) * (y-1); // 20 //e * d - 1 = y * m int e = 3; int d = 7; ...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)
public static void main(String[] args) {
 int x = 3;
 int y = 11;
 int n = x * y;
 int m = (x-1) * (y-1); // 20
 //e * d - 1 = y * m 
 int e = 3;
 int d = 7;
 System.out.println((e * d) % m); // 1
 //public  n  e //private n d
 byte[] bytes = "a".getBytes(StandardCharsets.UTF_8);
 //a^e % n = b 加密
 pbyte(bytes);
 int[] bytesne = new int[bytes.length];
 for (int i = 0; i <bytes.length ; i++) {
 long c = bytes[i];
 for (int j = 1; j <e; j++) {
 c*=bytes[i];
 }
 bytesne[i] = (byte)(c % n);
 }
 pint(bytesne);
 //b^d % n = a
 //a^d % n = b 解密
 byte[] rr = new byte[bytes.length];
 for (int i = 0; i <bytesne.length ; i++) {
 long c = bytesne[i];
 for (int j = 1; j <d; j++) {
 c*=bytesne[i];
 }
 rr[i] = (byte)(c % n);
 }
 pbyte(rr);
 System.out.println(new String(rr));
 Integer.toBinaryString(n);
}
public static void pbyte(byte[] bytes){
 for (byte aByte : bytes) {
 System.out.print(aByte + ">>");
 }
 System.out.println();
}
public static void pint(int[] bytes){
 for (int aByte : bytes) {
 System.out.print(aByte+ ">>");
 }
 System.out.println();
}

1
97>>
25>>
31>>

###

RSA要求明文m<n,而"a"=97>33。
计算没问题,解密出来31≡97 mod 33。

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 这个rsa算法哪里出错了呢? 为什么算的

    这个rsa算法哪里出错了呢? 为什么算的

  • 在Win10(2004) linux kali子系统,apt-g

    在Win10(2004) linux kali子系统,apt-g

  • VueJs的项目,里面嵌套了iframe,在360

    VueJs的项目,里面嵌套了iframe,在360

  • rsa加密溢出该如何解决?

    rsa加密溢出该如何解决?

腾讯云代理商
海外云服务器