Coder Social home page Coder Social logo

445727994 / sm4 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tinywan/php-java-sm4

0.0 1.0 0.0 13 KB

基于PHP语言和Java语言的国密SM4算法库 。包含加密/解密,摘要算法的实现代码和测试方法 。

License: MIT License

PHP 66.55% Java 33.45%

sm4's Introduction

国密 SM4

SM4加密方式类似于AES加密,为对称加密,可以通过相应的秘钥进行加密和解密。

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

目录结构

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

使用

加密

$key = '35d251411ea04318565f0dbda6ffb6a8'

// 加密内容
$content = [
    'name' => 'Tinywan',
    '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'

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

$sm4 = new SM4($key);
$decryptedContent = $sm4->decrypt($content);
var_dump($decryptedContent);

解密结果

{
    "name": "tinywan",
    "age": 24,
    "github": {
        "url": "https://github.com/tinywan",
        "start": 2000
    }
}

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

引用文献

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

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

sm4's People

Contributors

tinywan avatar

Watchers

James Cloos avatar

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.