java 利用des纯数字加密解密工具
时间: 2023-11-10 17:39:42 浏览: 51
下面是使用 DES 算法进行纯数字加密解密的 Java 工具类代码示例:
```java
import javax.crypto.Cipher;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.SecretKeyFactory;
import javax.crypto.SecretKey;
import java.security.spec.KeySpec;
import java.util.Base64;
public class NumericEncryption {
private static final String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding";
private static final String KEY_ALGORITHM = "DES";
private static final String CHARSET = "UTF-8";
private static final String IV_PARAMETER = "12345678"; // 8位长度的向量参数
public static void main(String[] args) {
String input = "12345678"; // 待加密的数字
String key = "abcdefgh"; // 密钥,必须是8位长度的字符串
String encrypted = encrypt(input, key); // 加密后的结果
System.out.println(encrypted);
String decrypted = decrypt(encrypted, key); // 解密后的结果
System.out.println(decrypted);
}
public static String encrypt(String input, String key) {
try {
IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET));
KeySpec keySpec = new DESKeySpec(key.getBytes(CHARSET));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
byte[] encryptedBytes = cipher.doFinal(input.getBytes(CHARSET));
return Base64.getEncoder().encodeToString(encryptedBytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String decrypt(String input, String key) {
try {
IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET));
KeySpec keySpec = new DESKeySpec(key.getBytes(CHARSET));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(input));
return new String(decryptedBytes, CHARSET);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
```
这段代码使用了 DES 算法进行加密和解密,需要注意以下几点:
1. 密钥必须是8位长度的字符串,因此在使用时需要对密钥进行处理。
2. 使用 CBC 模式需要指定一个8位长度的向量参数,可以自定义值。
3. 加密后的结果需要进行 Base64 编码,解密前需要进行 Base64 解码。
4. 在使用 Cipher 类时需要指定算法、加密模式和填充方式。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)