国密sm2的Vue、Python、Java互通使用

编程入门 行业动态 更新时间:2024-10-09 07:27:05

<a href=https://www.elefans.com/category/jswz/34/1665282.html style=国密sm2的Vue、Python、Java互通使用"/>

国密sm2的Vue、Python、Java互通使用

目录

一、Vue

二、Python 

三、Java


 

一、Vue

# npm install --save sm-cryptoimport {sm2} from 'sm-crypto'
const cipherMode = 1
const private_key = 'd9d37f4f46e8514c6f9398a984e74f3eead994e8f4ac5f92e5deb313cb5ad6a6'
const public_key = '04' + 'e332ee43ac37be458550652fb9de9d58faf4bea2567534fda3319212a55b0732f5a9b7304b3a0127355ef98419b3a3598d0108611d658839e5d603abe01683ea'
let en_data = sm2.doEncrypt('123', public_key, cipherMode)
console.log(en_data)
let de_data = sm2.doDecrypt('6e10e194a2373d7d30a8f79d944fef516f2644076f7889560c5849c57b7c18f624a2e2d6c088459396aa9dbba71dd4fe242faa6a94cfb9b62ecbac537e894c3df67b62931ad511b050043e897719e332f708c24b9e137d3a87aebffc6ba4430e300d9a', private_key, cipherMode);
console.log(de_data)

二、Python 

pip install gmssl
from gmssl import sm2# 16进制的公钥和私钥
private_key = '3037723d47292171677ec8bd7dc9af696c7472bc5f251b2cec07e65fdef22e25'
public_key = '04298364ec840088475eae92a591e01284d1abefcda348b47eb324bb521bb03b0b2a5bc393f6b71dabb8f15c99a0050818b56b23f31743b93df9cf8948f15ddb54'
cipherMode = 1
sm2_crypt = sm2.CryptSM2(public_key=public_key, private_key=private_key, mode=cipherMode)UTF8 = 'utf-8'def encrypt(data: str) -> str:# sm2 加密return sm2_crypt.encrypt(data.encode(UTF8)).hex()def decrypt(data: str) -> str:# 解密return sm2_crypt.decrypt(bytes.fromhex(data)).decode(UTF8)if __name__ == '__main__':# 解密s = decrypt('c4eb975c40ea2f8dcdb78ba8900c5e617fb1186f9100fa5f8cbf2b59c40105a1f455018d174c99b65d9f3e439bc12be1aef884bf445d20d63de795bbb8b962b95c91f6fe7826c3d11387838741d319c4c8f227038e11ffb8f6f10ad52be0e02b516c6af45a4b')print(s)

三、Java

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.BCUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;import java.io.FileInputStream;
import java.io.FileOutputStream;/*** @author Created by ${USER} on ${YEAR}-${MONTH}-${DAY} ${TIME}:${SECOND}*/
public class Main {static String pri = "d9d37f4f46e8514c6f9398a984e74f3eead994e8f4ac5f92e5deb313cb5ad6a6";static String pub = "e332ee43ac37be458550652fb9de9d58faf4bea2567534fda3319212a55b0732f5a9b7304b3a0127355ef98419b3a3598d0108611d658839e5d603abe01683ea";static SM2 sm2 = new SM2(BCUtil.toSm2Params(pri), BCUtil.toSm2Params(pub.substring(0, 64), pub.substring(64, 128)));public static void main(String[] args) {if (args.length == 0) {System.out.println("./gm get\n" +"./gm [en|de] [src] [dst]");System.exit(0);}if (args.length == 3) {if (!args[0].matches("en|de")) {System.out.println("mode err");System.exit(1);}en_de_f(args[1], args[2], args[0]);} else {if ("get".equals(args[0])) {System.out.println(getKeyPair());}}}public static void en_de_f(String src, String dst, String mode) {try (FileInputStream fileInputStream = new FileInputStream(src);FileOutputStream fos = new FileOutputStream(dst)) {if ("en".equals(mode)) {fos.write(sm2.encrypt(fileInputStream, KeyType.PublicKey));} else {fos.write(sm2.decrypt(fileInputStream, KeyType.PrivateKey));}} catch (Exception e) {e.printStackTrace();}}private static String getKeyPair() {SM2 sm2 = new SM2();return String.format("pri: %s\npub: %s", sm2.getDHex(), HexUtil.encodeHexStr(sm2.getQ(false)));}
}

更多推荐

国密sm2的Vue、Python、Java互通使用

本文发布于:2023-12-07 09:04:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1670772.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:国密   Vue   Java   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!