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

Java函数实现加解密算法的实现与应用

发布时间:2023-07-04 14:43:30

Java是一种面向对象的高级编程语言,有着广泛的应用领域。在加解密算法方面,Java提供了一些现成的库和函数,可以方便地实现各种加解密算法。

首先,我们可以使用Java的Security API来实现对称加解密算法,如DES、AES等。对称加解密算法使用同一个密钥进行加密和解密,因此需要确保密钥的保密性。下面是一个使用AES加解密算法的示例代码:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class EncryptionUtils {
    public static String encrypt(String input, String key) {
        try {
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] encryptedBytes = cipher.doFinal(input.getBytes());
            return Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decrypt(String encryptedInput, String key) {
        try {
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedInput));
            return new String(decryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

使用上述代码,我们可以很方便地实现对字符串的加解密操作。调用encrypt函数传入明文和密钥,可以得到密文;调用decrypt函数传入密文和密钥,可以得到明文。

此外,Java还提供了非对称加密算法的实现,如RSA。非对称加密算法使用一对密钥,包括公钥和私钥。公钥用于加密,私钥用于解密。下面是用RSA算法进行加解密的示例代码:

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import java.util.Base64;

public class EncryptionUtils {
    public static KeyPair generateKeyPair() {
        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(1024);
            return keyPairGenerator.generateKeyPair();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String encrypt(String input, PublicKey publicKey) {
        try {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] encryptedBytes = cipher.doFinal(input.getBytes());
            return Base64.getEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decrypt(String encryptedInput, PrivateKey privateKey) {
        try {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedInput));
            return new String(decryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static PublicKey convertToPublicKey(byte[] publicKeyBytes) {
        try {
            X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes);
            return KeyFactory.getInstance("RSA").generatePublic(spec);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static PrivateKey convertToPrivateKey(byte[] privateKeyBytes) {
        try {
            PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKeyBytes);
            return KeyFactory.getInstance("RSA").generatePrivate(spec);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

上述代码提供了生成密钥对、加密和解密的功能。首先使用generateKeyPair函数生成一对密钥。使用encrypt函数传入明文和公钥进行加密,得到密文;使用decrypt函数传入密文和私钥进行解密,得到明文。

以上是加解密算法在Java中的实现与应用的简要介绍。通过调用Java提供的库和函数,我们可以很方便地实现各种加解密算法,保护敏感数据的安全性。