欢迎访问宙启技术站
智能推送

使用Java函数实现简单的加密、解密操作

发布时间:2023-06-23 05:00:27

加密和解密是信息安全领域中常用的术语,我们常常在网络传输、文件存储等方面使用加密技术来保证信息的安全性。本文将介绍如何使用Java函数实现简单的加密和解密操作。

加密是指将原始信息通过某种算法转换为难以被人直接理解的形式,以达到保护信息安全的目的。解密则是将加密后的信息还原成原本的明文。

Java中提供了多种加密和解密的API,如MessageDigest、Cipher等。下面我们分别介绍几种常见的加密和解密算法的实现方法。

1. MD5加密

MD5是一种广泛使用的加密算法,它将任意长度的字符串转换成固定长度的128位字符串。Java中可以使用MessageDigest类来实现MD5加密。

示例代码:

import java.math.BigInteger;
import java.security.MessageDigest;
 
public class MD5Util {
 
    public static String md5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger no = new BigInteger(1, messageDigest);
            String hashtext = no.toString(16);
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

2. AES加密

AES是一种高级加密标准,它是一种对称加密算法,即加密和解密都使用同一个密钥。Java中可以使用Cipher类来实现AES加密。

示例代码:

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
 
public class AESUtil {
 
    private static final String ALGORITHM = "AES";
    private static final String KEY = "myencryptkey";
 
    public static String encrypt(String input) throws Exception {
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encrypted = cipher.doFinal(input.getBytes());
        return Base64.getEncoder().encodeToString(encrypted);
    }
 
    public static String decrypt(String input) throws Exception {
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decoded = Base64.getDecoder().decode(input);
        byte[] decrypted = cipher.doFinal(decoded);
        return new String(decrypted);
    }
 
    private static Key generateKey() throws Exception {
        byte[] key = KEY.getBytes();
        return new SecretKeySpec(key, ALGORITHM);
    }
}

3. RSA加密

RSA是一种非对称加密算法,它需要一对公钥和私钥来进行加密和解密。Java中可以使用KeyPairGenerator、Cipher类来实现RSA加密。

示例代码:

import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import javax.crypto.Cipher;
import java.util.Base64;
 
public class RSAUtil {
 
    public static final int KEY_SIZE = 1024;
 
    public static KeyPair generateKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(KEY_SIZE);
        return keyPairGenerator.generateKeyPair();
    }
 
    public static String encrypt(String input, Key publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encrypted = cipher.doFinal(input.getBytes());
        return Base64.getEncoder().encodeToString(encrypted);
    }
 
    public static String decrypt(String input, Key privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decoded = Base64.getDecoder().decode(input);
        byte[] decrypted = cipher.doFinal(decoded);
        return new String(decrypted);
    }
}

以上三种加密算法都是常见的加密方式,可以根据需要选择使用。在实际应用中,我们也可以将多种加密算法进行组合使用,以达到更高的安全性。

总结:

本文介绍了使用Java函数实现简单的加密、解密操作,并分别介绍了MD5、AES、RSA三种常见的加密算法的实现方法。您也可以根据需求选择适合的加密算法来保护数据的安全。