1 Star 7 Fork 2

cn-openjava / gmsm-java

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

国密算法-Java版

介绍

国密算法是我国自主研发的密码算法体系,用于保护我国信息安全。它包括对称密码算法、非对称密码算法和杂凑算法,如SM1、SM2、SM3、SM4、SM7、SM9和祖冲之密码算法等。 本软件主要提供了SM2、SM3、SM4三种算法。

SM2是一种非对称加密算法,基于离散对数实现,常用于数字签名和密钥交换;SM3是一种杂凑算法,用于数据完整性校验和消息认证码,该算法为不可逆的算法;SM4是一种分组密码算法,可用于替代DES/AES等国际密码算法,拥有和AES算法相同的密钥长度和分组长度,均为128比特。

除这三种算法的基本实现外,还提供了针对传输加解密、存储加解密、配置文件加解密三种应用场景的算法实现。

传输加解密:采用SM2+SM3+SM4(CBC)的组合。SM2加密SM4的密钥和向量值;SM4加密要传输的数据;SM3保证数据传输的完整性。整个过程中,SM4的密钥和向量值每次都会发生变化,即实现了“一次一密”。

存储加解密:采用SM3+SM4(CBC)的组合。SM3保证数据的完整性;SM4对数据进行加解密。

配置文件加解密:与存储加解密类似。

使用说明

本软件对应的jar包已经发布到maven中央仓库,只需要在pom.xml中引入即可:

<!--引入国密加解密组件-->
<dependency>
    <groupId>cn.openjava</groupId>
    <artifactId>gmsm</artifactId>
    <version>2.0.5</version>
</dependency>

在cn.openjava.gmsm.test中提供了测试方法:Test.java,运行后可看到各种算法运算后的值。

SM3摘要:C4B26BA9FB01E8A03884C6CEB3A6CAAE4B78EF8391BB8977E342662A32B04275
SM4-生成密钥:CA4AE6E12B72947BD53C71DCB9A8AF07
SM4-ECB加密:6C090B06707137C725AB3321A5FE6B22
SM4-ECB解密:你好,世界
SM4-生成iv:AA26EA6E61053EF7D5858D67F62DA66D
SM4-CBC加密:86833295EEC0EA65B451D071D1035AAE
SM4-CBC解密:你好,世界
SM2-公钥:046042E11E5AE6AEB8A02EE23F36023B99F3048176CA97B3E6416DEC543C402D2E2FB739C6BAAE8E8D4EC66496072E092D1A6DC42375C00337AF6AA871BDF72A90
SM2-私钥:C42B72B3054D98387D2C1360924C54178C2F379971633F5942746AA7F9EFADB8
SM2-密文:049F955BD2CC44C1450D845DD5720A8096B42D61AA20E378FF41739C4A226E7E81CACE7FD443C5C3F2AD7C18DA9BA3A65DA8BAA8D5F30E19FB92422939EACDA23ED9F3B643E2225BBAA008B93D14C43B82BDF1314B28EB56B1C08B5FB67A1F0DC6469C0304B6857B93C4C7D766372C38
SM2-明文:你好,世界
传输加密:D8E2853ABE1338E10AF080CEA93631A1603BAFFA87C7AE9F7F615F67EB8619B2E82182B388C0827BC8DAFE684FFC03F8C191D549F5565D83B4A661080FA87EC7D0A489DE2F06B6620E786D97389A3339B2A4CFACBDD4F77976BCD46783841D9D83706E1D0B1EAC48B936658D2BC2CA79G04C6A40D212E467CA0831111E7754BA1C5E2CC16C31DED0BBB4969E96B7A203D34EF10327BA981B5F6FBA295DC1A5C65E8FF785D8DBF121248A34071E25F881939C9AD64BECA8EDDD2AD38FC62189D07148BFD5F4B6D07596C7D0887750D819844A0B5FBDC0AD6728210DA6CB550C777ABE8E41CF19A0781EB2454442C94B64B5A
传输解密:你好,世界
传输解密-对象:你好,世界
配置文件加密:09CE1E6685A00B3556A4A49862F9EA9BA98EBBF58EAB0A8364351EACC59751AE5FB877105807AF6C38B7574A02B2341AA775B65781B04671393E495043E925C6A7D29F4BE5E64B48AFBBBBAF0023576EF5809444749A92E8BB4F8A95CB288F4B
配置文件解密:你好,世界
数据库加密:931A4033B0ABB103C741D397D02B5367551C2A444E630FDFF63FD0E8C37A388E53EDC25FACFDC272D8A3642A1B6C57A9886DA072D70D87E4072FC00606B73DA41F969DBF7437A2FFB2ED87CAD4D306685F36EC5D554A15E2A90F61E34F41B836
数据库解密:2

