我无法解码我自己的 RSA 消息! (使用 java)
I can't decode my own RSA message! (using java)
所以我想用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 完成后会更改其值。如有任何反馈或建议,我们将不胜感激!
这是因为24390大于n(=3233)。事实上 24390 % 3233 = 1759,所以你的算法一切正常。
RSA 仅在 m < n 时有效。
所以我想用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 完成后会更改其值。如有任何反馈或建议,我们将不胜感激!
这是因为24390大于n(=3233)。事实上 24390 % 3233 = 1759,所以你的算法一切正常。
RSA 仅在 m < n 时有效。