测试效果:
DES 测试:
密码:e44a6d94-fade-44cd-ab33-5e0060125334
明文:1234567890
密文:GNJ/2VwEXKQJntTFEe1jSg==
解密:1234567890
Triple DES 测试:
密码:e44a6d94-fade-44cd-ab33-5e0060125334
明文:1234567890
密文:OCYnKHgjmu16p8x3lLiCKQ==
解密:1234567890
import java.util.Base64; | |
import javax.crypto.Cipher; | |
import javax.crypto.SecretKey; | |
import java.security.SecureRandom; | |
import javax.crypto.spec.DESKeySpec; | |
import javax.crypto.SecretKeyFactory; | |
/** | |
* DES 加密解密工具 | |
* @author 鸿则<hungtcs@163.com> | |
* @date 2020-05-07 | |
*/ | |
public class DESUtils { | |
private static final String algorithm = "DES"; | |
private static final String charsetName = "UTF-8"; | |
private static final String transformation = "DES"; | |
private DESUtils() { | |
} | |
public static String encrypt(String input, String password) throws Exception { | |
SecureRandom random = new SecureRandom(); | |
DESKeySpec desKeySpec = new DESKeySpec(password.getBytes(DESUtils.charsetName)); | |
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DESUtils.algorithm); | |
SecretKey secretKey = keyFactory.generateSecret(desKeySpec); | |
Cipher cipher = Cipher.getInstance(DESUtils.transformation); | |
cipher.init(Cipher.ENCRYPT_MODE, secretKey, random); | |
byte[] a = cipher.doFinal(input.getBytes()); | |
return Base64.getEncoder().encodeToString(a); | |
} | |
public static String decrypt(String input, String password) throws Exception { | |
SecureRandom random = new SecureRandom(); | |
DESKeySpec desKeySpec = new DESKeySpec(password.getBytes(DESUtils.charsetName)); | |
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DESUtils.algorithm); | |
SecretKey secretKey = keyFactory.generateSecret(desKeySpec); | |
byte[] inputBytes = Base64.getDecoder().decode(input); | |
Cipher cipher = Cipher.getInstance(DESUtils.transformation); | |
cipher.init(Cipher.DECRYPT_MODE, secretKey, random); | |
return new String(cipher.doFinal(inputBytes), DESUtils.charsetName); | |
} | |
} |
import crypto from 'crypto-js'; | |
export class EncryptDecodeUtils { | |
public static encryptDES(plain: string, password: string) { | |
const keyHex = crypto.enc.Utf8.parse(password); | |
return crypto.DES.encrypt(plain, keyHex, { | |
mode: crypto.mode.ECB, | |
padding: crypto.pad.Pkcs7, | |
}).toString(); | |
} | |
public static decodeDES(encrypted: string, password: string) { | |
const keyHex = crypto.enc.Utf8.parse(password); | |
return crypto.DES.decrypt(encrypted, keyHex, { | |
mode: crypto.mode.ECB, | |
padding: crypto.pad.Pkcs7, | |
}).toString(crypto.enc.Utf8); | |
} | |
public static encryptTripleDES(plain: string, password: string) { | |
const keyHex = crypto.enc.Utf8.parse(password); | |
return crypto.TripleDES.encrypt(plain, keyHex, { | |
mode: crypto.mode.ECB, | |
padding: crypto.pad.Pkcs7, | |
}).toString(); | |
} | |
public static decodeTripleDES(encrypted: string, password: string) { | |
const keyHex = crypto.enc.Utf8.parse(password); | |
return crypto.TripleDES.decrypt(encrypted, keyHex, { | |
mode: crypto.mode.ECB, | |
padding: crypto.pad.Pkcs7, | |
}).toString(crypto.enc.Utf8); | |
} | |
} |
/** | |
* @author 鸿则<hungtcs@163.com> | |
* @date 2020-05-07 | |
*/ | |
public class Main { | |
private final String password = "e44a6d94-fade-44cd-ab33-5e0060125334"; | |
public static void main(String[] args) throws Exception { | |
Main main = new Main(); | |
main.test1(); | |
System.out.println(); | |
main.test2(); | |
} | |
private void test1() throws Exception { | |
System.out.println("DES 测试:"); | |
final String plain = "1234567890"; | |
System.out.println("密码:" + this.password); | |
System.out.println("明文:" + plain); | |
String encrypted = DESUtils.encrypt(plain, this.password); | |
System.out.println("密文:" + encrypted); | |
String decrypted = DESUtils.decrypt(encrypted, this.password); | |
System.out.println("解密:" + decrypted); | |
} | |
private void test2() throws Exception { | |
System.out.println("Triple DES 测试:"); | |
final String plain = "1234567890"; | |
System.out.println("密码:" + this.password); | |
System.out.println("明文:" + plain); | |
String encrypted = TripleDESUtils.encrypt(plain, this.password); | |
System.out.println("密文:" + encrypted); | |
String decrypted = TripleDESUtils.decrypt(encrypted, this.password); | |
System.out.println("解密:" + decrypted); | |
} | |
} |
import java.util.Base64; | |
import javax.crypto.Cipher; | |
import javax.crypto.SecretKey; | |
import java.security.SecureRandom; | |
import javax.crypto.SecretKeyFactory; | |
import javax.crypto.spec.DESedeKeySpec; | |
/** | |
* Triple DES 加密解密工具 | |
* @author 鸿则<hungtcs@163.com> | |
* @date 2020-05-07 | |
*/ | |
public class TripleDESUtils { | |
private static final String algorithm = "DESede"; | |
private static final String charsetName = "UTF-8"; | |
private static final String transformation = "DESede/ECB/PKCS5Padding"; | |
private TripleDESUtils() { | |
} | |
public static String encrypt(String input, String password) throws Exception { | |
SecureRandom random = new SecureRandom(); | |
DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(password.getBytes(TripleDESUtils.charsetName)); | |
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(TripleDESUtils.algorithm); | |
SecretKey secretKey = keyFactory.generateSecret(deSedeKeySpec); | |
Cipher cipher = Cipher.getInstance(TripleDESUtils.transformation); | |
cipher.init(Cipher.ENCRYPT_MODE, secretKey, random); | |
byte[] a = cipher.doFinal(input.getBytes()); | |
return Base64.getEncoder().encodeToString(a); | |
} | |
public static String decrypt(String input, String password) throws Exception { | |
SecureRandom random = new SecureRandom(); | |
DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(password.getBytes(TripleDESUtils.charsetName)); | |
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(TripleDESUtils.algorithm); | |
SecretKey secretKey = keyFactory.generateSecret(deSedeKeySpec); | |
byte[] inputBytes = Base64.getDecoder().decode(input); | |
Cipher cipher = Cipher.getInstance(TripleDESUtils.transformation); | |
cipher.init(Cipher.DECRYPT_MODE, secretKey, random); | |
return new String(cipher.doFinal(inputBytes), TripleDESUtils.charsetName); | |
} | |
} |