Base64编码格式的公钥证书转化为PublicKey类型

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dingchenxixi/article/details/79030448

网上查找过相应的代码,确实给的DEMO是可行的,但是只是限于一定的加密算法维度,比如说RSA4096就不行了
贴出相应的代码,供参考:

 3 import java.security.Key;
  4 import java.security.KeyFactory;
  5 import java.security.KeyPair;
  6 import java.security.KeyPairGenerator;
  7 import java.security.PrivateKey;
  8 import java.security.PublicKey;
  9 import java.security.Signature;
 10 import java.security.interfaces.RSAPrivateKey;
 11 import java.security.interfaces.RSAPublicKey;
 12 import java.security.spec.PKCS8EncodedKeySpec;
 13 import java.security.spec.X509EncodedKeySpec;
 14 
 15 import javax.crypto.Cipher;
 16 
 17 import org.apache.commons.codec.binary.Base64;
 18 
 19 import sun.misc.BASE64Decoder;
 20 import sun.misc.BASE64Encoder;
public static String str_pubK = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqPvovSfXcwBbW8cKMCgwqNpsYuzF8RPAPFb7LGsnVo44JhM/xxzDyzoYtdfNmtbIuKVi9PzIsyp6rg+09gbuI6UGwBZ5DWBDBMqv5MPdOF5dCQkB2Bbr5yPfURPENypUz+pBFBg41d+BC+rwRiXELwKy7Y9caD/MtJyHydj8OUwIDAQAB";
/**
 32        * 使用getPublicKey得到公钥,返回类型为PublicKey
 33        * @param base64 String to PublicKey
 34        * @throws Exception
 35        */
 36       public static PublicKey getPublicKey(String key) throws Exception {
 37             byte[] keyBytes;
 38             keyBytes = (new BASE64Decoder()).decodeBuffer(key);
 39             X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
 40             KeyFactory keyFactory = KeyFactory.getInstance("RSA");
 41             PublicKey publicKey = keyFactory.generatePublic(keySpec);
 42             return publicKey;
 43       }

试试这个公钥:



MIIDBTCCAe2gAwIBAgIKb0XKECkuAA7bcjANBgkqhkiG9w0BAQsFADAlMRUwEwYDVQQDDAxBQkMgVEVTVCBDQTIxDDAKBgNVBAoMA0FCQzAeFw0xNzEyMjQyMTE0MDBaFw0yMjEyMjQyMTE0MDBaME4xLzAtBgNVBAMMJjQ0OTk0NDIxMzAwLjAwMDEuMDAwMC42NjQ0NTAyNTY2MjY5MzAwMQ0wCwYDVQQLDARVd2luMQwwCgYDVQQKDANBQkMwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAJPVlBDFMqpzYWbdGe5dkiw2uRuq6Kd9/e7TZDfwS0L3mf5/kaDs93oRSqWr497SBliQeEbA3cHphWpceUpBu7DreYvH+scSsLrR8D6t7pcVQacIqL7YDYNsCpjrmZ/DQh9Ci7AZckYVu6TzojVwbbmbjo/0C3tdfWEzSq10LM51AgMBAAGjgZEwgY4wHwYDVR0jBBgwFoAUErtyLm5Iqy6z01KtzsJ33EmoKtIwCQYDVR0TBAIwADBBBgNVHR8EOjA4MDagNKAypDAwLjEQMA4GA1UEAwwHY3JsNDQ3NDEMMAoGA1UECwwDY3JsMQwwCgYDVQQKDANBQkMwHQYDVR0OBBYEFPAOIJAri/gehQGw4RtVaqa+v2mGMA0GCSqGSIb3DQEBCwUAA4IBAQCd9ofET3kZmU0932OGIZHdqDdMhrfESmM6dTutg8SfuTnniA43qYHDZFpNa86rl4Sox0lVyIx5mhRM4edSVzfYaX0WNtYlYDqkJONcn0yRenKwjrhpnyqFM3uQsMDW0VL0+Is8PKZqiaIfjPXY2a+OqY6zGN6vpUpkM0eT2GdJLZrIKM6ufKov7cTtRDfslFBmNh6N9LWqQPsItmDyXDeUIh5IKeHn+xwaL9MjYgxYhWpkMWmRbmp0o8Vf3qKLJ0wxptjGx0dhF1CcFbv5KzJRS3DLqKJAT0N1BWI8lJDkI5B553ouPMbQmOFAMgRp0eFR++Fm0aLPlyaGBZOWzrZy


参考文章:https://www.cnblogs.com/KKatherine/p/4128444.html
在测试自己的公钥时,出现X509.ObjectIdentifier() – data isn’t an object ID (tag = -96) error,在https://samebug.io/exceptions/93395/java.security.InvalidKeyException/ioexception-detect-premature-eof或许有你需要的答案,-96的错误没具体查,但是在https://stackoverflow.com/questions/18856431/rsa-keys-transformation-for-java-clarification-needed上的解决方法倒是提醒了我,我有了证书,为什么不直接获取公钥了?
1.从证书文件流中获取

InputStream inStream = null;
 try {
     inStream = new FileInputStream("fileName-of-cert");
     CertificateFactory cf = CertificateFactory.getInstance("X.509");
     X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream);
 } finally {
     if (inStream != null) {
         inStream.close();
     }
 }

2.直接获取
PublicKey myPubKey = cert.getPublicKey();
参考:https://stackoverflow.com/questions/18856431/rsa-keys-transformation-for-java-clarification-needed

展开阅读全文

没有更多推荐了,返回首页