java - How to get private key from jks keystore for signing the message with privatekey using shal256rsa in java
问题描述
I want to encrypt the message with signed private key.and verify the signature but getting error in verify signature
//importing the crt and key into p12 file and importing into jks file
openssl pkcs12 -export -in D:\cedge_uat\STAR_cedgenetbanking_in.crt -inkey D:\cedge_uat\newcedgenetbanking251920.key -name cedge1 -out D:\cedge_uat\convertedfile1.p12
keytool -importkeystore -deststorepass cedge1 -destkeystore newkeystore.jks -srckeystore D:\cedge_uat\convertedfile1.p12 -srcstoretype PKCS12
public static PrivateKey generatePrivateKey() throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException, UnrecoverableKeyException{
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
//Jks file path
FileInputStream fis;
PrivateKey privateKey = null;
try {
fis = new FileInputStream("C:/Program Files/Java/jre1.8.0_171/bin/keystore12.jks");
keyStore.load(fis, "changeit".toCharArray());
//jks file password
privateKey = (PrivateKey) keyStore.getKey("changeit", "changeit".toCharArray());
System.out.println("privateKey--"+privateKey);
} catch (FileNotFoundException e) {
System.out.println("e--"+e);
e.printStackTrace();
}
return privateKey;
}
public static String signature(String sessionkey, PrivateKey privatekey) throws Exception
{
Signature sign = Signature.getInstance("SHA256withRSA");
sign.initSign(privatekey);
sign.update(sessionkey.getBytes());
return new String(Base64.getEncoder().encodeToString(sign.sign()));
}
public static PublicKey generatePublicKey() throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException, UnrecoverableKeyException{
KeyStore keyStore = KeyStore.getInstance("JKS");
FileInputStream fis;
PublicKey publicKey=null;
try {
fis = new FileInputStream("C:/Program Files/Java/jre1.8.0_171/bin/newkeystore.jks");
keyStore.load(fis, "cedge1".toCharArray());
Certificate cert = keyStore.getCertificate("cedge1");
publicKey = cert.getPublicKey();
} catch (FileNotFoundException e) {
System.out.println("e--"+e);
e.printStackTrace();
}
return publicKey;
}
//Signature verification using their public key
public static boolean verifySignature(String input, String signature, PublicKey publicKey) throws Exception
{
Signature verifySig = Signature.getInstance("SHA256withRSA");
verifySig.initVerify(publicKey);
byte[] singedData = Base64.getDecoder().decode(signature);
verifySig.update(Base64.getDecoder().decode(input));
boolean isVerified = verifySig.verify(singedData);
System.out.println("isVerified "+ isVerified);
return isVerified;
}
Getting below response:
privateKey--sun.security.rsa.RSAPrivateCrtKeyImpl@ffe594cb privateKey=sun.security.rsa.RSAPrivateCrtKeyImpl@ffe594cb Signature=QtFcvROXmFb+SIqi/sFG5BXtMviidqWYP0ae/Z0PQNKbxYg9LiJMAqjU+XB+V7awkpVpeV8/TmrxO2AFi1hDLOOOdL4rVY1xxPTGw77Q==
publicKey=Sun RSA public key, 2048 bits modulus: 2170304779081185713374867545321744099657549785541087943424133659953554520622568213352873219823464920874049569111847413669517192082390131 public exponent: 65537 Exception in thread "main" java.lang.IllegalArgumentException: Last unit does not have enough valid bits at java.util.Base64$Decoder.decode0(Unknown Source)
public static void main(String[] args) throws Exception
{
//generatePrivateKey();
PrivateKey privateKey=generatePrivateKey();
System.out.println("privateKey="+privateKey);
String signature = signature("hello",privateKey);
System.out.println("Signature="+signature);
//generatePublicKey();
PublicKey publicKey=generatePublicKey();
System.out.println("publicKey="+publicKey);
System.out.println("verify="+ verifySignature("hello",signature,publicKey));
}
解决方案
推荐阅读
- reactjs - React 应用程序仅在开发者模式下才会慢到爬行
- python - 无法在 for 循环中更新具有多个值的数据库列
- python - 只是希望有人向我解释他们的确切含义。这是一个 Codecademy 问题
- python - 如何使用 OOP 确保所有元素都显示在 Tkinter 画布上
- android - 为什么设计者使用“@+id”而不是“@id”作为约束?
- awk - 防止 awk 添加非整数?
- python - 安装 anaconda 时,boto3 不会安装在 anaconda-2020.07 /apps/anaconda3-2020.07/
- elasticsearch - elasticsearch-certutil 创建的证书在生产中不可用?
- docker - 如何在 Alpine docker 容器中接收 UDP 消息?
- html - 为什么我使用 % 时背景图像会消失?