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";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;}}public static String des3DecodeECB(String Key, String Data) {try {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;}}private static byte[] build3DesKey(String keyStr) {try {byte[] key = new byte[24]; byte[] temp = keyStr.getBytes("UTF-8"); if (key.length > temp.length) {System.arraycopy(temp, 0, key, 0, temp.length);} else {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";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;}}public static String des3DecodeECB(String Key, String Data) {try {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;}}private static byte[] build3DesKey(String keyStr) {try {byte[] key &#61; new byte[24]; byte[] temp &#61; keyStr.getBytes("UTF-8"); if (key.length > temp.length) {System.arraycopy(temp, 0, key, 0, temp.length);} else {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