21xrx.com
2025-03-29 06:04:04 Saturday
文章检索 我的文章 写文章
Java加密算法大全:常用的加密算法详解及示例代码
2023-06-13 01:47:14 深夜i     15     0
Java加密算法 对称加密 非对称加密 消息摘要

在信息安全领域中,加密算法尤为重要,其中Java加密算法也是相对较常用的一种。本文将详细介绍Java中常用的加密算法及其示例代码。

1. 对称加密算法

对称加密算法是最简单也最常见的加密算法,它使用相同的密钥对数据进行加密和解密。常用的对称加密算法有DES、3DES、AES等。下面是使用AES对称加密算法实现加解密的示例代码:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AesUtil {
  private static final String ALGORITHM = "AES";
  private static final String KEY = "mykey123";
  public static String encrypt(String value) throws Exception {
    SecretKeySpec spec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, spec);
    byte[] encryptedValue = cipher.doFinal(value.getBytes());
    return Base64.getEncoder().encodeToString(encryptedValue);
  }
  public static String decrypt(String encryptedValue) throws Exception {
    SecretKeySpec spec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, spec);
    byte[] decodedValue = Base64.getDecoder().decode(encryptedValue);
    byte[] decryptedValue = cipher.doFinal(decodedValue);
    return new String(decryptedValue);
  }
  public static void main(String[] args) throws Exception {
    String value = "Hello, world!";
    String encryptedValue = encrypt(value);
    String decryptedValue = decrypt(encryptedValue);
    System.out.println("Original value: " + value);
    System.out.println("Encrypted value: " + encryptedValue);
    System.out.println("Decrypted value: " + decryptedValue);
  }
}

2. 非对称加密算法

非对称加密算法使用一对不同的密钥对数据进行加密和解密,常用的非对称加密算法有RSA、DSA等。下面是使用RSA非对称加密算法实现加解密的示例代码:

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.util.Base64;
public class RsaUtil {
  private static final String ALGORITHM = "RSA";
  public static KeyPair generateKeyPair() throws Exception {
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM);
    keyPairGen.initialize(2048);
    return keyPairGen.generateKeyPair();
  }
  public static String sign(byte[] data, PrivateKey privateKey) throws Exception {
    Signature signature = Signature.getInstance("SHA256withRSA");
    signature.initSign(privateKey);
    signature.update(data);
    byte[] signData = signature.sign();
    return Base64.getEncoder().encodeToString(signData);
  }
  public static boolean verify(byte[] data, PublicKey publicKey, String sign) throws Exception {
    Signature signature = Signature.getInstance("SHA256withRSA");
    signature.initVerify(publicKey);
    signature.update(data);
    byte[] signData = Base64.getDecoder().decode(sign);
    return signature.verify(signData);
  }
  public static void main(String[] args) throws Exception {
    String data = "Hello, world!";
    KeyPair keyPair = generateKeyPair();
    PrivateKey privateKey = keyPair.getPrivate();
    PublicKey publicKey = keyPair.getPublic();
    String sign = sign(data.getBytes(), privateKey);
    boolean verifyResult = verify(data.getBytes(), publicKey, sign);
    System.out.println("Original data: " + data);
    System.out.println("Sign data: " + sign);
    System.out.println("Verify result: " + verifyResult);
  }
}

3. 消息摘要算法

消息摘要算法是将任意长度的消息通过摘要算法转换成固定长度的摘要值的算法,常用的消息摘要算法有MD5、SHA-1、SHA-256等。下面是使用SHA-256消息摘要算法实现摘要计算的示例代码:

import java.security.MessageDigest;
import java.util.Base64;
public class DigestUtil {
  private static final String ALGORITHM = "SHA-256";
  public static String digest(String value) throws Exception {
    MessageDigest md = MessageDigest.getInstance(ALGORITHM);
    byte[] encodedValue = md.digest(value.getBytes());
    return Base64.getEncoder().encodeToString(encodedValue);
  }
  public static void main(String[] args) throws Exception {
    String value = "Hello, world!";
    String digestValue = digest(value);
    System.out.println("Original value: " + value);
    System.out.println("Digest value: " + digestValue);
  }
}

  
  

评论区

请求出错了