栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA RSA 加解密 签名 解签 复制使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA RSA 加解密 签名 解签 复制使用

package a.b.c;


import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.base64;
import javax.crypto.Cipher;


public class JieJieMI {
	public static String data = "RSA-java-代码123";
	// 公钥
	private static String publicKeyStr = "";

	// 私钥
	private static String privateKeyStr = "";

	private static String qianmin = "SHA1withRSA";

	private static String RSA = "RSA";

	public static String getString(byte[] bytes) {
		return base64.getEncoder().encodeToString(bytes);
	}

	public static byte[] getByte(String s) {
		return base64.getDecoder().decode(s);
	}

	// 生成密钥对
	private static void into() throws NoSuchAlgorithmException {
		KeyPairGenerator keyPairGenerator;
		keyPairGenerator = KeyPairGenerator.getInstance(RSA);
		keyPairGenerator.initialize(1024);
		KeyPair keyPair = keyPairGenerator.generateKeyPair();
		// 获取公钥,并以base64格式打印出来
		PublicKey publicKey = keyPair.getPublic();
		publicKeyStr = getString(publicKey.getEncoded());
		// 获取私钥,并以base64格式打印出来
		PrivateKey privateKey = keyPair.getPrivate();
		privateKeyStr = getString(privateKey.getEncoded());
	}

	// 将base64编码后的公钥字符串转成PublicKey实例
	private static PublicKey getPublicKey(String publicKey) throws Exception {
		byte[] keyBytes = getByte(publicKey);
		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(RSA);
		return keyFactory.generatePublic(keySpec);
	}

	// 将base64编码后的私钥字符串转成PrivateKey实例
	private static PrivateKey getPrivateKey(String privateKey) throws Exception {
		byte[] keyBytes = getByte(privateKey);
		PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(RSA);
		return keyFactory.generatePrivate(keySpec);
	}

	// 公钥加密
	public static String encryptByPublicKey(String content) throws Exception {
		// 获取公钥
		PublicKey publicKey = getPublicKey(publicKeyStr);
		Cipher cipher = Cipher.getInstance(RSA);
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
		byte[] cipherText = cipher.doFinal(content.getBytes());
		return getString(cipherText);
	}

	// 私钥加密
	public static String encryptByPrivateKey(String content) throws Exception {
		// 获取私钥
		PrivateKey privateKey = getPrivateKey(privateKeyStr);
		Cipher cipher = Cipher.getInstance(RSA);
		cipher.init(Cipher.ENCRYPT_MODE, privateKey);
		byte[] cipherText = cipher.doFinal(content.getBytes());
		return getString(cipherText);
	}

	// 私钥解密
	public static String decryptByPrivateKey(String content) throws Exception {
		// 获取私钥
		PrivateKey privateKey = getPrivateKey(privateKeyStr);
		Cipher cipher = Cipher.getInstance(RSA);
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		byte[] cipherText = getByte(content);
		byte[] decryptText = cipher.doFinal(cipherText);
		return new String(decryptText);
	}

	// 公钥解密
	public static String decryptByPublicKey(String content) throws Exception {
		// 获取公钥
		PublicKey publicKey = getPublicKey(publicKeyStr);
		Cipher cipher = Cipher.getInstance(RSA);
		cipher.init(Cipher.DECRYPT_MODE, publicKey);
		byte[] cipherText = getByte(content);
		byte[] decryptText = cipher.doFinal(cipherText);
		return new String(decryptText);
	}


	
	public static String sign(String str) {
		String base64Sign = "";
		try {
			PrivateKey priKey = getPrivateKey(privateKeyStr);
			// 签名
			Signature sign = Signature.getInstance(qianmin);
			sign.initSign(priKey);
			byte[] bysData = str.getBytes("UTF-8");
			sign.update(bysData);
			byte[] signByte = sign.sign();
			base64Sign = getString(signByte);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
		return base64Sign;
	}

	
	public static boolean verify(String signStr, String verStr)
			throws Exception {
		boolean verfy = false;
		try {
			PublicKey pubKey = getPublicKey(publicKeyStr);
			byte[] signed = getByte(signStr);
			Signature sign = Signature.getInstance(qianmin);
			sign.initVerify(pubKey);
			sign.update(verStr.getBytes("UTF-8"));
			verfy = sign.verify(signed);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
		return verfy;
	}

	public static void main(String[] args) throws Exception {
		into();
		
		System.out.println(data);
		// 公钥加密
		String encryptedBytes = encryptByPublicKey(data);
		System.out.println(encryptedBytes);
		// 私钥解密
		String decryptedBytes = decryptByPrivateKey(encryptedBytes);
		System.out.println(decryptedBytes);
		// 私钥加密
		String encryptedBytes2 = encryptByPrivateKey(data);
		System.out.println(encryptedBytes2);
		// 公钥解密
		String decryptedBytes2 = decryptByPublicKey(encryptedBytes2);
		System.out.println(decryptedBytes2);

		//签名
		String s = sign(data);
		System.out.println(s);
		boolean rsa = verify(s, data);
		System.out.println(rsa);
		//解签
	}
}

注意事项:
编码工具要统一
推荐使用base64JDK 自带的

前段

import JSEncrypt from 'jsencrypt/bin/jsencrypt'
const publicKey = ''
// 加密
export function encryptPassword(txt) {
  const encryptor = new JSEncrypt()
  encryptor.setPublicKey(publicKey) // 设置公钥
  return encryptor.encrypt(txt) // 对数据进行加密
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/314486.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号