英文:
Decode Base64 JKS Trust Store in Java
问题
以下是翻译好的内容:
我有一个基于64位JKS信任存储字符串,我正在尝试解码它。
当我使用这个网站来解码文件时,我会得到一个可下载的文件,其中包含以下细节:
当我在-Djavax.net.ssl.trustStore=文件路径下使用它时,这个文件可以正常工作。
现在我正在尝试使用Java自己解码该文件并将其写入文件:
byte[] decoded = Base64.getDecoder().decode(data);
FileOutputStream fos = new FileOutputStream(new File(basePath));
fos.write(decoded);
这种方式会得到像����这样的不可读字符。
我还尝试将字节数组转换为字符串:
StringBuilder sbHexDump = new StringBuilder();
for (byte b : decoded) {
sbHexDump.append(String.format("%02x", b));
}
这种方式得到的十六进制转储与我从网站下载的相同,但没有空格和换行符。无论哪种方式,我都会得到以下错误:
java.io.IOException: 无效的密钥库格式
at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:663)
at sun.security.provider.JavaKeyStore$JKS.engineLoad(JavaKeyStore.java:56)
at sun.security.provider.KeyStoreDelegator.engineLoad(KeyStoreDelegator.java:224)
at sun.security.provider.JavaKeyStore$DualFormatJKS.engineLoad(JavaKeyStore.java:70)
at java.security.KeyStore.load(KeyStore.java:1445)
我将非常感谢您的帮助。
英文:
I have based 64 JKS trust store string which I'm trying to decode.
When I'm using this site to decode the file I'm getting a downloadable file with the following details:
This file is working when I'm using it with -Djavax.net.ssl.trustStore=path-to-file
Now I'm trying to decode the file myself using Java and write it to a file:
byte[] decoded = Base64.getDecoder().decode(data);
FileOutputStream fos = new FileOutputStream(new File(basePath));
fos.write(decoded);
This way I'm getting not readable characters like ���� .
I also try to convert the byte array to String:
StringBuilder sbHexDump = new StringBuilder();
for (byte b : decoded) {
sbHexDump.append(String.format("%02x", b));
}
This way I'm getting the same hexdump as I downloaded from the website, but without spaces and newlines.
In both ways, I get the following error:
java.io.IOException: Invalid keystore format
at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:663)
at sun.security.provider.JavaKeyStore$JKS.engineLoad(JavaKeyStore.java:56)
at sun.security.provider.KeyStoreDelegator.engineLoad(KeyStoreDelegator.java:224)
at sun.security.provider.JavaKeyStore$DualFormatJKS.engineLoad(JavaKeyStore.java:70)
at java.security.KeyStore.load(KeyStore.java:1445)
I would appreciate your help
答案1
得分: 4
这样我得到的是不可读的字符,类似于����。
是的,那又怎样?JKS是一种二进制格式。并非所有的字节都映射到可打印字符。只要keytool
能够读取它,就没问题。
英文:
> This way I'm getting not readable characters like ���� .
Yes, so what? JKS is a binary format. Not all bytes are mapped to printable characters. As long as keytool
can read it you're good.
答案2
得分: 2
除了了解 base64 编码方案(https://en.wikipedia.org/wiki/Base64)之外,您可能还需要注意任何大端字节顺序的问题,以确定字节的放置方式。编码只是取前8位,对其中的6位进行编码,将接下来的8位连接到剩余的2位上并对其中的6位进行编码,将接下来的8位连接到剩余的4位上并对其中的6位进行编码,最后再编码剩下的最后6位。如果在处理3字节组时遇到文件结束(eof),会有一个等号(=)填充系统来进行编码。
唯一的技巧是从正确的一端获取位,并将位连接到正确的一端。如果您正在对非字节数据(如整数)进行二进制编码,您必须知道它们是如何被大端编码的,因为整数的低字节可能是小端或大端编码的第1个或第4个字节。
添加空格、换行符或类似字符只是为了使其在文本编辑器、屏幕查看、电子邮件、人类等方面更加可读。
英文:
In addition to knowing the base 64 encoding scheme (https://en.wikipedia.org/wiki/Base64), you may need to be aware of any big-endian issues as to byte placement. Encoding is just taking the first 8 bits, encodng 6 of them, concatenate the next 8 to the remaining 2 and encoding 6 of them, concatenate the next 8 to the remaining 4 and encoding 6 of them, and encode the last 6. If you hit eof in the midst of a 3 byte group, there is an = padding system to encode this.
The only trick is to take your bits from the correct end and concatenate your bits on the correct end. If you are encoding binary non-byte data like integers, you have to be big-endian aware of how they encoded it, as the low byte of the integer may be either 1st or 4th byte encoded for little or big endian, respectively.
The addition of spaces or line feeds or the like is just to make it more palatable to text editors, screen viewing, emailing, humans, etc.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论