2013-10-22 27 views
0

我正在为每个变量添加值以获取加密邮件。所有硬编码用于测试目的。RSA加密 - 尝试加密邮件返回错误的值

返回值?????应该返回538 1729 1328 1328 2146。我有什么错误吗?这是我用我的代码唯一的问题。

public static void main(String[] args){ 
    int p = 61; 
    int q = 37; 
    int pq = p * q; 
    int phiPQ = (p - 1) * (q - 1); 
    int e = 7; 
    int d = 1543; 
    String message = encryptMsg("hello", pq, e); 
    System.out.println(message); 
} 

public static String encryptMsg(String msg, int pq, int e){ 
    BigInteger bE = new BigInteger(Integer.toString(e)); 
    BigInteger bPQ = new BigInteger(Integer.toString(pq)); 
    String encryptedMsg = ""; 
    for(int i = 0; i < msg.length(); i++){ 
     BigInteger m = new BigInteger(Integer.toString(msg.charAt(i))); 
     BigInteger bC = m.modPow(bE, bPQ); 
     encryptedMsg += " " + (char)bC.intValue(); 
    } 
    return encryptedMsg; 
} 
+0

Btw:结果的第一个值是1039,而不是538。 – LostAvatar

回答

1

我想你可能想

encryptedMsg +=" " + bC.toString(); 

目前你铸造整数为字符,所以又回到unicode的,我想,而你似乎在期待什么是整数作为字符串。