Giter Club home page Giter Club logo

php-java-sm4's Introduction

概述

2012年3月,国家密码管理局正式公布了包含SM4分组密码算法在内的《祖冲之序列密码算法》等6项密码行业标准。与DES和AES算法类似,SM4算法是一种分组密码算法。其分组长度为128bit,密钥长度也为128bit。加密算法与密钥扩展算法均采用32轮非线性迭代结构,以字(32位)为单位进行加密运算,每一次迭代运算均为一轮变换函数F。SM4算法加/解密算法的结构相同,只是使用轮密钥相反,其中解密轮密钥是加密轮密钥的逆序。

说明

加密前需要客户端先自己生成一个长度为32位的子串作为key(Java生成的 32为 hash值),其中子串不能包含中文。以下SM4加密方式为ECB模式,需要客户端选择应用的加密方式。

目录结构

.
|-- LICENSE
|-- README.md
|-- SM4.php
|-- SM4Example.java
`-- test.php 

参考案例

加密

$key = '35d251411ea04318565f0dbda6ffb6a8';

// 加密内容
$content = [
    'name' => 'Tinywan',
    'School' => 'ZheJiang University',
    'age' => 24,
    'github' => [
        'url' => 'https://github.com/Tinywan',
        'start' => 2000,
    ],
];

// 必须转换为字符串
$content = json_encode($content, JSON_UNESCAPED_UNICODE);
$sm4 = new SM4($key);
$encryptContent = $sm4->encrypt($content);
var_dump($encryptContent);
// 加密内容: b4358f5860343dbf2089ba75ee55deca8d922a069413f39cb3f8b64c01048c780ba5f03290642505d65d79c59684d76cf42443047f547c9f29dc2a49f872a2719ce00539058ab1fb5830e8e0c10144b574a87118390baa765b3429ba7afe5d28

解密

$key = '35d251411ea04318565f0dbda6ffb6a8';

// 加密内容
$encryptContent = 'b4358f5860343dbf2089ba75ee55deca8d922a069413f39cb3f8b64c01048c780ba5f03290642505d65d79c59684d76cf42443047f547c9f29dc2a49f872a2719ce00539058ab1fb5830e8e0c10144b574a87118390baa765b3429ba7afe5d28';

$sm4 = new SM4($key);
$decryptedJsonContent = $sm4->decrypt($encryptContent);
print_r($decryptedJsonContent);

解密结果

{
    "name": "Tinywan",
    "School": "ZheJiang University",
    "age": 24,
    "github": {
        "url": "https://github.com/Tinywan",
        "start": 2021
    }
}

可以通过 json_decode($decryptedJsonContent, true) ,转换为数组使用

Java Base64 加密密文解密

加密

// 双方约定的key
$key = 'wuExNti0srT13No1';
$content = [
    'name' => 'Tinywan',
    'School' => 'ZheJiang University',
    'age' => 24,
    'github' => [
        'url' => 'https://github.com/Tinywan',
        'start' => 2000,
    ],
];

$hexKey = bin2hex($key);
// 必须转换为字符串
$content = json_encode($content, JSON_UNESCAPED_UNICODE);
$sm4 = new SecuritySM4($hexKey);
$encryptContent =  $sm4->encrypt($content);
$base64encryptContent =  base64_encode(hex2bin($encryptContent));
print_r($base64encryptContent);
// 输出:CtVs1kzBAX9cOYpAZtcMCZ8n5mjyULAMZ65VTcGuP9fCv2iE1HCP7HZsJgmsrofLce8hH7I8MMCUgzWSLwPUET3I8bITbWYGTSLEMHkz4DLlklua2ky0FDJwxBqo5d2HnXpyMXaYJF1fT+5ztBgLhk0uBD9/DLSmOhiZIR1PcJc=

解密

// 双方约定的key
$key = 'wuExNti0srT13No1';
$content = 'CtVs1kzBAX9cOYpAZtcMCZ8n5mjyULAMZ65VTcGuP9fCv2iE1HCP7HZsJgmsrofLce8hH7I8MMCUgzWSLwPUET3I8bITbWYGTSLEMHkz4DLlklua2ky0FDJwxBqo5d2HnXpyMXaYJF1fT+5ztBgLhk0uBD9/DLSmOhiZIR1PcJc=';

$hexKey = bin2hex($key);
$baseDecodeContent = base64_decode($content);
$encryptContent = bin2hex($baseDecodeContent);

// 开始解密
$sm4 = new SM4($hexKey);
$decryptedJsonContent = $sm4->decrypt($encryptContent);
$origindData = json_decode($decryptedJsonContent,true);
print_r($origindData);

引用文献

  1. 《PHP实现国密算法SM4》

  2. 《关于PKCS5Padding与PKCS7Padding的区别》

php-java-sm4's People

Contributors

tinywan 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

Watchers

 avatar  avatar  avatar

php-java-sm4's Issues

java代码加了base64,php代码应该咋改?

java代码:
/**
* 加密
*
* @param data
* @param key
* @param charset
* @return
* @throws Exception
*/
public static String sm4Encrypt(String data, String key, String charset) throws Exception {
byte[] encrypt = SM4Util.encrypt_Ecb_Padding(ByteUtils.fromHexString(key), data.getBytes(charset));
return Base64.encodeBase64String(encrypt);
}

/**
 * 解密
 *
 * @param data
 * @param key
 * @param charset
 * @return
 * @throws Exception
 */
public static String sm4Decrypt(String data, String key, String charset) throws Exception {
    byte[] encrypt = SM4Util.decrypt_Ecb_Padding(ByteUtils.fromHexString(key), Base64.decodeBase64(data));
    String plainData = new String(encrypt, Charset.forName(charset));
    return plainData;
}

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.