我有DER(可分辨编码规则)编码二进制数据形式的非对称加密公钥,这是ASN.1(抽象语法表示法一)编码的限制形式。
MIIB / DCCAWmgAwIBAgIQpbarEXfe8rVDlWlg2T + ixzAJBgUrDgMCHQUAMBgxFjAUBgNVBAMTDUJBTktESE9GQVIwMDEwIBcNMTMwNzEwMDkxMjU5WhgPMjA5OTEyMjkyMDAwMDBaMBgxFjAUBgNVBAMTDUJBTktESE9GQVIwMDEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALZTB / 2vKxWwCGhUdywVvikj8klvlzpZTJbVd0bRIN82bTTzp53SDXczc7mkto4vsqelGqnyjZcigyhj5y60SWYggc83d89I + i2Vo77am6aW8tfx1p / x9Op6bDLIN8V0uyoBK8IhRbuiugHmbP69Fyq4vXQ4 + D2EzmmOuPRQfg4BAgMBAAGjTTBLMEkGA1UdAQRCMECAEAMkZd7uwQQG7803GjCmF7yhGjAYMRYwFAYDVQQDEw1CQU5LREhPRkFSMDAxghCltqsRd97ytUOVaWDZP6LHMAkGBSsOAwIdBQADgYEAhau3OD9QPoJm + H8v70WQmGUwJaS2IZORo / f8sMgUnVA6qoiD7BRkv8VVT0No4H + 77YnYR2mtlCkU1BenKM3bC4WQXsXawMDSOoJcqBVLBFpYzl / 8xpNrRyA8yyLUX37kXmH6mdioGLiNSKhQvX / XBYkTeOnsS2umt + zjS2JDS + G =
如何加密此密钥以生成文本1234的密码。
我在互联网上尝试了以下示例代码:
public class ATMPINClient{
public static void main(String[] args) throws Exception {
String pkey = "MIIB/DCCAWmgAwIBAgIQpbarEXfe8rVDlWlg2T+ixzAJBgUrDgMCHQUAMBgxFjAUBgNVBAMTDUJBTktESE9GQVIwMDEwIBcNMTMwNzEwMDkxMjU5WhgPMjA5OTEyMjkyMDAwMDBaMBgxFjAUBgNVBAMTDUJBTktESE9GQVIwMDEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALZTB/2vKxWwCGhUdywVvikj8klvlzpZTJbVd0bRIN82bTTzp53SDXczc7mkto4vsqelGqnyjZcigyhj5y60SWYggc83d89I+i2Vo77am6aW8tfx1p/x9Op6bDLIN8V0uyoBK8IhRbuiugHmbP69Fyq4vXQ4+D2EzmmOuPRQfg4BAgMBAAGjTTBLMEkGA1UdAQRCMECAEAMkZd7uwQQG7803GjCmF7yhGjAYMRYwFAYDVQQDEw1CQU5LREhPRkFSMDAxghCltqsRd97ytUOVaWDZP6LHMAkGBSsOAwIdBQADgYEAhau3OD9QPoJm+H8v70WQmGUwJaS2IZORo/f8sMgUnVA6qoiD7BRkv8VVT0No4H+77YnYR2mtlCkU1BenKM3bC4WQXsXawMDSOoJcqBVLBFpYzl/8xpNrRyA8yyLUX37kXmH6mdioGLiNSKhQvX/XBYkTeOnsS2umt+zjS2JDS+g=";
String atmPin = "1234";
byte[] key = pkey.getBytes();
encrypt(atmPin, key);
}
public static byte[] encrypt(String atmPin, byte[] keyCode)
throws Exception {
X509Certificate cert = X509Certificate.getInstance(keyCode);
RSAPublicKey rk = (RSAPublicKey) cert.getPublicKey();
System.out.println("Algorithm: " + rk.getAlgorithm());
System.out.println("Modules : " + rk.getModulus());
System.out.println("Hex - Modules : " + rk.getModulus().toString(16).toUpperCase());
System.out.println("Exponent : " + rk.getPublicExponent());
System.out.println("Name : " + cert.getSubjectDN().getName());
System.out.println("Issuer Name : " + cert.getIssuerDN().getName());
System.out.println("Not After : " + cert.getNotAfter());
System.out.println("Not Before : " + cert.getNotBefore());
System.out.println("Format : " + cert.getPublicKey().getFormat());
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, rk);
System.out.println("Actual PIN : " + atmPin);
byte[] PIN = cipher.doFinal(atmPin.getBytes("UTF-8"));
String encPIN = new sun.misc.BASE64Encoder().encode(PIN);
System.out.println("Encrypted Pin : " + encPIN);
return PIN;
}
}
获得以下错误:
Exception in thread "main" javax.security.cert.CertificateException: InvocationTargetException: javax.security.cert.CertificateException: invalid DER-encoded certificate data
at javax.security.cert.X509Certificate.getInst(X509Certificate.java:226)
at javax.security.cert.X509Certificate.getInstance(X509Certificate.java:186)
at atmpinclient.ATMPINClient.encrypt(ATMPINClient.java:33)
at atmpinclient.ATMPINClient.main(ATMPINClient.java:26)
Java Result: 1
请告知如何实现此加密。
答案 0 :(得分:0)
我可以通过将开始和结束证书附加到公钥来加密
String beignKey = "-----BEGIN CERTIFICATE-----"+System.getProperty("line.separator");;
String pkey = "MIIB/DCCAWmgAwIBAgIQpbarEXfe8rVDlWlg2T+ixzAJBgUrDgMCHQUAMBgxFjAUBgNVBAMTDUJBTktESE9GQVIwMDEwIBcNMTMwNzEwMDkxMjU5WhgPMjA5OTEyMjkyMDAwMDBaMBgxFjAUBgNVBAMTDUJBTktESE9GQVIwMDEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALZTB/2vKxWwCGhUdywVvikj8klvlzpZTJbVd0bRIN82bTTzp53SDXczc7mkto4vsqelGqnyjZcigyhj5y60SWYggc83d89I+i2Vo77am6aW8tfx1p/x9Op6bDLIN8V0uyoBK8IhRbuiugHmbP69Fyq4vXQ4+D2EzmmOuPRQfg4BAgMBAAGjTTBLMEkGA1UdAQRCMECAEAMkZd7uwQQG7803GjCmF7yhGjAYMRYwFAYDVQQDEw1CQU5LREhPRkFSMDAxghCltqsRd97ytUOVaWDZP6LHMAkGBSsOAwIdBQADgYEAhau3OD9QPoJm+H8v70WQmGUwJaS2IZORo/f8sMgUnVA6qoiD7BRkv8VVT0No4H+77YnYR2mtlCkU1BenKM3bC4WQXsXawMDSOoJcqBVLBFpYzl/8xpNrRyA8yyLUX37kXmH6mdioGLiNSKhQvX/XBYkTeOnsS2umt+zjS2JDS+g=";
String endKey = System.getProperty("line.separator")+"-----END CERTIFICATE-----" ;
pkey = beignKey + pkey + endKey;