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

Java中常用的加密/解密函数及其实现

发布时间:2023-06-18 08:55:40

Java中常用的加密/解密函数包括MD5、SHA、AES、DES、RSA等。对于一些需要保密的信息,进行加密处理可以保证信息的安全性。下面将介绍这些常用加密/解密函数及其实现。

1. MD5

MD5是一种常用的哈希函数,可以将任意长度的信息压缩成一个128位的哈希值。MD5算法可以很好地保护信息不被篡改。在Java中可以使用java.security.MessageDigest类来实现MD5算法的加密。

示例代码:

public static String md5(String input) throws Exception {
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] result = md.digest(input.getBytes());
    StringBuilder sb = new StringBuilder();
    for (byte b : result) {
        sb.append(String.format("%02x", b));
    }
    return sb.toString();
}

2. SHA

SHA是一种安全哈希算法,其安全性比MD5高。SHA算法有多种版本,包括SHA-1、SHA-224、SHA-256、SHA-384和SHA-512等。在Java中可以使用java.security.MessageDigest类来实现SHA算法的加密。

示例代码:

public static String sha(String input) throws Exception {
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    byte[] result = md.digest(input.getBytes());
    StringBuilder sb = new StringBuilder();
    for (byte b : result) {
        sb.append(String.format("%02x", b));
    }
    return sb.toString();
}

3. AES

AES是一种高级加密标准,它使用对称密钥加密机制,可以保证数据的安全性和完整性。在Java中可以使用javax.crypto.Cipher类来实现AES算法的加密和解密。

示例代码:

public static byte[] aesEncrypt(byte[] input, byte[] key) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    return cipher.doFinal(input);
}

public static byte[] aesDecrypt(byte[] input, byte[] key) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    return cipher.doFinal(input);
}

4. DES

DES是一种对称密钥加密算法,它被广泛用于数据加密和解密。在Java中可以使用javax.crypto.Cipher类来实现DES算法的加密和解密。

示例代码:

public static byte[] desEncrypt(byte[] input, byte[] key) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(key, "DES");
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    return cipher.doFinal(input);
}

public static byte[] desDecrypt(byte[] input, byte[] key) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(key, "DES");
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    return cipher.doFinal(input);
}

5. RSA

RSA是一种非对称加密算法,它使用公钥和私钥对数据进行加密和解密。在Java中可以使用java.security.KeyPairGenerator类来生成RSA公钥和私钥,使用javax.crypto.Cipher类来实现RSA算法的加密和解密。

示例代码:

public static KeyPair generateKeyPair() throws Exception {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    keyPairGenerator.initialize(1024);
    return keyPairGenerator.generateKeyPair();
}

public static byte[] rsaEncrypt(byte[] input, PublicKey publicKey) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    return cipher.doFinal(input);
}

public static byte[] rsaDecrypt(byte[] input, PrivateKey privateKey) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    return cipher.doFinal(input);
}

综上所述,Java中常用的加密/解密函数包括MD5、SHA、AES、DES、RSA等。通过这些算法,在Java中可以实现对数据的安全加密和解密。