7

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

 3 years ago
source link: https://www.oschina.net/question/3775878_2320413
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

cielSwift 发布于 12/13 21:42
阅读 893
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>>

猴子叫你一声敢答应吗
12/16 10:56

N和M的取值太笑了,阁下的逻辑大概看了下是正确的,但是你要加密的是ascii你的欧拉函数的值应该大于你要加密的单个数值。e≡ 1 (mod φ(n))

jia234
12/16 11:09  Android
我帮你解读下代码,兄弟其实你只做了一件事:把a转成了utf-8返回三个byte,一个utf-8编码是三个字节组成,然后你输出了这三个字节。所有压根还没涉及到RSA
osc_07305217
12/17 19:31

引用来自“猴子叫你一声敢答应吗”的评论

N和M的取值太笑了,阁下的逻辑大概看了下是正确的,但是你要加密的是ascii你的欧拉函数的值应该大于你要加密的单个数值。e≡ 1 (mod φ(n))

1. 如猴子所说,明文要小于密钥长度(小于N)。

2. 关于模幂运算,要作为大数参与运算;对byte数组每位进行模幂,有点奇怪;运算容易溢出(显然,你也注意到了)。

WindSpeed
前天 20:32

这颜色能不能调一下,我的天 五颜六色的

tcxu
前天 20:39
楼主提问的代码,没有按照java代码存入。
tcxu
前天 20:36

引用来自“WindSpeed”的评论

这颜色能不能调一下,我的天 五颜六色的

调一下颜色:

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();
}
up-8dcc3e76a00cab9e11bfe6e5da4855aa4d5.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK