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

Java函数中的加密和解密方法

发布时间:2023-06-16 07:23:25

Java函数中的加密和解密方法是一种常用的技术,用于保护数据的机密性和完整性。Java本身提供了很多加密和解密的方法,包括对称加密算法、非对称加密算法、消息摘要算法和数字签名算法等。下面将介绍常见的加密和解密方法。

一、对称加密算法

对称加密算法是指加密和解密使用相同的密钥,加密过程如下所示:

①使用密钥对明文进行处理,生成密文;

②使用密钥对密文进行处理,还原成明文。

Java提供了常见的对称加密算法DES、AES、RC2等,下面以AES算法为例进行介绍。

1. AES加解密方法

在Java中使用AES算法进行加解密,需要用到javax.crypto.*和java.security.*两个包。

①生成密钥

public static byte[] generateKey() throws Exception {
    //实例化
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    //设置密钥长度
    keyGenerator.init(128);
    //生成密钥
    SecretKey secretKey = keyGenerator.generateKey();
    //获取密钥的二进制编码形式
    byte[] keyBytes = secretKey.getEncoded();
    return keyBytes;
}

②加密

public static byte[] encrypt(byte[] data, byte[] keyBytes) throws Exception {
    //生成密钥
    SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
    //实例化Cipher对象
    Cipher cipher = Cipher.getInstance("AES");
    //设置加密模式和密钥
    cipher.init(Cipher.ENCRYPT_MODE, key);
    //加密
    byte[] encrypted = cipher.doFinal(data);
    return encrypted;
}

③解密

public static byte[] decrypt(byte[] encrypted, byte[] keyBytes) throws Exception {
    //生成密钥
    SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
    //实例化Cipher对象
    Cipher cipher = Cipher.getInstance("AES");
    //设置解密模式和密钥
    cipher.init(Cipher.DECRYPT_MODE, key);
    //解密
    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

二、非对称加密算法

非对称加密算法是指加密和解密使用不同的密钥,通常分为公钥加密和私钥解密、私钥加密和公钥解密两种方式。在Java中常用的非对称加密算法有RSA、DSA、EC等,下面以RSA算法为例进行介绍。

1. RSA加解密方法

在Java中使用RSA算法进行加解密,同样需要用到javax.crypto.*和java.security.*两个包。

①生成密钥对

public static KeyPair generateKeyPair() throws Exception {
    //实例化
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    //设置密钥长度
    keyPairGenerator.initialize(1024);
    //生成密钥对
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    return keyPair;
}

②公钥加密

public static byte[] encryptByPublicKey(byte[] data, Key publicKey) throws Exception {
    //实例化Cipher对象
    Cipher cipher = Cipher.getInstance("RSA");
    //设置加密模式和公钥
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    //加密
    byte[] encrypted = cipher.doFinal(data);
    return encrypted;
}

③私钥解密

public static byte[] decryptByPrivateKey(byte[] encrypted, Key privateKey) throws Exception {
    //实例化Cipher对象
    Cipher cipher = Cipher.getInstance("RSA");
    //设置解密模式和私钥
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    //解密
    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

三、消息摘要算法

消息摘要算法是指将任意长度的消息通过计算得到的固定长度的摘要值,用来确认消息完整性的一种技术。Java中常用的消息摘要算法有MD5、SHA-1、SHA-256等。

①生成摘要值

public static byte[] generateDigest(byte[] data, String algorithm) throws Exception {
    //实例化消息摘要对象
    MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
    //获取摘要值
    byte[] digest = messageDigest.digest(data);
    return digest;
}

四、数字签名算法

数字签名算法是指使用私钥对数据进行签名,用公钥进行验证的技术,用来确保数据的完整性和来源。Java中常用的数字签名算法有DSA、RSA等。

①生成签名

public static byte[] generateSignature(byte[] data, PrivateKey privateKey) throws Exception {
    //实例化Signature对象
    Signature signature = Signature.getInstance("SHA256withRSA");
    //设置私钥
    signature.initSign(privateKey);
    //更新数据
    signature.update(data);
    //签名
    byte[] sign = signature.sign();
    return sign;
}

②验证签名

public static boolean verifySignature(byte[] data, byte[] sign, PublicKey publicKey) throws Exception {
    //实例化Signature对象
    Signature signature = Signature.getInstance("SHA256withRSA");
    //设置公钥
    signature.initVerify(publicKey);
    //更新数据
    signature.update(data);
    //验证签名
    boolean isValid = signature.verify(sign);
    return isValid;
}

总结:

Java函数中的加密和解密方法是一种常见的保护数据机密性和完整性的技术,涵盖了对称加密算法、非对称加密算法、消息摘要算法和数字签名算法等多个方面。掌握这些方法可以有效提高开发者的数据保护能力。