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

利用Java函数实现加密和解密技术的方法解析

发布时间:2023-06-03 17:16:17

加密和解密是保护信息安全的基本技术之一。Java作为一种常用的编程语言,也拥有多种加密和解密的函数供开发者使用。本文将对Java函数实现加密和解密技术的方法进行解析。

一、对称加密

对称加密是一种加密方式,加密和解密使用同一个密钥。常见的对称加密算法有DES、3DES、AES等。

Java中对称加密的实现需要使用javax.crypto包中的Cipher类。具体实现步骤如下:

1. 创建Cipher对象:需要指定使用的算法、加密/解密模式以及填充方式。

2. 创建密钥:可以使用SecretKeyFactory类和KeyGenerator类生成密钥。

3. 初始化Cipher:使用init方法初始化Cipher,需要指定加密/解密模式和密钥。

4. 加密/解密:使用doFinal方法对数据进行加密/解密操作。

下面以AES加密为例,具体代码如下:

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AESTest {
    private static final String AES = "AES";

    /**
     * 生成AES密钥
     * @return
     * @throws Exception
     */
    public static byte[] generateAESKey() throws Exception {
        // 使用KeyGenerator生成密钥
        KeyGenerator keygen = KeyGenerator.getInstance(AES);
        // 指定密钥长度128位
        keygen.init(128);
        // 生成密钥
        SecretKey secretKey = keygen.generateKey();
        return secretKey.getEncoded();
    }

    /**
     * 加密
     * @param content 明文
     * @param key 密钥
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(byte[] content, byte[] key) throws Exception {
        // 生成密钥
        Key k = new SecretKeySpec(key, AES);
        // 创建Cipher对象
        Cipher cipher = Cipher.getInstance(AES);
        // 初始化Cipher,指定加密模式和密钥
        cipher.init(Cipher.ENCRYPT_MODE, k);
        // 加密
        return cipher.doFinal(content);
    }

    /**
     * 解密
     * @param content 密文
     * @param key 密钥
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] content, byte[] key) throws Exception {
        // 生成密钥
        Key k = new SecretKeySpec(key, AES);
        // 创建Cipher对象
        Cipher cipher = Cipher.getInstance(AES);
        // 初始化Cipher,指定解密模式和密钥
        cipher.init(Cipher.DECRYPT_MODE, k);
        // 解密
        return cipher.doFinal(content);
    }

    public static void main(String[] args) throws Exception {
        String content = "hello world";
        // 生成密钥
        byte[] key = generateAESKey();
        // 加密
        byte[] encryptResult = encrypt(content.getBytes(), key);
        System.out.println("加密后:" + new String(encryptResult));
        // 解密
        byte[] decryptResult = decrypt(encryptResult, key);
        System.out.println("解密后:" + new String(decryptResult));
    }
}

二、非对称加密

非对称加密是一种加密方式,加密和解密使用不同的密钥,其中一个密钥用于加密,另一个用于解密。常见的非对称加密算法有RSA、ECC等。

Java中非对称加密的实现需要使用java.security包中的KeyPairGenerator、KeyPair、PrivateKey、PublicKey等类。具体实现步骤如下:

1. 创建KeyPairGenerator对象:需要指定使用的算法。

2. 生成密钥对:使用generateKeyPair方法生成公钥和私钥。

3. 获取私钥和公钥:从生成的KeyPair对象中获取私钥和公钥。

4. 使用公钥加密数据:可以使用Cipher类进行加密操作。

5. 使用私钥解密数据:同样使用Cipher类进行解密操作。

下面以RSA加密为例,具体代码如下:

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.Cipher;

public class RSATest {
    private static final String RSA = "RSA";

    /**
     * 生成RSA密钥对
     * @return
     * @throws Exception
     */
    public static KeyPair generateRSAKeyPair() throws Exception {
        // 使用KeyPairGenerator生成密钥对
        KeyPairGenerator keygen = KeyPairGenerator.getInstance(RSA);
        // 指定密钥长度1024位
        keygen.initialize(1024);
        // 生成密钥对
        return keygen.generateKeyPair();
    }

    /**
     * 加密
     * @param content 明文
     * @param publicKey 公钥
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(byte[] content, PublicKey publicKey) throws Exception {
        // 创建Cipher对象,使用公钥加密
        Cipher cipher = Cipher.getInstance(RSA);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        // 加密
        return cipher.doFinal(content);
    }

    /**
     * 解密
     * @param content 密文
     * @param privateKey 私钥
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] content, PrivateKey privateKey) throws Exception {
        // 创建Cipher对象,使用私钥解密
        Cipher cipher = Cipher.getInstance(RSA);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        // 解密
        return cipher.doFinal(content);
    }

    public static void main(String[] args) throws Exception {
        String content = "hello world";
        // 生成RSA密钥对
        KeyPair keyPair = generateRSAKeyPair();
        // 获取公钥和私钥
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        // 加密
        byte[] encryptResult = encrypt(content.getBytes(), publicKey);
        System.out.println("加密后:" + new String(encryptResult));
        // 解密
        byte[] decryptResult = decrypt(encryptResult, privateKey);
        System.out.println("解密后:" + new String(decryptResult));
    }
}

三、消息摘要

消息摘要是一种单向加密方式,将任意长度的数据映射为固定长度的消息摘要,通常用于验证数据完整性。常见的消息摘要算法有MD5、SHA-1、SHA-256等。

Java中消息摘要的实现需要使用java.security包中的MessageDigest类。具体实现步骤如下:

1. 创建MessageDigest对象:需要指定使用的算法。

2. 计算消息摘要:使用digest方法计算消息摘要。

下面以MD5算法为例,具体代码如下:

import java.security.MessageDigest;

public class MD5Test {
    private static final String MD5 = "MD5";

    /**
     * 计算MD5摘要
     * @param content 明文
     * @return
     * @throws Exception
     */
    public static byte[] md5(byte[] content) throws Exception {
        // 创建MessageDigest对象,指定算法MD5
        MessageDigest md = MessageDigest.getInstance(MD5);
        // 计算消息摘要
        return md.digest(content);
    }

    public static void main(String[] args) throws Exception {
        String content = "hello world";
        // 计算MD5摘要
        byte[] md5Result = md5(content.getBytes());
        System.out.println("摘要为:" + new String(md5Result));
    }
}

综上所述,Java函数实现加密和解密技术分为对称加密、非对称加密和消息摘要三类,开发者可以针对具体业务场景选择不同的加密方式。