我会详细讲解“详解DES加密算法的原理与Java实现”的完整攻略,并包含两条示例说明。
一、DES加密算法的原理
DES是一种分组加密算法,加密时将明文分成64位一组的大小,每组的最后一位用于存储校验位。DES总共使用16个循环轮次(每轮使用一个48位的密钥子)。第一轮会将明文分成左右两部分,右部分通过跟密钥进行一个函数F运算,F函数使得输入的较小变成较大,保证输入的变化能够传递到后续轮次。然后是左右互换之后的迭代,每次迭代的结果是新的左右两个组。最后一次迭代后,左右两个组不需要再互换,他们直接连接在一起形成最终的加密结果。
二、Java实现DES加密算法
Java有很多支持DES的库。
1. 实现方法一:SecretKeyFactory
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class DESUtil {
public static String encrypt(String data, String key) {
try {
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] result = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(result);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String decrypt(String data, String key) {
try {
byte[] bytes = Base64.getDecoder().decode(data);
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] result = cipher.doFinal(bytes);
return new String(result, StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
String plaintext = "Hello, world!";
String key = "12345678";
String ciphertext = encrypt(plaintext, key);
System.out.println("加密结果:" + ciphertext);
String decrypted = decrypt(ciphertext, key);
System.out.println("解密结果:" + decrypted);
}
}
2. 实现方法二:DESEncrypter类
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.ByteArrayOutputStream;
import java.security.InvalidKeyException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
public class DESEncrypter {
private Cipher ecipher;
private Cipher dcipher;
DESEncrypter(SecretKey key) {
try {
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
} catch (Exception e) {
e.printStackTrace();
}
}
public byte[] encrypt(byte[] input) throws InvalidKeyException {
return doFinal(input, ecipher);
}
public byte[] decrypt(byte[] input) throws InvalidKeyException {
return doFinal(input, dcipher);
}
private byte[] doFinal(byte[] input, Cipher cipher) throws InvalidKeyException {
try {
return cipher.doFinal(input);
} catch (Exception e) {
e.printStackTrace();
throw new InvalidKeyException("Invalid key to: " + e.getMessage());
}
}
public static void main(String[] args) throws InvalidKeySpecException {
try {
String plaintext = "Hello, world!";
byte[] sk = "12345678".getBytes();
KeySpec ks = new DESKeySpec(sk);
SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
SecretKey key = kf.generateSecret(ks);
DESEncrypter encrypter = new DESEncrypter(key);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(plaintext.getBytes());
byte[] encrypted = encrypter.encrypt(outputStream.toByteArray());
outputStream = new ByteArrayOutputStream();
outputStream.write(encrypted);
String decrypted = new String(encrypter.decrypt(outputStream.toByteArray()));
System.out.println("加密结果:" + new String(encrypted));
System.out.println("解密结果:" + decrypted);
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上就是两种Java实现DES加密算法的方法。需要注意的是,DES加密算法的安全性已经被攻破,不建议直接使用,推荐使用AES加密算法取代DES。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解DES加密算法的原理与Java实现 - Python技术站