Java 中常见的加密算法,DES、3DES、AES、RSA

加密算法是一种将数据转换为不可读形式的算法,以保护数据的机密性和完整性。加密算法被广泛应用于计算机网络、数据库、电子商务等领域,用于保护敏感数据的安全性,如用户密码、信用卡信息、医疗记录等。在 Java 中,有许多常见的加密算法,本文将对加密算法的基本概念和常见的加密算法进行介绍,并附上代码示例。

在这里插入图片描述

加密算法的基本概念

加密算法是一种将明文转换为密文的算法。明文是指未经加密的数据,而密文是指经过加密算法处理后的不可读数据。加密算法可以分为对称加密算法和非对称加密算法两类。

对称加密算法是指使用同一个密钥进行加密和解密,常见的对称加密算法有 DES、3DES、AES 等。对称加密算法的优点是加密和解密速度快,适用于大数据量的加密和解密。但是,对称加密算法的缺点是密钥需要在加密和解密双方之间共享,密钥的安全性无法得到保障。

非对称加密算法是指使用不同的密钥进行加密和解密,常见的非对称加密算法有 RSA、DSA 等。非对称加密算法的优点是密钥可以在不同的实体之间共享,密钥的安全性可以得到保障。但是,非对称加密算法的缺点是加密和解密速度较慢,适用于小数据量的加密和解密。

Java 中常见的加密算法

DES

DES(Data Encryption Standard)是一种对称加密算法,它使用 56 位密钥将 64 位明文加密为 64 位密文。DES 算法已经被证明不够安全,因此已经被 AES 算法所取代。在 Java 中,可以使用 javax.crypto 包中的类来实现 DES 加密和解密。

下面是使用 DES 加密和解密的示例代码:

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class DESUtil {
    private static final String KEY_ALGORITHM = "DES";
    private static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";

    /**
     * 生成 DES 密钥
     */
    public static byte[] generateKey() throws NoSuchAlgorithmException {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
        keyGenerator.init(new SecureRandom());
        SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }

    /**
     * 使用 DES 加密数据
     */
    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        return cipher.doFinal(data);
    }

    /**
     * 使用 DES 解密数据
     */
    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        return cipher.doFinal(data);
    }
}

使用示例:

public class Main {
    public static void main(String[] args) throws Exception {
        // 生成 DES 密钥
        byte[] key = DESUtil.generateKey();

        // 明文
        byte[] plainText = "Hello, world!".getBytes();

        // 使用 DES 加密数据
        byte[] cipherText = DESUtil.encrypt(plainText, key);

        // 使用 DES 解密数据
        byte[] decryptedText = DESUtil.decrypt(cipherText, key);

        // 输出结果
        System.out.println("明文:" + new String(plainText));
        System.out.println("密文:" + new String(cipherText));
        System.out.println("解密后的明文:" + new String(decryptedText));
    }
}

3DES

3DES(Triple DES)是一种对称加密算法,它使用三个 56 位的密钥对数据进行加密和解密。3DES 算法比 DES 算法更安全,但是加密和解密速度较慢。在 Java 中,可以使用 javax.crypto 包中的类来实现 3DES 加密和解密。

下面是使用 3DES 加密和解密的示例代码:

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class TripleDESUtil {
    private static final String KEY_ALGORITHM = "DESede";
    private static final String CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";

    /**
     * 生成 3DES 密钥
     */
    public static byte[] generateKey() throws NoSuchAlgorithmException {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
        keyGenerator.init(new SecureRandom());
        SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }

    /**
     * 使用 3DES 加密数据
     */
    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        return cipher.doFinal(data);
    }

    /**
     * 使用 3DES 解密数据
     */
    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        return cipher.doFinal(data);
    }
}

使用示例:

public class Main {
    public static void main(String[] args) throws Exception {
        // 生成 3DES 密钥
        byte[] key = TripleDESUtil.generateKey();

        // 明文
        byte[] plainText = "Hello, world!".getBytes();

        // 使用 3DES 加密数据
        byte[] cipherText = TripleDESUtil.encrypt(plainText, key);

        // 使用 3DES 解密数据
        byte[] decryptedText = TripleDESUtil.decrypt(cipherText, key);

        // 输出结果
        System.out.println("明文:" + new String(plainText));
        System.out.println("密文:" + new String(cipherText));
        System.out.println("解密后的明文:" + new String(decryptedText));
    }
}

