GithubHelp home page GithubHelp logo

sm2's Introduction

SM2 国密前端加密代码

经过测试,同一密钥对同一数据进行 50万 次加密,后端解密一切正常

依赖资源

  • crypto-js

使用方法

加密:

const msg = 'hello world'
const pubkeyHex = '0452712EBA7FE2C9615F6DE59C6EF662R085BD52B25952597CC95014BB8F201987F8D818EFFE710DBEC08FE2E4C7E3E0113EEBAB4B0E8B044E1A3CC8B149D76BE7';
const cipherMode = 0;

// 获取加密过后的密文
const result = sm2Encrypt(msg, pubkeyHex, cipherMode);
参数 参数类型 说明
msg String 加密的文本
pubkeyHex String 公钥 hex
cipherMode String 0: C1C2C3、1: C1C3C2

sm2's People

Contributors

saberization avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sm2's Issues

Js前台加密,Java后台解密(Hutool)

 
    // log是Lombok的@Slf4j注解
    //
    // HuTool,生成公钥和私钥(需要引入maven依赖):

    KeyPair pair = SecureUtil.generateKeyPair("SM2");

    byte[] privateKeyBytes = pair.getPrivate().getEncoded();
    byte[] publicKeyBytes = pair.getPublic().getEncoded();

    String privateKeyBase64 = Base64.getEncoder().encodeToString(privateKeyBytes);
    String publicKeyBase64 = Base64.getEncoder().encodeToString(publicKeyBytes);

    System.out.println("私钥(Base64): " + privateKeyBase64);
    System.out.println("公钥(Base64): " + publicKeyBase64);

    SM2 sm2 = SmUtil.sm2(privateKeyBytes, publicKeyBytes);
    // 公钥加密,私钥解密
    String encryptStr = sm2.encryptBase64("你好", KeyType.PublicKey);
    System.out.println(encryptStr);
    String decryptStr = StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));
    System.out.println(decryptStr);



  // 后端解密过程.
  // 把上面生成的公钥和私钥对改成下面的:
  
  private static final String PUBLIC_KEY =
      "公钥";
  private static final String PRIVATE_KEY =
      "私钥";

  public static String decryptStr(String params) {
    try {
      var sm2 = SmUtil.sm2(PRIVATE_KEY, PUBLIC_KEY);
      // 前台cipherMode=1
      sm2.setMode(SM2Engine.Mode.C1C3C2);
      // 把下面这个值的输出返回给前台,这是前台用的pubkeyHex
      // 
      // System.out.println(sm2.getQ(false));
      var decryptResult = StrUtil.utf8Str(sm2.decrypt(params, KeyType.PrivateKey));
      var decryptResult1 = new String(Base64.getDecoder().decode(decryptResult));
      log.info("入参: {},解密后: {}", params, decryptResult1);
      return decryptResult1;
    } catch (Exception exception) {
      log.error("解密失败,入参: {},失败原因: {}", params, exception.getMessage());
      exception.printStackTrace();
    }
    return "";
  }

Error: Calling enrollment endpoint failed with error

Error: Calling enrollment endpoint failed with error [Error: write EPROTO 140222719264576:error:100D7010:elliptic curve routines:ECKEY_PUB_DECODE:EC lib:../deps/openssl/openssl/crypto/ec/ec_ameth.c:202:
140222719264576:error:0B07707D:x509 certificate routines:X509_PUBKEY_get:public key decode error:../deps/openssl/openssl/crypto/asn1/x_pubkey.c:154:
140222719264576:error:140900EF:SSL routines:ssl3_get_server_certificate:unable to find public key parameters:../deps/openssl/openssl/ssl/s3_clnt.c:1313:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.