【发布时间】:2014-03-16 03:25:10
【问题描述】:
我正在开发一个实现 RSA 加密算法的程序,就像个人练习一样,它不会保护任何人的信息或任何东西。我试图了解如何以数字方式解释明文段落,从而对其进行加密。我知道大多数 UTF-8 字符最终只使用 1 个字节的空间,而不是人们可能认为的 2 个字节,但仅此而已。这是我的代码:
BigInteger ONE = new BigInteger("1");
SecureRandom rand = new SecureRandom();
BigInteger d, e, n;
BigInteger p = BigInteger.probablePrime(128, rand);
BigInteger q = BigInteger.probablePrime(128, rand);
BigInteger phi = (p.subtract(ONE)).multiply(q.subtract(ONE));
n = p.multiply(q);
e = new BigInteger("65537");
d = e.modInverse(phi);
String string = "test";
BigInteger plainText = new BigInteger(string.getBytes("UTF-8"));
BigInteger cipherText = plainText.modPow(e, n);
BigInteger originalMessage = cipherText.modPow(d, n);
String decrypted = new String(originalMessage.toByteArray(),"UTF-8");
System.out.println("original: " + string);
System.out.println("decrypted: " + decrypted);
System.out.println(plainText);
System.out.println(cipherText);
System.out.println(originalMessage);
System.out.println(string.getBytes("UTF-8"));
byte byteArray[] = string.getBytes("UTF-8");
for(byte littleByte:byteArray){
System.out.println(littleByte);
}
它输出:
original: test
decrypted: test
1952805748
16521882695662254558772281277528769227027759103787217998376216650996467552436
1952805748
[B@60d70b42
116
101
115
116
也许更具体地说,我想知道这一行:
BigInteger plainText = new BigInteger(string.getBytes("UTF-8"));
“test”的每个字母是否都有值,并且它们在这里按字面意思相加?就像说 t=1,e=2,s=3,t=1 例如,如果您从该字符串中获取字节,您最终会得到 7 还是只是像 1231 一样将这些值放在一起?为什么
BigInteger plainText = new BigInteger(string.getBytes("UTF-8")); 输出1952805748
【问题讨论】:
标签: java encryption encoding utf-8 biginteger