java 读取公钥文件并使用该密钥加密另一个文件时出现问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6124310/
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-10-30 14:25:10  来源:igfitidea点击:

problem reading a publickey file and use the key to encrypt another file

javaencryptionpublic-key

提问by Yolo

I've been strugling with reading a publickey file which I want to get the key sting in the file and use it to encrypt another file. I'm using RSA PKCS1 v1.5 in encrypting and signing the file with SH1 hashing algorythim but thats not the problem, the problem is that I've been supplied with the publickey file to use when encrypting and I cant seem to win with reading the file and generating a publicKey object.

我一直在努力阅读一个公钥文件,我想获取文件中的密钥并用它来加密另一个文件。我正在使用 RSA PKCS1 v1.5 使用 SH1 散列算法对文件进行加密和签名,但这不是问题,问题是我提供了在加密时使用的公钥文件,但我似乎无法赢得阅读文件并生成一个 publicKey 对象。

Here's the code:

这是代码:

void setPublicKey(String file) 
{ 
     try 
     { 
       FileInputStream keyfis = new FileInputStream(file); 
       byte[] encKey = new byte[keyfis.available()]; keyfis.read(encKey);
       keyfis.close();
       X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey);
       KeyFactory keyFactory = KeyFactory.getInstance("RSA");
// I get an exception on the below line
       publicKey = keyFactory.generatePublic(pubKeySpec);
     } catch (Exception e)
       {
         e.printStackTrace();
       }
}

Can someone please help!!

有人可以帮忙吗!!

回答by Robert

AFAIK X509 encoded keys are binary files encoded using ASN.1. Therefore the question on new-lines at the end does not make any sense.

AFAIK X509 编码的密钥是使用 ASN.1 编码的二进制文件。因此,最后关于换行的问题没有任何意义。

If you have a text file you have a PEM encoded file and I am currently not sure which KeySpec you have to use in this case.

如果你有一个文本文件,你就有一个 PEM 编码的文件,我目前不确定在这种情况下你必须使用哪个 KeySpec。

You may convert the PEM encoded key to a DER encoded key (e.g. using OpenSSL) or you can use BouncyCastlewhich as support for loading PEM encoded keys.

您可以将 PEM 编码的密钥转换为 DER 编码的密钥(例如使用 OpenSSL),或者您可以使用BouncyCastle作为加载 PEM 编码的密钥的支持。

BTW: Using keyfis.read(encKey);is dangerous as the read method only reads up encKey bytes but don't have to. Better create a DataInputStream from the InputStream and use readFully(encKey):

顺便说一句:使用keyfis.read(encKey);是危险的,因为 read 方法只读取 encKey 字节,但不必。更好地从 InputStream 创建一个 DataInputStream 并使用 readFully(encKey):

new DataInputStream(keyfis).readFully(encKey);

回答by Yolo

Found the solution but not sure yet if its the right solution coz I still have to get the PrivateKey and decrypt the file but for now I was able to encrypt it using the supplied PublicKey as the modulus but I don't have the exponent and I just used some common number “65537” as the exponent Which I read that it is not a critical part of the encryption.

找到了解决方案,但还不确定它是否是正确的解决方案,因为我仍然需要获取 PrivateKey 并解密文件,但现在我能够使用提供的 PublicKey 作为模数对其进行加密,但我没有指数,我只是使用了一些常见的数字“65537”作为指数,我读到它不是加密的关键部分。

I had to change the logic to use the RSAPublicKeySpec (which uses BigInteger and Base64Decoder)instead of X509EncodedKeySpec to set the KeySpec And continue to use the KeyFactory object to generate the public key. Now this logic NEEDS the modulus and exponent.

我不得不更改逻辑以使用 RSAPublicKeySpec(使用 BigInteger 和 Base64Decoder)而不是 X509EncodedKeySpec 来设置 KeySpec 并继续使用 KeyFactory 对象来生成公钥。现在这个逻辑需要模数和指数。

byte[] buffer = new byte[(int) new File(file).length()];
BufferedInputStream f = new BufferedInputStream(new FileInputStream(file));
f.read(buffer);
String modulusBase64 = new String(buffer);
BASE64Decoder b64dec = new BASE64Decoder();
String exponentBase64 = "65537";
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(new BigInteger    (1,                      b64dec.decodeBuffer(modulusBase64)), new BigInteger(1,     b64dec.decodeBuffer(exponentBase64)));
KeyFactory publicKeyFactory = KeyFactory.getInstance("RSA");
publicKey = publicKeyFactory.generatePublic(publicKeySpec);

//This is the PublicKey in the file. "J45t4SWGbFzeNuunHliNDZcLVeFU7lOpyNkX1xX+sVNaVJK8Cr0rSjUkDC8h9n+Zg7m0MVYk0byafPycmzWNDynpvj2go9mXwmUpmcQprX1vexxT5j1XmAaBZFYaJRcPWSVU92pdNh1Sd3USdFjgH0LQ5B3s8F95xdyc/5I5LDKhRobx6c1gUs/rnJfJjAgynrE4AsNsNem+STaZWjeb4J+f5Egy9xTSEl6UWxCClgCwhXopy10cBlH8CucpP0cyckOCIOloJ7mEMRCIpp6HPpYexVmXXSikTXh7aQ7tSlTMwUziIERc/zRpyj1Nk96Y7V8AorLFrn1R4Of66mpAdQ=="

//这是文件中的公钥。“J45t4SWGbFzeNuunHliNDZcLVeFU7lOpyNkX1xX + sVNaVJK8Cr0rSjUkDC8h9n + Zg7m0MVYk0byafPycmzWNDynpvj2go9mXwmUpmcQprX1vexxT5j1XmAaBZFYaJRcPWSVU92pdNh1Sd3USdFjgH0LQ5B3s8F95xdyc / 5I5LDKhRobx6c1gUs / rnJfJjAgynrE4AsNsNem + STaZWjeb4J + f5Egy9xTSEl6UWxCClgCwhXopy10cBlH8CucpP0cyckOCIOloJ7mEMRCIpp6HPpYexVmXXSikTXh7aQ7tSlTMwUziIERc / zRpyj1Nk96Y7V8AorLFrn1R4Of66mpAdQ ==”