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


    目录

    一、Vue

    二、Python 

    三、Java


     

    一、Vue

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

    二、Python 

    pip install gmssl
    1. from gmssl import sm2
    2. # 16进制的公钥和私钥
    3. private_key = '3037723d47292171677ec8bd7dc9af696c7472bc5f251b2cec07e65fdef22e25'
    4. public_key = '04298364ec840088475eae92a591e01284d1abefcda348b47eb324bb521bb03b0b2a5bc393f6b71dabb8f15c99a0050818b56b23f31743b93df9cf8948f15ddb54'
    5. cipherMode = 1
    6. sm2_crypt = sm2.CryptSM2(public_key=public_key, private_key=private_key, mode=cipherMode)
    7. UTF8 = 'utf-8'
    8. def encrypt(data: str) -> str:
    9. # sm2 加密
    10. return sm2_crypt.encrypt(data.encode(UTF8)).hex()
    11. def decrypt(data: str) -> str:
    12. # 解密
    13. return sm2_crypt.decrypt(bytes.fromhex(data)).decode(UTF8)
    14. if __name__ == '__main__':
    15. # 解密
    16. s = decrypt(
    17. 'c4eb975c40ea2f8dcdb78ba8900c5e617fb1186f9100fa5f8cbf2b59c40105a1f455018d174c99b65d9f3e439bc12be1aef884bf445d20d63de795bbb8b962b95c91f6fe7826c3d11387838741d319c4c8f227038e11ffb8f6f10ad52be0e02b516c6af45a4b')
    18. print(s)

    三、Java

    1. import cn.hutool.core.util.HexUtil;
    2. import cn.hutool.crypto.BCUtil;
    3. import cn.hutool.crypto.asymmetric.KeyType;
    4. import cn.hutool.crypto.asymmetric.SM2;
    5. import java.io.FileInputStream;
    6. import java.io.FileOutputStream;
    7. /**
    8. * @author Created by ${USER} on ${YEAR}-${MONTH}-${DAY} ${TIME}:${SECOND}
    9. */
    10. public class Main {
    11. static String pri = "d9d37f4f46e8514c6f9398a984e74f3eead994e8f4ac5f92e5deb313cb5ad6a6";
    12. static String pub = "e332ee43ac37be458550652fb9de9d58faf4bea2567534fda3319212a55b0732f5a9b7304b3a0127355ef98419b3a3598d0108611d658839e5d603abe01683ea";
    13. static SM2 sm2 = new SM2(BCUtil.toSm2Params(pri), BCUtil.toSm2Params(pub.substring(0, 64), pub.substring(64, 128)));
    14. public static void main(String[] args) {
    15. if (args.length == 0) {
    16. System.out.println("./gm get\n" +
    17. "./gm [en|de] [src] [dst]");
    18. System.exit(0);
    19. }
    20. if (args.length == 3) {
    21. if (!args[0].matches("en|de")) {
    22. System.out.println("mode err");
    23. System.exit(1);
    24. }
    25. en_de_f(args[1], args[2], args[0]);
    26. } else {
    27. if ("get".equals(args[0])) {
    28. System.out.println(getKeyPair());
    29. }
    30. }
    31. }
    32. public static void en_de_f(String src, String dst, String mode) {
    33. try (FileInputStream fileInputStream = new FileInputStream(src);
    34. FileOutputStream fos = new FileOutputStream(dst)) {
    35. if ("en".equals(mode)) {
    36. fos.write(sm2.encrypt(fileInputStream, KeyType.PublicKey));
    37. } else {
    38. fos.write(sm2.decrypt(fileInputStream, KeyType.PrivateKey));
    39. }
    40. } catch (Exception e) {
    41. e.printStackTrace();
    42. }
    43. }
    44. private static String getKeyPair() {
    45. SM2 sm2 = new SM2();
    46. return String.format("pri: %s\npub: %s", sm2.getDHex(), HexUtil.encodeHexStr(sm2.getQ(false)));
    47. }
    48. }

  • 相关阅读:
    智能化改造给企业带来的实际效果
    嵌入式开发:在嵌入式设备上实现出色UI体验的挑战。
    数据结构与算法之链表-python实现(简单好用)
    打印nXn方阵的上三角阵
    【WSN通信】基于Matlab模拟(AODV)路由协议
    虚拟机winserver2019安装搭建
    负载均衡 - F5
    【Git】有人瞎动你代码怎么办?学会git reset、 revert轻松解决
    Vim下更新coc-nvim
    目标检测:Generalized Focal Loss(2020)
  • 原文地址:https://blog.csdn.net/wenxingchen/article/details/133928043