「java私钥存储」java 公私钥加密和解密
今天给各位分享java私钥存储的知识,其中也会对java 公私钥加密和解密进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
在java中生成密钥对后如何保存
1、 用Vector保存并序列化存储到文件
2、 键值对的形式写入property文件
3、 存数据库
Java RSA 加密解密中 密钥保存并读取,数据加密解密并保存读取 问题
帮你完善了下代码。
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Map;
public class Test {
static String publicKey;
static String privateKey;
public Test() throws Exception {
// TODO Auto-generated constructor stub
MapString, Object keyMap = RSAUtils.genKeyPair();
publicKey = RSAUtils.getPublicKey(keyMap);
privateKey = RSAUtils.getPrivateKey(keyMap);
// 保存密钥,名字分别为publicKey。txt 和privateKey。txt;
PrintWriter pw1 = new PrintWriter(new FileOutputStream(
"D:/publicKey.txt"));
PrintWriter pw2 = new PrintWriter(new FileOutputStream(
"D:/privateKey.txt"));
pw1.print(publicKey);
pw2.print(privateKey);
pw1.close();
pw2.close();
// 从保存的目录读取刚才的保存的公钥,
String pubkey = readFile("D:/publicKey.txt");// 读取的公钥内容;
String data = readFile("D:/1.txt"); // 需要公钥加密的文件的内容(如D:/1.txt)
byte[] encByPubKeyData = RSAUtils.encryptByPublicKey(data.getBytes(),
pubkey);
//将加密数据base64后写入文件
writeFile("D:/Encfile.txt", Base64Utils.encode(encByPubKeyData).getBytes("UTF-8"));
// 加密后的文件保存在
String prikey = readFile("D:/privateKey.txt");// 从保存的目录读取刚才的保存的私钥,
String Encdata = readFile("D:/Encfile.txt");// 刚才加密的文件的内容;
byte[] encData = Base64Utils.decode(Encdata);
byte[] decByPriKeyData = RSAUtils.decryptByPrivateKey(encData, prikey);
// 解密后后的文件保存在D:/Decfile.txt
writeFile("D:/Decfile.txt", decByPriKeyData);
}
private static String readFile(String filePath) throws Exception {
File inFile = new File(filePath);
long fileLen = inFile.length();
Reader reader = new FileReader(inFile);
char[] content = new char[(int) fileLen];
reader.read(content);
System.out.println("读取到的内容为:" + new String(content));
return new String(content);
}
private static void writeFile(String filePath, byte[] content)
throws Exception {
System.out.println("待写入文件的内容为:" + new String(content));
File outFile = new File(filePath);
OutputStream out = new FileOutputStream(outFile);
out.write(content);
if (out != null) out.close();
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
new Test();
}
}
测试结果:
读取到的内容为:MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDVr9D9hYPD4kS5k86dRm+utyt5XGOSTPwT0YeoBnQmgeISkNsbtUFkY6txyodVl26IM1H5iwJ1jMQ63+lXfZxzNpeA+rHaxmeQ2qI+5ES9AF7G6KIwjzakKsA08Ly+1y3dp0BnoyHF7/Pj3AS28fDmE5piea7w36vp4E3Ts+F9vwIDAQAB
读取到的内容为:锘县ahaha
java rsa私钥加密
java rsa私钥加密是什么?让我们一起来了解一下吧!
java rsa私钥加密是一种加密算法。私钥加密算法是用私钥来进行加密与解密信息。私钥加密也被称作对称加密,原因是加密与解密使用的秘钥是同一个。
RSA加密需要注意的事项如下:
1. 首先产生公钥与私钥
2. 设计加密与解密的算法
3. 私钥加密的数据信息只能由公钥可以解密
4. 公钥加密的数据信息只能由私钥可以解密
实战演练,具体步骤如下: public class RsaCryptTools { private static final String CHARSET = "utf-8"; private static final Base64.Decoder decoder64 = Base64.getDecoder(); private static final Base64.Encoder encoder64 = Base64.getEncoder(); /** * 生成公私钥 * @param keySize * @return * @throws NoSuchAlgorithmException */ public static SecretKey generateSecretKey(int keySize) throws NoSuchAlgorithmException { //生成密钥对 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(keySize, new SecureRandom()); KeyPair pair = keyGen.generateKeyPair(); PrivateKey privateKey = pair.getPrivate(); PublicKey publicKey = pair.getPublic(); //这里可以将密钥对保存到本地 return new SecretKey(encoder64.encodeToString(publicKey.getEncoded()), encoder64.encodeToString(privateKey.getEncoded())); } /** * 私钥加密 * @param data * @param privateInfoStr * @return * @throws IOException * @throws InvalidCipherTextException */ public static String encryptData(String data, String privateInfoStr) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, getPrivateKey(privateInfoStr)); return encoder64.encodeToString(cipher.doFinal(data.getBytes(CHARSET))); } /** * 公钥解密 * @param data * @param publicInfoStr * @return */ public static String decryptData(String data, String publicInfoStr) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException { byte[] encryptDataBytes=decoder64.decode(data.getBytes(CHARSET)); //解密 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, getPublicKey(publicInfoStr)); return new String(cipher.doFinal(encryptDataBytes), CHARSET); } private static PublicKey getPublicKey(String base64PublicKey) throws NoSuchAlgorithmException, InvalidKeySpecException { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(base64PublicKey.getBytes())); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePublic(keySpec); } private static PrivateKey getPrivateKey(String base64PrivateKey) throws NoSuchAlgorithmException, InvalidKeySpecException { PrivateKey privateKey = null; PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(base64PrivateKey.getBytes())); KeyFactory keyFactory = null; keyFactory = KeyFactory.getInstance("RSA"); privateKey = keyFactory.generatePrivate(keySpec); return privateKey; } /** * 密钥实体 * @author hank * @since 2020/2/28 0028 下午 16:27 */ public static class SecretKey { /** * 公钥 */ private String publicKey; /** * 私钥 */ private String privateKey; public SecretKey(String publicKey, String privateKey) { this.publicKey = publicKey; this.privateKey = privateKey; } public String getPublicKey() { return publicKey; } public void setPublicKey(String publicKey) { this.publicKey = publicKey; } public String getPrivateKey() { return privateKey; } public void setPrivateKey(String privateKey) { this.privateKey = privateKey; } @Override public String toString() { return "SecretKey{" + "publicKey='" + publicKey + '\'' + ", privateKey='" + privateKey + '\'' + '}'; } } private static void writeToFile(String path, byte[] key) throws IOException { File f = new File(path); f.getParentFile().mkdirs(); try(FileOutputStream fos = new FileOutputStream(f)) { fos.write(key); fos.flush(); } } public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, IOException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidKeySpecException { SecretKey secretKey = generateSecretKey(2048); System.out.println(secretKey); String enStr = encryptData("你好测试测试", secretKey.getPrivateKey()); System.out.println(enStr); String deStr = decryptData(enStr, secretKey.getPublicKey()); System.out.println(deStr); enStr = encryptData("你好测试测试hello", secretKey.getPrivateKey()); System.out.println(enStr); deStr = decryptData(enStr, secretKey.getPublicKey()); System.out.println(deStr); } }
java私钥存储的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 公私钥加密和解密、java私钥存储的信息别忘了在本站进行查找喔。