SM2只有私钥情况下解密

本文档介绍了如何在Android应用中实现SM2国密算法的解密,基于两个开源代码库进行整合,重点讲解了SM2解密函数的使用和自定义类SM2的实现过程。

结合了两个大佬的代码,目前应该都是第一个大佬的代码

第一个Android_国密_SM2、SM3、SM4_ylineyline的专栏-优快云博客

第二个Android常用加密库集合_lambert_Lei的专栏-优快云博客

首先导入包:Android_国密_SM2、SM3、SM4_ylineyline的专栏-优快云博客

implementation 'org.bouncycastle:bcprov-jdk16:1.46'

接下来,都是自定义的东西不要找SM2的包在哪,SM2是自己的类

package com.iqilu.phonetoken.utils;

import android.text.TextUtils;
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECFieldElement;
import org.bouncycastle.math.ec.ECFieldElement.Fp;
import org.bouncycastle.math.ec.ECPoint;

import java.math.BigInteger;
import java.security.SecureRandom;


public class SM2 {

    // 正式参数
    public static final String[] ecc_param = {
            "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF",
            "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC",
            "28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93",
            "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123",
            "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7",
            "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0"
    };

    public final BigInteger ecc_p;
    public final BigInteger ecc_a;
    public final BigInteger ecc_b;
    public final BigInteger ecc_n;
    public final BigInteger ecc_gx;
    public final BigInteger ecc_gy;

    public final ECCurve ecc_curve;
    public final ECPoint ecc_point_g;

    public final ECDomainParameters ecc_bc_spec;
    public final ECKeyPairGenerator ecc_key_pair_generator;

    public final ECFieldElement ecc_gx_fieldelement;
    public final ECFieldElement ecc_gy_fieldelement;

    public SM2() {
        this.ecc_p = new BigInteger(ecc_param[0], 16);
        this.ecc_a = new BigInteger(ecc_param[1], 16);
        this.ecc_b = new BigInteger(ecc_param[2], 16);
        this.ecc_n = new BigInteger(ecc_param[3], 16);
        this.ecc_gx = new BigInteger(ecc_param[4], 16);
        this.ecc_gy = new BigInteger(ecc_param[5], 16);

        this.ecc_gx_fieldelement = new Fp(this.ecc_p, this.ecc_gx);
        this.ecc_gy_fieldelement = new Fp(this.ecc_p, this.ecc_gy);

        this.ecc_curve = new ECCurve.Fp(this.ecc_p, this.ecc_a, this.ecc_b);
        this.ecc_point_g = new ECPoint.Fp(this.ecc_curve, this.ecc_gx_fieldelement, this.ecc_gy_fieldelement);

        this.ecc_bc_spec = new ECDomainParameters(this.ecc_curve, this.ecc_point_g, this.ecc_n);

        this.ecc_key_pair_generator = new ECKeyPairGenerator();
        this.ecc_key_pair_generator.init(new ECKeyGenerationParameters(this.ecc_bc_spec, new SecureRandom()));
    }
    

    /**
     * 解密
     * @param value 需要解密数据(默认传入为16进制数据)
     * @param key 密钥(需要16进制字符串,其他编码请使用byte[])
     * @return
     */
    public String simpleDeCode(String value, String key) {
        if(TextUtils.isEmpty(value)|| TextUtils.isEmpty(key)){
            return null;
        }
        byte[] datas=Utils.hexStringToBytes(value);
        byte[] keyBytes=Utils.hexStringToBytes(key);
        return new String(deCode(datas,keyBytes));
    }

    public byte[] deCode(byte[] value, byte[] key) {
        if (value==null||key==null)
        {
            return null;
        }
        //加密字节数组转换为十六进制的字符串 长度变为value.length * 2
        String data = Utils.byteToHex(value);
        /***分解加密字串
         * (C1 = C1标志位2位 + C1实体部分128位 = 130)
         * (C2 = encryptedData.length * 2 - C1长度  - C2长度)
         * (C3 = C3实体部分64位  = 64)
         */
        byte[] c1Bytes = Utils.hexToByte(data.substring(0,130));
        int c2Len = value.length - 97;
        byte[] c2 = Utils.hexToByte(data.substring(130,130 + 2 * c2Len));
        byte[] c3 = Utils.hexToByte(data.substring(130 + 2 * c2Len,194 + 2 * c2Len));

        BigInteger userD = new BigInteger(1, key);

        //通过C1实体字节来生成ECPoint
        ECPoint c1 =ecc_curve.decodePoint(c1Bytes);
        Cipher cipher = new Cipher();
        cipher.Init_dec(userD, c1);
        cipher.Decrypt(c2);
        cipher.Dofinal(c3);
        //返回解密结果
        return c2;
    }


}

简单就是直接解密

参考第二篇文章直接用就行

M2 sm2 = new SM2();
String s = sm2.simpleDeCode(message, getPrivate_secret());

OpenSSL 1.1.1 并未原生支持中国的国家密码标准(GM/T)中的 SM2 算法。如果需要使用 SM2 算法,则需借助 GmSSL 工具包,这是一个基于 OpenSSL 的开源工具包,提供了对中国国家标准加密算法的支持[^4]。 以下是通过 GmSSL 实现 SM2 公钥加密和私钥解密的一个示例: ### 使用 GmSSL 实现 SM2 加密与解密 #### 1. 安装 GmSSL 首先安装 GmSSL 工具包。可以通过源码编译的方式完成安装: ```bash git clone https://github.com/guanzhi/GmSSL.git cd GmSSL ./config make sudo make install ``` #### 2. 创建 SM2 密钥对 生成 SM2 私钥和对应的公钥证书: ```bash gmssl ecparam -name sm2p256v1 -genkey -out sm2_private_key.pem gmssl ec -in sm2_private_key.pem -pubout -out sm2_public_key.pem ``` #### 3. 编写加密程序 下面是一个简单的 Python 脚本,用于演示如何利用 GmSSL 执行 SM2 公钥加密操作: ```python import subprocess def encrypt_with_sm2(public_key_path, plaintext): process = subprocess.Popen( ['gmssl', 'sm2keyutl', '-encrypt', '-inkey', public_key_path], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) output, error = process.communicate(input=plaintext.encode('utf-8')) if process.returncode != 0: raise Exception(f"Encryption failed: {error.decode()}") return output.hex() public_key_path = "sm2_public_key.pem" plaintext = "This is a secret message." ciphertext = encrypt_with_sm2(public_key_path, plaintext) print(f"Cipher Text: {ciphertext}") ``` #### 4. 解密程序 下面是相应的解密脚本,展示如何使用 SM2 私钥进行解密: ```python import subprocess def decrypt_with_sm2(private_key_path, ciphertext_hex): process = subprocess.Popen( ['gmssl', 'sm2keyutl', '-decrypt', '-inkey', private_key_path], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) output, error = process.communicate(input=bytes.fromhex(ciphertext_hex)) if process.returncode != 0: raise Exception(f"Decryption failed: {error.decode()}") return output.decode('utf-8') private_key_path = "sm2_private_key.pem" decrypted_text = decrypt_with_sm2(private_key_path, ciphertext) print(f"Decrypted Text: {decrypted_text}") ``` 以上代码展示了如何调用 `gmssl` 命令行工具来执行 SM2 的加解密过程。需要注意的是,在实际应用中可以进一步封装成库函数以便更高效地集成到应用程序中[^4]。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn_zxw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值