【问题标题】:I can't decode my own RSA message! (using java)我无法解码自己的 RSA 消息! (使用Java)
【发布时间】:2020-07-18 03:03:24
【问题描述】:

所以我想使用 RSA 加密为我加密一些信息,但有时我完成后无法对其进行解码。这是我的代码:

import java.math.BigInteger;

public class rsa_practice {
    public static void main(String[] args) {

        BigInteger p = new BigInteger("61");
        BigInteger q = new BigInteger("53");
        BigInteger e = new BigInteger("17");
        BigInteger n = p.multiply(q);
        BigInteger phi = p.subtract(new BigInteger("1")).multiply(q.subtract(new BigInteger("1")));
        BigInteger m = new BigInteger("22");    //24390, 1758
        BigInteger d = e.modInverse(phi);
        BigInteger c = m.modPow(e, n);
        BigInteger mAGAIN = c.modPow(d, n);

        System.out.println("" + c);
        System.out.println("" + mAGAIN);
    }
}

m 应该始终等于 mAGAIN,但由于某种原因,这对我来说并非总是如此。例如,如果 m 为 22 或 1758,则 mAGAIN 为 22 或 1758。但是,如果 m 为 24390,则由于某些原因,mAGAIN 为 1759。我不明白为什么在我完成后加密和解密 m 会改变它的值。任何反馈或建议将不胜感激!

【问题讨论】:

    标签: java encryption rsa biginteger


    【解决方案1】:

    这是因为 24390 大于 n (=3233)。事实上 24390 % 3233 = 1759,所以你的算法一切正常。

    RSA 仅在 m

    【讨论】:

    • 我不敢相信我忘记了 -_- 非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 2021-01-08
    • 2017-02-16
    • 1970-01-01
    • 2018-03-02
    相关资源
    最近更新 更多