后端java加解密以及前端js加解密(SM2)【通用版】
准备工作
后端(jar包)、前端(js文件)
阿里云盘:
代码实现
后端
import com.antherd.smcrypto.sm2.Keypair;
import com.antherd.smcrypto.sm2.Sm2;
import io.netty.util.internal.StringUtil;
public class SM2Encryptor {
/**
* 加密,使用公钥
*
* @param publicKey
* @param originalText
* @return
*/
public static String encryptText(String publicKey, String originalText) throws Exception {
if (StringUtil.isNullOrEmpty(publicKey)) {
throw new Exception("密钥不能为空...");
}
if (StringUtil.isNullOrEmpty(originalText)) {
throw new Exception("明文不能为空...");
}
try {
return Sm2.doEncrypt