当前位置:首页 > Java

java如何实现加密

2026-03-23 07:45:52Java

Java实现加密的方法

Java提供了多种加密方式,包括对称加密、非对称加密和哈希加密。以下是一些常见的加密实现方法:

java如何实现加密

对称加密(AES)

对称加密使用相同的密钥进行加密和解密。AES(Advanced Encryption Standard)是一种常用的对称加密算法。

java如何实现加密

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;

public class AESEncryption {
    public static SecretKey generateKey() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(256); // 可以使用128或192位
        return keyGenerator.generateKey();
    }

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

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

非对称加密(RSA)

非对称加密使用公钥和私钥进行加密和解密。RSA是一种常见的非对称加密算法。

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

public class RSAEncryption {
    public static KeyPair generateKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048); // 密钥长度
        return keyPairGenerator.generateKeyPair();
    }

    public static String encrypt(String plainText, java.security.PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String encryptedText, java.security.PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
        return new String(decryptedBytes);
    }
}

哈希加密(SHA-256)

哈希加密是一种单向加密,通常用于密码存储。SHA-256是一种常用的哈希算法。

import java.security.MessageDigest;
import java.util.Base64;

public class SHA256Encryption {
    public static String hash(String input) throws Exception {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hashBytes = digest.digest(input.getBytes());
        return Base64.getEncoder().encodeToString(hashBytes);
    }
}

使用示例

public class Main {
    public static void main(String[] args) throws Exception {
        // AES示例
        SecretKey aesKey = AESEncryption.generateKey();
        String aesEncrypted = AESEncryption.encrypt("Hello World", aesKey);
        String aesDecrypted = AESEncryption.decrypt(aesEncrypted, aesKey);
        System.out.println("AES Decrypted: " + aesDecrypted);

        // RSA示例
        KeyPair rsaKeyPair = RSAEncryption.generateKeyPair();
        String rsaEncrypted = RSAEncryption.encrypt("Hello World", rsaKeyPair.getPublic());
        String rsaDecrypted = RSAEncryption.decrypt(rsaEncrypted, rsaKeyPair.getPrivate());
        System.out.println("RSA Decrypted: " + rsaDecrypted);

        // SHA-256示例
        String shaHash = SHA256Encryption.hash("Hello World");
        System.out.println("SHA-256 Hash: " + shaHash);
    }
}

以上代码展示了如何在Java中实现常见的加密算法。根据具体需求选择合适的加密方式。

分享给朋友:

相关文章

vue项目如何实现

vue项目如何实现

Vue项目实现步骤 环境搭建 确保已安装Node.js和npm。使用Vue CLI创建项目: npm install -g @vue/cli vue create my-project cd my-…

如何配置java环境

如何配置java环境

下载JDK安装包 访问Oracle官网或OpenJDK官网,选择适合操作系统的JDK版本(如Windows、macOS或Linux)。下载完成后,运行安装程序并按照提示完成安装。 设置环境变量 右键…

vue如何实现id

vue如何实现id

Vue 中实现 ID 的方法 在 Vue 中,可以通过多种方式为元素或组件分配唯一的 ID。以下是几种常见的方法: 使用 v-bind 绑定 ID 通过 v-bind 动态绑定 ID,可以在模板中直…

java如何导入jar包

java如何导入jar包

在Eclipse中导入JAR包 右键项目 -> 选择"Properties" -> 左侧导航选择"Java Build Path" -> 切换到"Libraries"标签 ->…

java程序如何打包

java程序如何打包

打包Java程序的方法 使用JAR打包 Java程序可以通过JAR(Java Archive)格式打包,这是一种常见的压缩格式,适用于包含多个类文件和资源的项目。以下是使用JAR打包的步骤: 确保项…

vue如何实现级联

vue如何实现级联

实现级联选择器的基本方法 在Vue中实现级联选择器通常使用现成的组件库或自定义组件。以下是两种常见方式: 使用Element UI的Cascader组件 安装Element UI后,直接使用el-c…