1.SM3

SM3.getDigest(data)

2.SM4生成密钥和向量值

String key = SM4.generateKey();

3.SM4(ECB算法)加密:

SM4.encrypt(data, key);

4.SM4(CBC算法)加密:

SM4.encrypt(data, key, iv);

5.SM4(ECB算法)解密:

SM4.decrypt(data, key);

6.SM4(CBC算法)接i密:

SM4.decrypt(data, key, iv);

7.SM2生成公钥和私钥:

SM2KeyPair sm2Keys = SM2.generateSm2Keys(false);
String publicKey = sm2Keys.getPublicKey();
System.out.println("SM2-公钥:" + publicKey);
String privateKey = sm2Keys.getPrivateKey();
System.out.println("SM2-私钥:" + privateKey);

8.SM2加密和解密:

String encrypt = SM2.encrypt(data, publicKey);
System.out.println("SM2-密文:" + encrypt);
String data2 = SM2.decrypt(encrypt, privateKey);
System.out.println("SM2-明文:" + data2);

9.传输加密和解密:

String encrypt1 = GmTransmissionUtil.encrypt(data, sm2PubKey);
System.out.println("传输加密:" + encrypt1);
CipherObj decrypt1 = GmTransmissionUtil.decrypt(encrypt1, sm2PriKey);
System.out.println("传输解密:" + decrypt1.getCipherText());
System.out.println("传输解密-对象:" + GmTransmissionUtil.decrypt(encrypt1, sm2PriKey, String.class));

注意:不要用此软件默认的sm2的公钥和私钥,不安全!!! 请自行生成公钥和私钥。

传输加解密需要和前台配合使用。前端加解密请访问https://gitee.com/cn-openjava/gmsm-js,二者可相互实现加解密。

10.数据库加解密:

String encrypt3 = GmDBUtil.encrypt(2, sm4Key, ivKey);
System.out.println("数据库加密:" + encrypt3);
Integer decrypt2 = GmDBUtil.decrypt(encrypt3, sm4Key, ivKey, Integer.class);
System.out.println("数据库解密:" + decrypt2);

注意:不要用此软件默认的sm4的密钥和向量值,不安全!!! 请自行生成密钥和向量值。

11.配置文件加解密:

String encrypt2 = GmConfigurationUtil.encrypt(data, sm4Key, ivKey);
System.out.println("配置文件加密:" + encrypt2);
System.out.println("配置文件解密:" + GmConfigurationUtil.decrypt(encrypt2, sm4Key, ivKey));

注意:不要用此软件默认的sm4的密钥和向量值,不安全!!! 请自行生成密钥和向量值。****

最后,如果软件好用,请您帮忙点个小星星🌟吧。❤️❤️❤️

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

国密算法-JAVA版。包含非对称算法SM2、对称算法SM4、摘要算法SM3。还包含传输加解密(SM2+SM3+SM4)、数据库加解密(SM3+SM4)、配置文件加解密(SM3+SM4)的组合使用。 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/cn-openjava/gmsm-java.git
git@gitee.com:cn-openjava/gmsm-java.git
cn-openjava
gmsm-java
gmsm-java
master

搜索帮助