急求援助。RSA解密后的结果错误

aslprince 2013-03-07 06:40:59

public byte[] decryptCkey(byte[] text, PrivateKey privatekey) {
byte[] decrypted = null;
try {
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance("RSA");

// decrypt the text using the private key
cipher.init(Cipher.DECRYPT_MODE, privatekey);
decrypted = cipher.doFinal(text);
} catch (Exception ex) {
ex.printStackTrace();
}
//System.out.println("Text: " + new String(text));
System.out.println(decrypted.length);
System.out.println(privatekey.getAlgorithm());
return decrypted;

该程序出错部分是,产生一个128bit通信秘钥,存入一个16byte的数组里面, 用客户的公钥加密,然后将密钥发送给客户端。
客户接受到通信密钥后,用客户的私钥解密。
这部分是解密的的代码。

我这边测试时候发现, 解密后的密钥一直就只有10位。

求分析诊断~~
通宵等解答
...全文
146 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
aslprince 2013-03-07
  • 打赏
  • 举报
回复
public byte[] encryptCKey( byte[] bKey, PublicKey publicKey) { byte[] cipherText = null; //String sKey = bKey.toString(); try { // get an RSA cipher object and print the provider final Cipher cipher = Cipher.getInstance("RSA"); // encrypt the plain text using the public key cipher.init(Cipher.ENCRYPT_MODE, publicKey); //cipherText = cipher.doFinal(bKey); cipherText = cipher.doFinal(bKey); } catch (Exception e) { e.printStackTrace(); } return cipherText; } 这部分是用客户端的公钥加密通信密钥
aslprince 2013-03-07
  • 打赏
  • 举报
回复
KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); // 192 and 256 bits may not be available// Generate the secret key specs. SecretKey skey = kgen.generateKey(); byte[] bKey = skey.getEncoded(); 这部分代码是生成通信密钥,并将通信密钥转化byte[]
aslprince 2013-03-07
  • 打赏
  • 举报
回复
//Decrypt the communication key using user encrypt private key inputStream = new ObjectInputStream(new FileInputStream(encryptDecrypt.getUserEncryptPri())); final PrivateKey userDePriKey = (PrivateKey) inputStream.readObject(); final byte[] decryptedC = encryptDecrypt.decryptCkey(encryptedCKey, userDePriKey); 这部分代码是从文件中读入用户的私钥,目前检测读入的私钥是正确的

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