Public key cryptography is a well-known concept, but for some reason the JCE (Java Cryptography Extensions) documentation doesn't at all make it clear how to interoperate with common public key formats such as those produced by openssl. If you try to do a search on the web for how to make RSA public key cryptography work in Java, you quickly find a lot of people asking questions and not a lot of people answering them. In this post, I'm going to try to lay out very clearly how I got this working.
Just to set expectations, this is not a tutorial about how to use the cryptography APIs themselves in javax.crypto (look at the JCE tutorials from Sun for this); nor is this a primer about how public key cryptography works. This article is really about how to manage the keys with off-the-shelf utilities available to your friendly, neighborhood sysadmin and still make use of them from Java programs. Really, this boils down to "how do I get these darn keys loaded into a Java program where they can be used?" This is the article I wish I had when I started trying to muck around with this stuff....
Managing the keys
Openssl. This is the de-facto tool sysadmins use for managing public/private keys, X.509 certificates, etc. This is what we want to create/manage our keys with, so that they can be stored in formats that are common across most Un*x systems and utilities (like, say, C programs using the openssl library...). Java has this notion of its own keystore, and Sun will give you the keytool command with Java, but that doesn't do you much good outside of Java world.
Creating the keypair. We are going to create a keypair, saving it in openssl's preferred PEM format. PEM formats are ASCII and hence easy to email around as needed. However, we will need to save the keys in the binary DER format so Java can read them. Without further ado, here is the magical incantation for creating the keys we'll use:
# generate a 2048-bit RSA private key $ openssl genrsa -out private_key.pem 2048 # convert private Key to PKCS#8 format (so Java can read it) $ openssl pkcs8 -topk8 -inform PEM -outform DER -in private_key.pem \ -out private_key.der -nocrypt # output public key portion in DER format (so Java can read it) $ openssl rsa -in private_key.pem -pubout -outform DER -out public_key.der
You keep private_key.pem around for reference, but you hand the DER versions to your Java programs.
Loading the keys into Java
Really, this boils down to knowing what type of KeySpec to use when reading in the keys. To read in the private key:
import java.io.*; import java.security.*; import java.security.spec.*; public class PrivateKeyReader { public static PrivateKey get(String filename) throws Exception { File f = new File(filename); FileInputStream fis = new FileInputStream(f); DataInputStream dis = new DataInputStream(fis); byte[] keyBytes = new byte[(int)f.length()]; dis.readFully(keyBytes); dis.close(); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePrivate(spec); } }
And now, to read in the public key:
import java.io.*; import java.security.*; import java.security.spec.*; public class PublicKeyReader { public static PublicKey get(String filename) throws Exception { File f = new File(filename); FileInputStream fis = new FileInputStream(f); DataInputStream dis = new DataInputStream(fis); byte[] keyBytes = new byte[(int)f.length()]; dis.readFully(keyBytes); dis.close(); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePublic(spec); } }
That's about it. The hard part was figuring out a compatible set of:
- openssl DER output options (particularly the PKCS#8 encoding)
- which type of KeySpec Java needed to use (strangely enough, the public key needs the "X509" keyspec, even though you would normally handle X.509 certificates with the openssl x509 command, not the openssl rsa command. Real intuitive.)
From here, signing and verifying work as described in the JCE documentation; the only other thing you need to know is that you can use the "SHA1withRSA" algorithm when you get your java.security.Signature instance for signing/verifying, and that you want the "RSA" algorithm when you get your javax.crypto.Cipher instance for encrypting/decrypting.
Many happy security returns to you.
相关推荐
cmd-bat-批处理-脚本-rimraf.zip
该资源文件详细介绍了如何在ADS软件中进行混频器仿真,内容涵盖了仿真设置、参数调整、结果分析等多个方面。
cmd-bat-批处理-脚本-Messagebox_Cancel_TryAgain_Continue.zip
内容概要:本文详细介绍了如何使用C#客户端实现OpcUa通讯,具体包括远程连接KEPserverEX服务器的方法及其应用。文中不仅提供了详细的环境准备工作指导,还给出了具体的代码示例用于创建会话和连接服务器,最后通过UaExpert测试工具验证了整个流程的有效性。这是一套亲测稳定高效的解决方案,适用于工业自动化领域的数据传输和控制。 适合人群:从事工业自动化领域的研发人员和技术爱好者,尤其是那些有C#编程经验并对OpcUa通讯感兴趣的开发者。 使用场景及目标:①帮助开发者掌握C#客户端连接KEPserverEX服务器的具体方法;②提供实用的代码示例和测试工具使用指南,便于快速上手;③确保数据传输的安全性和稳定性,提高工作效率。 其他说明:文章强调了环境准备的重要性,包括安装必要的开发工具和库,同时也指出了可能遇到的问题及解决办法。此外,还提到了UaExpert作为一款优秀的测试工具,在验证连接和数据交互方面的优势。
人工智能中的自动逻辑推理.zip
cmd-bat-批处理-脚本-os2_cid_install.zip
在武汉大学大地测量学基础的编程作业中,我已完成相关任务。通过使用教材中的算例进行验证,程序运行结果的误差极小,这充分证明了程序的准确性和可靠性。在公式推导过程中,我严格遵循步骤,确保每一步都清晰严谨,从而保证了最终结果的高精度。作业中还提供了灵活的选择,用户可以根据自己的需求选择正算或反算操作,并且可以手动输入任意坐标,方便进行多样化的计算和验证。
警 院指挥教学数字训练系统设计——公 安院校电子沙盘教学系统设计.zip
cmd-bat-批处理-脚本-un-java.zip
cmd-bat-批处理-脚本-SetForegroundWindow.zip
内容概要:本文详细介绍了GSP软件在航空发动机和燃气轮机模型设计中的应用。首先,讨论了如何利用GSP软件构建不同类型的航空发动机(如涡扇、涡喷、涡轴)和燃气轮机模型,涵盖了从进气道到涡轮各组成部分的建模。接着,阐述了稳态特性的循环计算方法,包括空气、燃油、燃气等组分的变换及其能量转换和物质交换的计算。最后,探讨了瞬态性能计算和部件性能衰退降级仿真,通过时间序列分析和动态仿真模块,模拟发动机在不同工况下的瞬态响应和故障数据,从而全面评估发动机的性能。 适合人群:从事航空发动机和燃气轮机研究的设计工程师、研究人员和技术专家。 使用场景及目标:适用于需要深入了解航空发动机和燃气轮机构造、稳态和瞬态性能的研究和开发项目。目标是提高对发动机工作原理的理解,优化设计流程,提升性能预测精度。 其他说明:文中提到的技术手段不仅限于理论分析,还包括实际操作层面的代码实现,帮助读者掌握具体的建模和仿真技巧。
cmd-bat-批处理-脚本-installtrusted.zip
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
威布尔杂波下扩展目标检测.zip
remotesensing-13-04370-v2.pdf
二十四节气之霜降介绍.pptx
2025年网络安全技术漏洞扫描.docx
cmd-bat-批处理-脚本-virtualcam-uninstall.zip
不完备决策表的相对约简算法.pdf
chat-box1.12.3 版本windows专用