Python中如何获取Windows桌面上的Excel文件,最详细教程
时间: 2024-06-12 18:06:17 浏览: 12
以下是一个Java编写的DES加密算法示例:
```
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Key;
public class DESUtils {
private static final String ALGORITHM = "DES";
// 生成密钥
public static byte[] generateKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
keyGenerator.init(56);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
}
// 加密
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
Key k = new SecretKeySpec(key, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, k);
return cipher.doFinal(data);
}
// 解密
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
Key k = new SecretKeySpec(key, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, k);
return cipher.doFinal(data);
}
public static void main(String[] args) throws Exception {
String plainText = "Hello, world!"; // 待加密的明文
byte[] key = generateKey(); // 生成密钥
byte[] cipherText = encrypt(plainText.getBytes(StandardCharsets.UTF_8), key); // 加密
byte[] plainData = decrypt(cipherText, key); // 解密
System.out.println(new String(plainData, StandardCharsets.UTF_8)); // 打印解密后的明文
}
}
```
该示例中使用了Java标准库中的Cipher类来进行DES加密和解密。其中,generateKey()方法用于生成随机的56位DES密钥;encrypt()方法用于加密数据;decrypt()方法用于解密数据。在main()方法中,首先生成密钥,然后用该密钥加密明文,再用密钥解密密文,并打印解密后的明文。