Skip to content

Java hutool 解密demo #11

@winters1214

Description

@winters1214
// 密钥对生成地址 https://www.lzltool.com/SM2
private static final String PRIVATE_KEY = "";

private static final String PUBLIC_KEY = "";

public static String decryptStr(String cipherTextHex) {
    try {
        SM2 sm2 = SmUtil.sm2(PRIVATE_KEY, null); // 只需要私钥
        sm2.setMode(SM2Engine.Mode.C1C2C3); // 设置解密模式,JS 加密model 选:0
        byte[] decryptedBytes = sm2.decrypt(HexUtil.decodeHex(cipherTextHex), KeyType.PrivateKey);
        return Base64.decodeStr(StrUtil.utf8Str(decryptedBytes)); // 转换为字符串
    }catch (Exception e){
        log.error("decryptStr error: ", e);
    }
    return null;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions