一、JavaCrypto编码
JavaCrypto是Java平台自带的加密解密API,可以进行各种加密算法的实现,包括对称加密和非对称加密。对称加密的特点是加密解密使用的是相同的密钥,常见的有DES、AES等算法;非对称加密的特点是加密解密使用的是不同的密钥,常见的有RSA、DSA等算法。
JavaCrypto为我们提供了进行加密解密的工具。下面给出一个DES加密的示例代码:
public static String encrypt(String plaintext, String password) {
try {
SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(), "DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(plaintext.getBytes());
return new String(Base64.getEncoder().encode(encryptedData));
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
以上代码实现了对一个字符串进行DES加密,并返回了加密结果的Base64编码。
二、JavaCrypto解密
JavaCrypto还提供了各种解密算法的实现,包括对称解密和非对称解密。对称解密的特点是和加密使用相同的密钥进行解密,常见的有DES、AES等算法;非对称解密的特点是使用私钥进行解密,常见的有RSA、DSA等算法。
下面给出一个DES解密的示例代码:
public static String decrypt(String ciphertext, String password) {
try {
SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(), "DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(ciphertext.getBytes()));
return new String(decryptedData);
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
以上代码实现了对一个DES加密后的结果进行解密,并返回了解密得到的原文。
三、JavaCrypto应用案例
JavaCrypto的应用场景非常广泛,下面给出一个简单的应用案例——对一个文件进行AES加密。
首先需要生成一个AES密钥:
public static byte[] generateAesKey() {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
以上代码通过AES算法生成了一个128位的密钥。
接下来对文件进行AES加密:
public static void encryptFile(String inputFilePath, String outputFilePath, byte[] aesKey) {
try {
SecretKeySpec secretKey = new SecretKeySpec(aesKey, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
FileInputStream inputStream = new FileInputStream(inputFilePath);
FileOutputStream outputStream = new FileOutputStream(outputFilePath);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
byte[] encryptedData = cipher.update(buffer, 0, length);
outputStream.write(encryptedData);
}
byte[] encryptedData = cipher.doFinal();
outputStream.write(encryptedData);
inputStream.close();
outputStream.flush();
outputStream.close();
} catch(Exception e) {
e.printStackTrace();
}
}
以上代码实现了对一个文件进行AES加密,并将结果输出到另一个文件中。
最后,对加密后的文件进行解密:
public static void decryptFile(String inputFilePath, String outputFilePath, byte[] aesKey) {
try {
SecretKeySpec secretKey = new SecretKeySpec(aesKey, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
FileInputStream inputStream = new FileInputStream(inputFilePath);
FileOutputStream outputStream = new FileOutputStream(outputFilePath);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
byte[] decryptedData = cipher.update(buffer, 0, length);
outputStream.write(decryptedData);
}
byte[] decryptedData = cipher.doFinal();
outputStream.write(decryptedData);
inputStream.close();
outputStream.flush();
outputStream.close();
} catch(Exception e) {
e.printStackTrace();
}
}
以上代码实现了对加密后的文件进行解密,并将结果输出到另一个文件中。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/156757.html
微信扫一扫
支付宝扫一扫