前端实现vue
<template>
<div>
<h1>RSA 加密 Demo</h1>
<label for="public-key">公钥:</label>
<textarea id="public-key" v-model="publicKey"></textarea>
<label for="plaintext">明文:</label>
<textarea id="plaintext" v-model="plaintext"></textarea>
<button v-on:click="encrypt">加密</button>
<label for="ciphertext">密文:</label>
<textarea id="ciphertext" v-model="ciphertext"></textarea>
<label for="private-key">私钥:</label>
<textarea id="private-key" v-model="privateKey"></textarea>
<button v-on:click="decrypt">解密</button>
<label for="decryptedtext">解密后的明文:</label>
<textarea id="decryptedtext" v-model="decryptedtext"></textarea>
</div>
</template>
<script>
import rsaEncryptor from '../utils/RsaEncryptor.js';
export default {
data() {
return {
publicKey: '',
privateKey: '',
plaintext: '',
ciphertext: '',
decryptedtext: ''
}
},
methods: {
encrypt() {
// 使用公钥加密数据
this.ciphertext = rsaEncryptor.encryptByPublicKey(this.plaintext, this.publicKey);
},
decrypt() {
// 使用私钥解密数据
this.decryptedtext = rsaEncryptor.decryptByPrivateKey(this.ciphertext, this.privateKey);
}
}
}
</script>
后端实现java
package com.ruoyi.auth.utils;
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public class RSAUtils {
// 生成密钥对
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
return keyPairGenerator.generateKeyPair();
}
// 获取公钥字符串
public static String getPublicKeyString(PublicKey publicKey) {
return Base64.getEncoder().encodeToString(publicKey.getEncoded());
}
// 获取私钥字符串
public static String getPrivateKeyString(PrivateKey privateKey) {
return Base64.getEncoder().encodeToString(privateKey.getEncoded());
}
// 使用RSA公钥加密
public static String encryptByPublicKey(String data, String publicKey) throws Exception {
byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
byte[] keyBytes = base64ToBytes(publicKey);
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKeyObj = keyFactory.generatePublic(spec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKeyObj);
byte[] encryptedBytes = cipher.doFinal(dataBytes);
return bytesToBase64(encryptedBytes);
}
// 使用RSA私钥解密
public static String decryptByPrivateKey(String encryptedData, String privateKey) throws Exception {
byte[] encryptedBytes = base64ToBytes(encryptedData);
byte[] keyBytes = base64ToBytes(privateKey);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKeyObj = keyFactory.generatePrivate(spec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKeyObj);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
// 将字节数组转换为Base64字符串
private static String bytesToBase64(byte[] bytes) {
return Base64.getEncoder().encodeToString(bytes);
}
// 将Base64字符串转换为字节数组
private static byte[] base64ToBytes(String base64) {
return Base64.getDecoder().decode(base64);
}
}