常用加密解密算法java
时间: 2023-05-17 18:03:30 浏览: 96
常用的加密解密算法有DES、AES、RSA、MD5、SHA等。在Java中,可以使用javax.crypto包中的类来实现这些算法。例如,使用Cipher类来进行DES和AES的加密解密操作,使用KeyPairGenerator和Cipher类来进行RSA的加密解密操作,使用MessageDigest类来进行MD5和SHA的加密操作。
相关问题
DES加密解密算法java
DES加密解密算法是一种对称加密算法,在Java中可以使用JDK自带的Cipher类实现。下面是一个简单的示例代码:
```java
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.NoSuchAlgorithmException;
import java.util.Base64;
public class DesDemo {
private static final String KEY_ALGORITHM = "DES";
private static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";
public static String encrypt(String content, String password) throws Exception {
SecretKey secretKey = generateKey(password);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String content, String password) throws Exception {
SecretKey secretKey = generateKey(password);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] encryptedBytes = Base64.getDecoder().decode(content);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
private static SecretKey generateKey(String password) throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
keyGenerator.init(56);
return new SecretKeySpec(password.getBytes(StandardCharsets.UTF_8), KEY_ALGORITHM);
}
public static void main(String[] args) throws Exception {
String content = "Hello, World!";
String password = "12345678";
String encryptedContent = encrypt(content, password);
String decryptedContent = decrypt(encryptedContent, password);
System.out.println("明文:" + content);
System.out.println("密文:" + encryptedContent);
System.out.println("解密后:" + decryptedContent);
}
}
```
aes加密 解密算法 java
在Java中,可以使用Java Cryptography Extension(JCE)提供的AES加密和解密算法。下面是一个简单的示例代码,可以实现AES加密和解密:
```java
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESUtils {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
private static final String CHARSET = "UTF-8";
public static String encrypt(String plainText, String key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(CHARSET), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] cipherText = cipher.doFinal(plainText.getBytes(CHARSET));
return Base64.getEncoder().encodeToString(cipherText);
}
public static String decrypt(String cipherText, String key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(CHARSET), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] plainText = cipher.doFinal(Base64.getDecoder().decode(cipherText));
return new String(plainText, CHARSET);
}
}
```
使用示例:
```java
String plainText = "Hello, world!";
String key = "0123456789abcdef"; // 密钥必须是16位、24位或32位
String cipherText = AESUtils.encrypt(plainText, key);
System.out.println(cipherText);
String decryptedText = AESUtils.decrypt(cipherText, key);
System.out.println(decryptedText);
```
上述代码中,`encrypt`方法用于加密明文,`decrypt`方法用于解密密文。需要注意的是,密钥必须是16位、24位或32位,否则会抛出异常。另外,`TRANSFORMATION`指定了加密算法、工作模式和填充方式,这里使用的是ECB模式和PKCS5Padding填充方式。在实际应用中,需要根据具体需求选择合适的加密算法、工作模式和填充方式。