java 公匙 私匙_JAVA RSA加密问题:公匙解密失败 使用私匙加密获得的密文用公匙解密失败...

相关代码如下:

public static final String PUBLIC_KEY = "RSAPublicKey";//公钥

public static final String PRIVATE_KEY = "RSAPrivateKey";//私钥

/**

* 初始化密钥

* @return

* @throws Exception

*/

public static Map initKey()throws Exception{

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");

keyPairGenerator.initialize(1024);

KeyPair keyPair = keyPairGenerator.generateKeyPair();

//公钥

RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

//私钥

RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

Map keyMap = new HashMap(2);

keyMap.put(PUBLIC_KEY, publicKey);

keyMap.put(PRIVATE_KEY, privateKey);

return keyMap;

}

/**

* 取得公钥,并转化为String类型

* @param keyMap

* @return

* @throws Exception

*/

public static String getPublicKey(Map keyMap)throws Exception{

Key key = (Key) keyMap.get(PUBLIC_KEY);

return Base64.encodeBase64String(key.getEncoded());

}

/**

* 取得私钥,并转化为String类型

* @param keyMap

* @return

* @throws Exception

*/

public static String getPrivateKey(Map keyMap) throws Exception{

Key key = (Key) keyMap.get(PRIVATE_KEY);

return Base64.encodeBase64String(key.getEncoded());

}

/**

* 用私钥加密

* @param data 数据

* @param key 密钥

* @return

* @throws Exception

*/

public static byte[] encryptByPrivateKey(byte[] data,String key)throws Exception{

//解密密钥

byte[] keyBytes = Base64.decodeBase64(key);

//取私钥

PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);

//对数据加密

Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

cipher.init(1, privateKey);

return cipher.doFinal(data);

}

/**

* 用公钥解密

* @param data 加密数据

* @param key 密钥

* @return

* @throws Exception

*/

public static byte[] decryptByPublicKey(byte[] data, String key)

throws Exception

{

byte[] keyBytes = Base64.decodeBase64(key);

X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

Key publicKey = keyFactory.generatePublic(x509KeySpec);

Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

cipher.init(2, publicKey);

return cipher.doFinal(data);

}

public static void test(){

try{

String publicKey=getPublicKey(initKey());

String privateKey=getPrivateKey(initKey());

System.out.println("私钥加密——公钥解密");

String inputStr = "sign";

byte[] data = inputStr.getBytes("utf-8");

byte[] encodedData = encryptByPrivateKey(data, privateKey);

byte[] decodedData = decryptByPublicKey(encodedData, publicKey);

String outputStr = new String(decodedData);

System.out.println("加密前: " + inputStr + "nr" + "解密后: " + outputStr);

}

catch (Exception e)

{

e.printStackTrace();

}

}

调试反馈错误详情:

javax.crypto.BadPaddingException: Decryption error

at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)

at sun.security.rsa.RSAPadding.unpad(Unknown Source)

at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:356)

at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)

at javax.crypto.Cipher.doFinal(Cipher.java:2165)

at decryptByPublicKey(Signaturer.java:130)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值