常用加密算法的java实现
0 背景
加密技术是信息安全领域的核心组成部分,它通过将信息转换成只有授权用户才能解读的格式来保护数据的机密性和完整性。很多时候, 出于数据安全的角度考虑, 在信息传输的过程中, 我们需要对敏感的数据进行加密处理,防止数据泄露。它在保护信息安全、维护隐私权和确保网络活动安全方面发挥着至关重要的作用。
本文主要使用JAVA实现一些加密算法, 以便于在一些场景中更快速的使用, 比如爬虫。在之前我们也介绍了常用的加密算法在python中的实现: 常用加密算法的python实现.
常用的加密算法可以分为以下几种:
哈希算法
如MD5、SHA1、SHA256算法等, 这种加密方式将原始数据, 转换为固定长度的散列值。这是一种消息摘要算法, 一种单向加密的过程, 就是数据一旦被加密, 就不能还原成原来的数据.
对称加密算法
像AES, DES, 3DES 算法是一种堆成加密算法, 他们的特点是加密和解密过程使用相同的密钥。这种算法的主要优势在于其加解密速度快,适合于大量数据的加密。
非对称加密算法
像RSA、DSA等算法, 在加密和解密的过程中, 分别使用不同的密钥进行数据的加密和解密, 这两个密钥分别是公钥和私钥,这两个密钥是相关联的,但彼此不同。通常, 一个是公钥,用来公开分享,用于数据的加密, 另一个是私钥,自己保密, 不让别人知道, 用来进行数据的解密.
1 算法实现
md5
import java.security.MessageDigest;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
public class EncryptDecrypt {
public static String encode_md5(String text) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(text.getBytes(StandardCharsets.UTF_8));
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}
public static void main(String[] args) throws NoSuchAlgorithmException {
String encode_text = encode_md5("1");
System.out.println("md5 hash result is: " + encode_text);
}
}
// md5 hash result is: c4ca4238a0b923820dcc509a6f75849b
sha256
import java.security.MessageDigest;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
public class EncryptDecrypt {
public static String encode_sha256(String text) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(text.getBytes(StandardCharsets.UTF_8));
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}
public static void main(String[] args) throws NoSuchAlgorithmException {
String encode_text_sha256 = encode_sha256("1");
System.out.println("sha256 hash result is: " + encode_text_sha256);
}
}
// sha256 hash result is: 6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b
base64
编码 & 解码
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class EncryptDecrypt {
public static String encode_base64(String text) {
String encode_text = Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8));
return encode_text;
}
public static String decode_base64(String text) {
byte[] decode_bytes = Base64.getDecoder().decode(text);
return new String(decode_bytes, StandardCharsets.UTF_8);
}
public static void main(String[] args) throws NoSuchAlgorithmException {
String encode_text_base64 = encode_base64("1");
System.out.println("base64 encode result is: " + encode_text_base64);
String decode_text_base64 = decode_base64("MQ==");
System.out.println("base64 decode result is: " + decode_text_base64);
}
}
// base64 encode result is: MQ==
// base64 decode result is: 1
aes
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class Aes {
static String key = "c4ca4238a0b92382";
static SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "AES");
public static String encrypt_aes(String text) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret_key);
byte[] encrypt_bytes = cipher.doFinal(text.getBytes(StandardCharsets.UTF_8));
String encrypt_text = Base64.getEncoder().encodeToString(encrypt_bytes);
return encrypt_text;
}
public static String decrypt_aes(String text) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secret_key);
byte[] decrypt_bytes = cipher.doFinal(Base64.getDecoder().decode(text));
String decrypt_text = new String(decrypt_bytes, StandardCharsets.UTF_8);
return decrypt_text;
}
public static void main(String[] args) throws Exception {
String encrypt_text = encrypt_aes("1");
System.out.println("encrypt_aes: " + encrypt_text);
// jqEKi41hXQIyQ5bD30WrCA==
String decrypt_text = decrypt_aes("jqEKi41hXQIyQ5bD30WrCA==");
System.out.println("decrypt_aes: " + decrypt_text);
// 1
}
}
rsa
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import javax.crypto.Cipher;
public class Rsa {
public static KeyPair generate_keypair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}
public static String encrypt_rsa(PublicKey publicKey, String text) throws Exception {
Cipher encryptCipher = Cipher.getInstance("RSA");
encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encrypt_bytes = encryptCipher.doFinal(text.getBytes());
String encrypt_text = Base64.getEncoder().encodeToString(encrypt_bytes);
return encrypt_text;
}
public static String encrypt_rsa(String publicKeyString, String text) throws Exception {
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyString));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
return encrypt_rsa(publicKey, text);
}
public static String decrypt_rsa(PrivateKey privateKey, String text) throws Exception {
Cipher decryptCipher = Cipher.getInstance("RSA");
decryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decrypt_bytes = decryptCipher.doFinal(Base64.getDecoder().decode(text));
String decrypt_text = new String(decrypt_bytes);
return decrypt_text;
}
public static String decrypt_rsa(String privateKeyString, String text) throws Exception {
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyString.getBytes()));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
return decrypt_rsa(privateKey, text);
}
public static void main(String[] args) throws Exception {
KeyPair keyPair = generate_keypair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
String encrypt_text = encrypt_rsa(publicKey, "1");
System.out.println("encrypt_rsa: " + encrypt_text);
String decrypt_text = decrypt_rsa(privateKey, encrypt_text);
System.out.println("decrypt_aes: " + decrypt_text);
}
}
//encrypt_rsa: AkMYpVzQRvhXLHCMoO.../nBSEL4B0K4TaEdOdbXDA==
//decrypt_aes: 1
2 关于
由于国内安卓端APP应用还是以java开发为主, 因此了解一些常用加密算法的原理和实现是实现app端爬虫的基础.
欢迎关注我的微信公众号
原文始发于微信公众号(其之):常用加密算法的java实现
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/204865.html