如何利用 Java 方法实现数据加密

发布时间:2025-01-22 05:34:37 作者:小樊
来源:亿速云 阅读:117

在 Java 中,有多种方法可以实现数据加密。这里,我将向您介绍两种常用的加密算法:AES(高级加密标准)和 DES(数据加密标准)。

  1. 使用 AES 加密和解密数据

AES 是一种对称加密算法,它使用相同的密钥进行加密和解密。以下是一个使用 AES 加密和解密数据的示例:

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class AesEncryption {

    public static void main(String[] args) throws Exception {
        String originalData = "Hello, World!";
        String encryptedData = encrypt(originalData);
        String decryptedData = decrypt(encryptedData);

        System.out.println("Original Data: " + originalData);
        System.out.println("Encrypted Data: " + encryptedData);
        System.out.println("Decrypted Data: " + decryptedData);
    }

    public static String encrypt(String data) throws Exception {
        SecretKey secretKey = generateSecretKey();
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String encryptedData) throws Exception {
        SecretKey secretKey = generateSecretKey();
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decodedBytes = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedBytes = cipher.doFinal(decodedBytes);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }

    public static SecretKey generateSecretKey() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128); // 设置密钥长度,可以是 128、192 或 256 位
        return keyGenerator.generateKey();
    }
}
  1. 使用 DES 加密和解密数据

DES 是一种较旧的加密算法,它使用 56 位密钥进行加密和解密。以下是一个使用 DES 加密和解密数据的示例:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class DesEncryption {

    public static void main(String[] args) throws Exception {
        String originalData = "Hello, World!";
        String encryptedData = encrypt(originalData);
        String decryptedData = decrypt(encryptedData);

        System.out.println("Original Data: " + originalData);
        System.out.println("Encrypted Data: " + encryptedData);
        System.out.println("Decrypted Data: " + decryptedData);
    }

    public static String encrypt(String data) throws Exception {
        DESKeySpec desKeySpec = new DESKeySpec("12345678".getBytes(StandardCharsets.UTF_8)); // 设置密钥
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String encryptedData) throws Exception {
        DESKeySpec desKeySpec = new DESKeySpec("12345678".getBytes(StandardCharsets.UTF_8)); // 设置密钥
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decodedBytes = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedBytes = cipher.doFinal(decodedBytes);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }
}

请注意,这些示例仅用于演示目的。在实际应用中,您需要考虑更多的安全因素,例如密钥管理、加密模式(如 CBC、CFB 等)和填充方案(如 PKCS5Padding、NoPadding 等)。此外,对于更高级的加密需求,您可以考虑使用 Java 加密扩展(Java Cryptography Extension, JCE)或 Java Cryptography Architecture(JCA)。

推荐阅读:
  1. 利用JMeter的beanshell进行接口的加密处理
  2. 利用Jasypt如何对Spring Boot配置文件加密

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:Java 方法中的线程安全问题如何解决

下一篇:Java 方法的内存泄漏如何排查

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》