如何将私钥从 .der 文件加载到 java 私钥对象中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20119874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:17:42  来源:igfitidea点击:

how to load the private key from a .der file into java private key object

javaformatbouncycastlepkcs#8

提问by monim

I'm writing a java program to import private keys from files within the file system and make a private key object, using java... I could do it for files in .pemformat but, with .der format, I had no idea what to do, since I couldnt firstly detect the algorithm used to generate the keys. within .pemfiles I could determine the algorithm from the header for PKCS#1which have a header like
-----BEGIN RSA PRIVATE KEY----
formats and used the bouncycastle pemreader for those in PKCS#8 which have a header
-----BEGIN PRIVATE KEY-----but with those in .der format no idea :(
also if anyone have an idea about .keyformat tell me
thanx

我正在编写一个 java 程序来从文件系统中的文件中导入私钥并使用 java 创建一个私钥对象...我可以对.pem格式的文件执行此操作,但是使用 .der 格式,我不知道该做什么做,因为我无法首先检测用于生成密钥的算法。在.pem文件中,我可以从PKCS#1具有标题
-----BEGIN RSA PRIVATE KEY----
格式的标题中确定算法,并使用 bouncycastlepem阅读器用于 PKCS#8 中的那些有标题
-----BEGIN PRIVATE KEY-----但 .der 格式的人不知道:(
如果有人知道.key格式告诉我
thanx

采纳答案by gtrig

If your DER files are in PKCS#8 format, you can use the Java KeyFactoryand do something like this:

如果您的 DER 文件是 PKCS#8 格式,您可以使用 Java KeyFactory并执行以下操作:

// Read file to a byte array.
String privateKeyFileName = "C:\myPrivateKey.der";   
Path path = Paths.get(privateKeyFileName);
byte[] privKeyByteArray = Files.readAllBytes(path);

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privKeyByteArray);

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

PrivateKey myPrivKey = keyFactory.generatePrivate(keySpec);

System.out.println("Algorithm: " + myPrivKey.getAlgorithm());

You mentioned that you may not know what algorithm the key is using. I'm sure there is a more elegant solution than this, but you could create several KeyFactoryobjects (one for each possible algorithm) and try to generatePrivate()on each one until you do not get an InvalidKeySpecException.

您提到您可能不知道密钥使用的是什么算法。我确信有比这更优雅的解决方案,但是您可以创建多个KeyFactory对象(每个可能的算法一个)并尝试generatePrivate()对每个对象进行操作,直到您没有得到InvalidKeySpecException.

回答by monim

thanks @gtrig using ur idea and editing the code like this :

感谢@gtrig 使用你的想法并像这样编辑代码:

            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(KeyBytes);  
            try 
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                privateKey = keyFactory.generatePrivate(keySpec);
                algorithm = keyFactory.getAlgorithm();
                //algorithm = "RSA";
                //publicKey = keyFactory.generatePublic(keySpec);
            } catch (InvalidKeySpecException excep1) {
                try {
                    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
                    privateKey = keyFactory.generatePrivate(keySpec);
                    algorithm = keyFactory.getAlgorithm();
                    //publicKey = keyFactory.generatePublic(keySpec);
                } catch (InvalidKeySpecException excep2) {

                    KeyFactory keyFactory = KeyFactory.getInstance("EC");
                    privateKey = keyFactory.generatePrivate(keySpec);

                } // inner catch
            }

the code is working well now

代码现在运行良好