当前位置: 首页 > 工具软件 > gmssl-python > 使用案例 >

sm2 加解密 python(gmssl) + vue(sm-crypto) + java(hutool)

华宏逸
2023-12-01

sm2 python(gmssl) + vue(sm-crypto) + java(hutool) 互相加解密

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

from gmssl import sm2

private_key = 'd9d37f4f46e8514c6f9398a984e74f3eead994e8f4ac5f92e5deb313cb5ad6a6'
public_key = 'e332ee43ac37be458550652fb9de9d58faf4bea2567534fda3319212a55b0732f5a9b7304b3a0127355ef98419b3a3598d0108611d658839e5d603abe01683ea'
sm2_crypt = sm2.CryptSM2(public_key=public_key, private_key=private_key)
UTF8 = 'utf8'


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)

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)));
    }
}
 类似资料: