【密码学】RSA公私钥的生成
一、liunx命令生成公私钥文件
ssh-keygen -t rsa -b 4096 -f ~/.ssh/my_rsa_key -t rsa 指定密钥类型为 RSA。 -b 4096 指定密钥的位数为 4096 位。这是一个较为安全的选择,但可以根据需要调整大小。 -f ~/.ssh/my_rsa_key 指定密钥文件的存储路径和文件名。 执行上述命令后,你会在 ~/.ssh 目录下得到一个名为 my_rsa_key 的私钥文件和一个同名但后缀为 .pub 的公钥文件 my_rsa_key.pub。
参考1:https://blog.csdn.net/qq_23974323/article/details/77678491
参考2:java生成代码
package com.sxf.study.common.dao.rsa; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.security.Key; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.util.HashMap; import java.util.Map; /** * @Author * @Date 2021/9/27 12:05 下午 **/ public class RsaTest { public static final String KEY_ALGORITHM = "RSA"; // public static final String SIGNATURE_ALGORITHM = "MD5withRSA"; public static final String SIGNATURE_ALGORITHM = "SHA1WithRSA"; private static final String PUBLIC_KEY = "RSAPublicKey"; private static final String PRIVATE_KEY = "RSAPrivateKey"; public static String getPublicKey(Map keyMap) throws Exception { Key key = (Key) keyMap.get(PUBLIC_KEY); byte[] publicKey = key.getEncoded(); return encryptBASE64(key.getEncoded()); } public static String getPrivateKey(Map keyMap) throws Exception { Key key = (Key) keyMap.get(PRIVATE_KEY); byte[] privateKey = key.getEncoded(); return encryptBASE64(key.getEncoded()); } public static byte[] decryptBASE64(String key) throws Exception { return (new BASE64Decoder()).decodeBuffer(key); } public static String encryptBASE64(byte[] key) throws Exception { return (new BASE64Encoder()).encodeBuffer(key); } public static Map initKey() throws Exception { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); System.out.println("algorithm:"+keyPairGen.getAlgorithm()); keyPairGen.initialize(1024); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); Map keyMap = new HashMap(2); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; } public static void main(String[] args) { Map keyMap; try { keyMap = initKey(); String publicKey = getPublicKey(keyMap); System.out.println("公钥=>"+publicKey); String privateKey = getPrivateKey(keyMap); System.out.println("私钥=>"+privateKey); } catch (Exception e) { e.printStackTrace(); } } }
参考3:https://blog.csdn.net/Tombjp/article/details/96477474?utm_term=SHA1WithRSA%E5%8A%A0%E5%AF%86javarsa&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-6-96477474&spm=3001.4430