21xrx.com
2024-11-08 21:06:20 Friday
登录
文章检索 我的文章 写文章
作为一个Java开发者
2023-06-11 02:58:34 深夜i     --     --
Java 加密 代码实例

作为一个Java开发者,我们时常需要对敏感数据进行加密以保障其安全性。本文将介绍Java中几种常见的加密方式,并提供代码实例,帮助我们更好地理解和使用它们。

1. 对称加密

对称加密是指加密和解密过程使用相同密钥的加密方式。常见的对称加密算法包括DES、AES等。我们可以通过以下代码来使用Java中的AES加密算法加密一个字符串:


import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

import java.util.Base64;

public class AESEncryption {

  public static String encrypt(String key, String plainText) throws Exception {

    SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");

    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

    cipher.init(Cipher.ENCRYPT_MODE, secretKey);

    byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());

    return Base64.getEncoder().encodeToString(encryptedBytes);

  }

  public static String decrypt(String key, String encryptedText) throws Exception {

    SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");

    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

    cipher.init(Cipher.DECRYPT_MODE, secretKey);

    byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);

    byte[] decryptedBytes = cipher.doFinal(decodedBytes);

    return new String(decryptedBytes);

  }

  public static void main(String[] args) throws Exception {

    String plainText = "Hello World!";

    String key = "ThisIsASecretKey";

    String encryptedText = encrypt(key, plainText);

    System.out.println("Encrypted Text: " + encryptedText);

    System.out.println("Decrypted Text: " + decrypt(key, encryptedText));

  }

}

2. 非对称加密

非对称加密是指加密和解密过程使用不同密钥的加密方式。常见的非对称加密算法包括RSA、DSA等。我们可以通过以下代码来使用Java中的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 RSAEncryption {

  public static void main(String[] args) throws Exception {

    String plainText = "Hello World!";

    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");

    keyPairGenerator.initialize(2048);

    KeyPair keyPair = keyPairGenerator.generateKeyPair();

    PublicKey publicKey = keyPair.getPublic();

    PrivateKey privateKey = keyPair.getPrivate();

    String encryptedText = encrypt(publicKey, plainText);

    System.out.println("Encrypted Text: " + encryptedText);

    String decryptedText = decrypt(privateKey, encryptedText);

    System.out.println("Decrypted Text: " + decryptedText);

    String signature = sign(privateKey, plainText);

    System.out.println("Signature: " + signature);

    System.out.println("Is Signature Valid: " + verify(publicKey, plainText, signature));

  }

  public static String encrypt(PublicKey publicKey, String plainText) 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(PrivateKey privateKey, String encryptedText) throws Exception {

    Cipher cipher = Cipher.getInstance("RSA");

    cipher.init(Cipher.DECRYPT_MODE, privateKey);

    byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);

    byte[] decryptedBytes = cipher.doFinal(decodedBytes);

    return new String(decryptedBytes);

  }

  public static String sign(PrivateKey privateKey, String plainText) throws Exception {

    Signature signature = Signature.getInstance("SHA256withRSA");

    signature.initSign(privateKey);

    signature.update(plainText.getBytes());

    byte[] signedBytes = signature.sign();

    return Base64.getEncoder().encodeToString(signedBytes);

  }

  public static boolean verify(PublicKey publicKey, String plainText, String signature) throws Exception {

    Signature sig = Signature.getInstance("SHA256withRSA");

    sig.initVerify(publicKey);

    sig.update(plainText.getBytes());

    byte[] decodedBytes = Base64.getDecoder().decode(signature);

    return sig.verify(decodedBytes);

  }

}

3. 消息摘要

消息摘要是指提取一段数据的唯一指纹信息,以便于保证其完整性和一致性。常见的消息摘要算法包括MD5、SHA1、SHA256等。我们可以通过以下代码来使用Java中的SHA256算法生成一个消息摘要:


import java.nio.charset.StandardCharsets;

import java.security.MessageDigest;

import java.util.Base64;

public class SHA256Digest {

  public static void main(String[] args) throws Exception {

    String plainText = "Hello World!";

    String digest = generateDigest(plainText);

    System.out.println("Digest: " + digest);

  }

  public static String generateDigest(String plainText) throws Exception {

    MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");

    byte[] digestBytes = messageDigest.digest(plainText.getBytes(StandardCharsets.UTF_8));

    return Base64.getEncoder().encodeToString(digestBytes);

  }

}

综上所述,本文介绍了Java中几种常见的加密方式:对称加密、非对称加密和消息摘要,并提供了相应的代码实例,希望能对Java开发者在对数据进行加密处理时有所启发。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复