gpt4 book ai didi

java - RSA 加密时遇到 InvalidKeyException

转载 作者:行者123 更新时间:2023-12-01 22:18:51 26 4
gpt4 key购买 nike

我在尝试使用我的公钥加密时遇到了上述异常。

java.security.InvalidKeyException: IOException: DerInputStream.getLength(): lengthTag=111, too big

代码如下:

    public static String encryptWithMyPubKey (String text) throws Exception {
if (myPubKey == null) {
URL path = RSAUtils.class.getResource("pub.der");
System.out.println("Path to private key file" + path.getFile());
File f = new File(path.getFile());
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int)f.length()];
dis.readFully(keyBytes);
dis.close();

KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyBytes));
}
}

这是什么意思?

最佳答案

我已经解决了这个问题。这是由于der二进制文件格式错误造成的。将 .der 文件从资源文件夹复制到类路径时,Maven 构建会对 .der 文件应用过滤器。为了保留这些文件,您需要通过更改 pom.xml 来排除 .der 文件(或任何与此相关的二进制文件),如下所示:

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${webappDirectory}/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.der</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/java</directory>
<filtering>false</filtering>
<includes>
<include>**/*.der</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

关于java - RSA 加密时遇到 InvalidKeyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30371866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com