1. 公钥加密
1.1. 公钥加密说明
1.1.1. 公钥加密的定义
hengshi 系统公钥加密,主要用于 hengshi 用户密码的加密。
结构说明
1.2. 接口说明
1.2.1. 获取公钥
获取公钥。
请求URL
POST '/api/v1/rsa'
请求参数
无
URL 参数
无
request body 参数
无
返回对象的格式说明
字段 | 类型 | 说明 |
---|---|---|
uuid | STRING | uuid |
publicKey | STRING | 公钥 |
接口示例1: 获取公钥
POST /api/v1/rsa
返回:
{
"version":"3.0-SNAPSHOT@026a150",
"code":0,
"msg":"success",
"data":{
"uuid":"315b33bc-24f8-4e01-a92b-e57a250453db",
"publicKey":"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMIZ/S3Gu4V02uE2mOgZyRzfe8njecXBn3YSwhyVFmKLpF6pJIBBthsGOI+I45DlZpMaEbympaNS6UHTIULnZUcCAwEAAQ=="
}
}
接口示例2: 使用公钥加密
linux shell 示例
encrypt_password_v2() {
local pubkey="$1"
local passwd="$2"
echo -n "$passwd" | openssl rsautl -encrypt -inkey <(echo -e "-----BEGIN PUBLIC KEY-----\n$(echo $pubkey | split -b 64 --filter "cat; echo ''")\n-----END PUBLIC KEY-----") -pubin | base64 | tr -d '\n'
}
encrypt_password_v2 {PUBLIC_KEY} {PASSWORD}
java 示例
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public static String encryptPassword(String publicKeyStr, String password) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
byte[] bytes = Base64.getDecoder().decode(publicKeyStr);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(bytes);
PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
String encryptedPassword = Base64.getEncoder().encodeToString(cipher.doFinal(password.getBytes()));
return encryptedPassword;
}
javascript 实例
import { JSEncrypt } from 'jsencrypt';
function encryptPassword(publicKey, password) {
const encrypt = new JSEncrypt();
encrypt.setPublicKey(publicKey);
const ep = encrypt.encrypt(password);
return ep
}