AES

AES(Advanced Encryption Standard)是一种对称加密算法,它使用 128、192 或 256 位密钥将数据加密为密文。AES 算法比 DES 和 3DES 算法更安全,加密和解密速度也更快。在 Java 中,可以使用 javax.crypto 包中的类来实现 AES 加密和解密。

下面是使用 AES 加密和解密的示例代码:

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class AESUtil {
    private static final String KEY_ALGORITHM = "AES";
    private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    /**
     * 生成 AES 密钥
     */
    public static byte[] generateKey(int keySize) throws NoSuchAlgorithmException {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
        keyGenerator.init(keySize, new SecureRandom());
        SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }

    /**
     * 使用 AES 加密数据
     */
    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        return cipher.doFinal(data);
    }

    /**
     * 使用 AES 解密数据
     */
    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
return cipher.doFinal(data);
}
}

使用示例:

public class Main {
    public static void main(String[] args) throws Exception {
        // 生成 AES 密钥
        byte[] key = AESUtil.generateKey(128);

        // 明文
        byte[] plainText = "Hello, world!".getBytes();

        // 使用 AES 加密数据
        byte[] cipherText = AESUtil.encrypt(plainText, key);

        // 使用 AES 解密数据
        byte[] decryptedText = AESUtil.decrypt(cipherText, key);

        // 输出结果
        System.out.println("明文:" + new String(plainText));
        System.out.println("密文:" + new String(cipherText));
        System.out.println("解密后的明文:" + new String(decryptedText));
    }
}

RSA

RSA 是一种非对称加密算法,它使用公钥和私钥对数据进行加密和解密。公钥可以公开,任何人都可以使用公钥加密数据,但是只有持有私钥的人才能够解密数据。在 Java 中,可以使用 java.security 包中的类来实现 RSA 加密和解密。

下面是使用 RSA 加密和解密的示例代码:

import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;

public class RSAUtil {
    private static final String KEY_ALGORITHM = "RSA";
    private static final int KEY_SIZE = 1024;

    /**
     * 生成 RSA 密钥对
     */
    public static KeyPair generateKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
        keyPairGenerator.initialize(KEY_SIZE);
        return keyPairGenerator.generateKeyPair();
    }

    /**
     * 使用公钥加密数据
     */
    public static byte[] encrypt(byte[] data, byte[] publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, KeyUtil.getPublicKey(publicKey));
        return cipher.doFinal(data);
    }

    /**
     * 使用私钥解密数据
     */
    public static byte[] decrypt(byte[] data, byte[] privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, KeyUtil.getPrivateKey(privateKey));
        return cipher.doFinal(data);
    }
}

import java.security.Key;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class KeyUtil {
    private static final String KEY_ALGORITHM = "RSA";

    /**
     * 将公钥的字节数组转换为 Key 对象
     */
    public static Key getPublicKey(byte[] publicKey) throws Exception {
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        return keyFactory.generatePublic(keySpec);
    }

    /**
     * 将私钥的字节数组转换为 Key 对象
     */
    public static Key getPrivateKey(byte[] privateKey) throws Exception {
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        return keyFactory.generatePrivate(keySpec);
    }
}

使用示例:

public class Main {
    public static void main(String[] args) throws Exception {
        // 生成 RSA 密钥对
        KeyPair keyPair = RSAUtil.generateKeyPair();
        byte[] publicKey = keyPair.getPublic().getEncoded();
        byte[] privateKey = keyPair.getPrivate().getEncoded();

        // 明文
        byte[] plainText = "Hello, world!".getBytes();

        // 使用公钥加密数据
        byte[] cipherText = RSAUtil.encrypt(plainText, publicKey);

        // 使用私钥解密数据
        byte[] decryptedText = RSAUtil.decrypt(cipherText, privateKey);

        // 输出结果
        System.out.println("明文:" + new String(plainText));
        System.out.println("密文:" + new String(cipherText));
        System.out.println("解密后的明文:" + new String(decryptedText));
    }
}

以上是 Java 中常见的加密算法示例代码,这些算法在实际应用中都有广泛的应用。需要注意的是,在使用加密算法时需要注意密钥的安全性,以免被攻击者窃取或猜测密钥,导致数据泄露或被篡改。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT徐师兄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值