热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

3DES加解密/PKCS5Padding/PKCS7Padding

1说明2完整代码publicstaticvoidmain(String[]args)throwsIOException{Stringstrdes3EncodeECB(Key3D
1 说明

在这里插入图片描述

2 完整代码

public static void main(String[] args) throws IOException {String str = des3EncodeECB("Key3Des","@##h586ellAAon你好");System.out.println(str);System.out.println(des3DecodeECB("Key3Des",str));}private static final String DES3 = "DESede";/*** 3DES ECB模式加密*/public static String des3EncodeECB(String Key, String Data) {try {byte[] data=Data.getBytes("UTF-8");byte[] key = build3DesKey(Key);SecretKey DESKey = new SecretKeySpec(key, DES3); //生成密钥Cipher cipher = Cipher.getInstance(DES3 + "/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, DESKey);return new BASE64Encoder().encode(cipher.doFinal(data));} catch (Exception e) {e.printStackTrace();return null;}}/*** 3DES ECB模式解密*/public static String des3DecodeECB(String Key, String Data) {try {//--通过base64,将字符串转成byte数组BASE64Decoder decoder = new BASE64Decoder();byte[] data = decoder.decodeBuffer(Data);byte[] key = build3DesKey(Key);SecretKey DESKey = new SecretKeySpec(key, DES3); //生成密钥Cipher cipher = Cipher.getInstance(DES3 + "/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, DESKey);byte[] bout=cipher.doFinal(data);return new String(bout,"UTF-8");} catch (Exception e) {e.printStackTrace();return null;}}/*** 根据字符串生成密钥字节数组** @param keyStr 密钥字符串*/private static byte[] build3DesKey(String keyStr) {try {byte[] key = new byte[24]; //声明一个24位的字节数组,默认里面都是0byte[] temp = keyStr.getBytes("UTF-8"); //将字符串转成字节数组if (key.length > temp.length) {//如果temp不够24位,则拷贝temp数组整个长度的内容到key数组中System.arraycopy(temp, 0, key, 0, temp.length);} else {//如果temp大于24位,则拷贝temp数组24个长度的内容到key数组中System.arraycopy(temp, 0, key, 0, key.length);}return key;} catch (Exception e) {e.printStackTrace();return null;}}
3 验证

在这里插入图片描述

4 更换为PKCS7Padding

Maven

<dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15on</artifactId><version>1.56</version></dependency>

添加 默认加密提供者

static { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());}

package com.example.demo.tools;import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;public class Des3 {public static void main(String[] args) throws IOException {String str &#61; des3EncodeECB("Key3Des","&#64;##h586ellAAon你好");System.out.println(str);System.out.println(des3DecodeECB("Key3Des",str));}static {Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());}private static final String DES3 &#61; "DESede";/*** 3DES ECB模式加密*/public static String des3EncodeECB(String Key, String Data) {try {byte[] data&#61;Data.getBytes("UTF-8");byte[] key &#61; build3DesKey(Key);SecretKey DESKey &#61; new SecretKeySpec(key, DES3); //生成密钥Cipher cipher &#61; Cipher.getInstance(DES3 &#43; "/ECB/PKCS7Padding");cipher.init(Cipher.ENCRYPT_MODE, DESKey);return new BASE64Encoder().encode(cipher.doFinal(data));} catch (Exception e) {e.printStackTrace();return null;}}/*** 3DES ECB模式解密*/public static String des3DecodeECB(String Key, String Data) {try {//--通过base64,将字符串转成byte数组BASE64Decoder decoder &#61; new BASE64Decoder();byte[] data &#61; decoder.decodeBuffer(Data);byte[] key &#61; build3DesKey(Key);SecretKey DESKey &#61; new SecretKeySpec(key, DES3); //生成密钥Cipher cipher &#61; Cipher.getInstance(DES3 &#43; "/ECB/PKCS7Padding");cipher.init(Cipher.DECRYPT_MODE, DESKey);byte[] bout&#61;cipher.doFinal(data);return new String(bout,"UTF-8");} catch (Exception e) {e.printStackTrace();return null;}}/*** 根据字符串生成密钥字节数组** &#64;param keyStr 密钥字符串*/private static byte[] build3DesKey(String keyStr) {try {byte[] key &#61; new byte[24]; //声明一个24位的字节数组&#xff0c;默认里面都是0byte[] temp &#61; keyStr.getBytes("UTF-8"); //将字符串转成字节数组if (key.length > temp.length) {//如果temp不够24位&#xff0c;则拷贝temp数组整个长度的内容到key数组中System.arraycopy(temp, 0, key, 0, temp.length);} else {//如果temp大于24位&#xff0c;则拷贝temp数组24个长度的内容到key数组中System.arraycopy(temp, 0, key, 0, key.length);}return key;} catch (Exception e) {e.printStackTrace();return null;}}
}

5 参考文献

https://blog.csdn.net/weixin_43272781/article/details/107330649

https://www.jianshu.com/p/028fe67e6c58

http://tool.chacuo.net/cryptdes查阅网站
公众号地址

博客地址

https://blog.csdn.net/weixin_41563161

公众号

博客地址

https://blog.csdn.net/weixin_41563161
在这里插入图片描述


推荐阅读
author-avatar
carmen青春-潮人帮
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有