21xrx.com
2025-06-23 16:31:29 Monday
文章检索 我的文章 写文章
Java加密的多种方法
2023-06-15 11:32:49 深夜i     12     0
Java加密 对称加密 非对称加密 散列算法

我最近在学习Java加密,发现Java提供了多种加密方式,我在这里分享一些常用的方法。

1. 对称加密:对称加密算法就是加密密钥与解密密钥相同的加密算法。Java提供了多种对称加密的方式,常用的是AES加密。

下面是一个简单的AES加密示例代码:

import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
public class AESUtil {
  private static Key generateKey(String password) throws Exception {
    KeyGenerator generator = KeyGenerator.getInstance("AES");
    SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
    secureRandom.setSeed(password.getBytes());
    generator.init(128, secureRandom);
    return generator.generateKey();
  }
  public static String encrypt(String password, String message) throws Exception {
    Key key = generateKey(password);
    byte[] encodedMessage = message.getBytes(StandardCharsets.UTF_8);
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] encryptedMessage = cipher.doFinal(encodedMessage);
    return new String(encryptedMessage, StandardCharsets.ISO_8859_1);
  }
  public static String decrypt(String password, String encryptedMessage) throws Exception {
    Key key = generateKey(password);
    byte[] encodedEncryptedMessage = encryptedMessage.getBytes(StandardCharsets.ISO_8859_1);
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] decryptedMessage = cipher.doFinal(encodedEncryptedMessage);
    return new String(decryptedMessage, StandardCharsets.UTF_8);
  }
}

在上面的代码中,我使用了AES算法实现了加密和解密方法。

2. 非对称加密:非对称加密算法是一种加密算法,它的加密密钥和解密密钥是不相同的。常用的非对称加密算法是RSA算法。

下面是一个简单的RSA加密示例代码:

import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
public class RSAUtil {
  public static KeyPair generateKeyPair() throws Exception {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    keyPairGenerator.initialize(1024);
    return keyPairGenerator.generateKeyPair();
  }
  public static String encrypt(String message, PublicKey publicKey) throws Exception {
    byte[] encodedMessage = message.getBytes(StandardCharsets.UTF_8);
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte[] encryptedMessage = cipher.doFinal(encodedMessage);
    return new String(encryptedMessage, StandardCharsets.ISO_8859_1);
  }
  public static String decrypt(String message, PrivateKey privateKey) throws Exception {
    byte[] encodedEncryptedMessage = message.getBytes(StandardCharsets.ISO_8859_1);
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    byte[] decryptedMessage = cipher.doFinal(encodedEncryptedMessage);
    return new String(decryptedMessage, StandardCharsets.UTF_8);
  }
}

在上面的代码中,我使用了RSA算法实现了加密和解密方法。

3. 散列算法:散列算法是一种单向加密算法,它将任意长度的数据映射为固定长度的输出。Java提供了多种散列算法,常用的是SHA系列算法。

下面是一个简单的SHA-256散列示例代码:

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
public class SHA256Util {
  public static String hash(String message) throws Exception {
    MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
    messageDigest.update(message.getBytes(StandardCharsets.UTF_8));
    byte[] digest = messageDigest.digest();
    StringBuilder stringBuilder = new StringBuilder();
    for (byte b : digest) {
      stringBuilder.append(String.format("%02x", b));
    }
    return stringBuilder.toString();
  }
}

在上面的代码中,我使用了SHA-256算法实现了散列方法。

综上所述,Java提供了多种加密方式,我们可以根据需要选择合适的算法来保护我们的数据安全。

  
  

评论区