如何使用Java加密技术?
在Java中,加密技术可以通过Java提供的加密API来实现。常见的加密方式有对称加密、非对称加密和哈希加密。下面是使用Java加密技术的完整使用攻略:
1. 对称加密
对称加密是指加密和解密都使用同一个密钥的加密方式。Java中常见的对称加密算法有DES、3DES、AES等,其中AES被广泛应用。下面是使用AES对称加密的示例代码:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESUtil {
private static final String KEY_ALGORITHM = "AES";
private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
public static String encrypt(String data, String key) throws Exception {
// 1. 生成密钥
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
keyGenerator.init(128);
SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF-8"), KEY_ALGORITHM);
// 2. 加密
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedData);
}
public static String decrypt(String data, String key) throws Exception {
// 1. 生成密钥
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
keyGenerator.init(128);
SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF-8"), KEY_ALGORITHM);
// 2. 解密
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(data));
return new String(decryptedData);
}
}
使用示例:
public static void main(String[] args) throws Exception {
String data = "Hello world!";
String key = "1234567890abcdef";
String encryptedData = AESUtil.encrypt(data, key);
System.out.println("加密后的数据:" + encryptedData);
String decryptedData = AESUtil.decrypt(encryptedData, key);
System.out.println("解密后的数据:" + decryptedData);
}
2. 非对称加密
非对称加密是指加密和解密使用不同的密钥的加密方式。在Java中,常用的非对称加密算法有RSA和DSA。下面是使用RSA非对称加密的示例代码:
import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
public class RSAUtil {
private static final String KEY_ALGORITHM = "RSA";
private static final String CIPHER_ALGORITHM = "RSA/ECB/PKCS1Padding";
public static String encrypt(String data, String publicKey) throws Exception {
// 1. 生成密钥对
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey _publicKey = keyPair.getPublic();
PrivateKey _privateKey = keyPair.getPrivate();
// 2. 加密
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, _publicKey);
byte[] encryptedData = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedData);
}
public static String decrypt(String data, String privateKey) throws Exception {
// 1. 获取私钥
PrivateKey _privateKey = RSAUtil.string2PrivateKey(privateKey);
// 2. 解密
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, _privateKey);
byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(data));
return new String(decryptedData);
}
public static PrivateKey string2PrivateKey(String privateKeyStr) throws Exception {
byte[] keyBytes = Base64.getDecoder().decode(privateKeyStr.getBytes());
return KeyFactory.getInstance(KEY_ALGORITHM).generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
}
}
使用示例:
public static void main(String[] args) throws Exception {
String data = "Hello world!";
String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4eL56hTphG5sqZe9DXIx" +
"vZmzdVXteoFXhuu6D1wOAHlq3OjO+lg23kQ7RFzj7cWzKXTcDCufzMBVf+JcdUEy" +
"YQDLQZ7HlCEJHmK4RFc4A2X0BQAL6Fd1rKxXxcvJk5zJNzbh2rMN/aKf9u7CVy45" +
"Vk6Sd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ouhu8c7Ru2hEshm8i1eT2xrkiawbnV" +
"7BUuxD8gA8X+5LpC6rX9Le7OHQ9YGUEp8TKqIpONRRe/wQoakrXgE+PuGvGHn0KJ" +
"9AKmp7i3q3QoZAzzj8eRaTc4OQKBgQDCz9GaWTL+R8Yw2MeSfkWeKiW0L6+xMt+2" +
"Vmgdh7f931mSThe5Owveqfon4Owkr8+e6gm9w3S6QlQm7VbbfS6quJKOcISQBrGG" +
"a5f0zZh1wkXNZ2P0LIzR8xO8kJMuJr7t+uzrdbp4NVdZKPdRC7JV0J0AZUQEZx88" +
"EBgyo2c36wKBgA9NBxK7Cm5kloT9eVnZxNBrP5Xt/HLF2vhyY3erfRmt+8pQj6K2" +
"55Kv3TITouvlfPuIOAbp92k6cG/nKOFh/I1Pwo5q6HbSTXoyuOxWrk0XLZ6d01ZQ" +
"K1RHKklXKwjYbp5kVpX2L6X/XtbkCfF/GIsVq2zzVXUHsA5CJmzjyC+1AoGBAN/q" +
"sdqXeU3blb8Zf62Cov+mX8JosnXuk/q6WDCXnxzz5xrA6UBgt9BfOIgOs05lNHAc" +
"RkYjkDpTa7NnNri1Mdwc3HvLPzfvgEgto7oAHRWgHx78zFK6ErM17PJoq8iGCN3f" +
"u63R11kd9sLfesuygRb9UQ2w5qqAw0bO8ABGoS//AoGBAJy861WIpmtozA4fmAGw" +
"BLa/1KtWqWYkT7r8DMHwYGhKuaL0O0hLz630rjTkPZuI4gWtK/V2iOdOolWvX9Jt" +
"L+auk/TH1Ew4cHY5bWeZBKI3vFJxLAG+w8+la7r1y/Zlxrx+6Gwmo9OYb977V6sI" +
"euU9/F3GKngW/urjLjhkkWm9";
String encryptedData = RSAUtil.encrypt(data, publicKey);
System.out.println("加密后的数据:" + encryptedData);
String decryptedData = RSAUtil.decrypt(encryptedData, privateKey);
System.out.println("解密后的数据:" + decryptedData);
}
3. 哈希加密
哈希加密是将任意长度的消息压缩成固定长度的摘要的加密方式。在Java中,常用的哈希算法有MD5和SHA系列算法。下面是使用MD5哈希加密的示例代码:
import java.security.MessageDigest;
public class MD5Util {
public static String encrypt(String data) throws Exception {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] digest = md5.digest(data.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}
}
使用示例:
public static void main(String[] args) throws Exception {
String data = "Hello world!";
String encryptedData = MD5Util.encrypt(data);
System.out.println("加密后的数据:" + encryptedData);
}
以上是使用Java加密技术的完整使用攻略,其中包含了对称加密、非对称加密和哈希加密三种加密方式。通过这些示例代码,可以方便地在Java中实现数据加密和解密的功能。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:如何使用Java加密技术? - Python技术站