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

目录

一、Vue

二、Python 

三、Java


一、Vue

# npm install --save sm-crypto

import {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)));
    }
}

猜你喜欢

转载自blog.csdn.net/wenxingchen/article/details/133928043