全国旗舰校区

不同学习城市 同样授课品质

北京

深圳

上海

广州

郑州

大连

武汉

成都

西安

杭州

青岛

重庆

长沙

哈尔滨

南京

太原

沈阳

合肥

贵阳

济南

下一个校区
就在你家门口
+
当前位置:首页  >  千锋问问

java对称加密有哪些怎么操作

问题描述:java对称加密有哪些怎么操作

推荐答案 本回答由问问达人推荐

  Java中提供了多种对称加密算法,常用的有DES、AES和DESede。下面我将介绍这些算法的使用方法。

千锋教育

  1.DES(Data Encryption Standard):DES是一种对称加密算法,密钥长度固定为56位。Java中可以使用javax.crypto包中的Cipher类进行DES加密。

  import javax.crypto.Cipher;

  import javax.crypto.SecretKey;

  import javax.crypto.SecretKeySpec;

  import java.nio.charset.StandardCharsets;

  import java.security.Key;

  import java.util.Base64;

  public class DESEncryptionExample {

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

  // 生成DES密钥

  String keyString = "your_key";

  byte[] keyData = keyString.getBytes(StandardCharsets.UTF_8);

  Key desKey = new SecretKeySpec(keyData, "DES");

  // 创建DES加密对象

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

  // 初始化加密模式

  cipher.init(Cipher.ENCRYPT_MODE, desKey);

  // 加密数据

  String plaintext = "Hello, World!";

  byte[] encryptedData = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));

  // 对加密数据进行Base64编码

  String encryptedText = Base64.getEncoder().encodeToString(encryptedData);

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

  // 初始化解密模式

  cipher.init(Cipher.DECRYPT_MODE, desKey);

  // 解密数据

  byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedText));

  // 输出解密结果

  String decryptedText = new String(decryptedData, StandardCharsets.UTF_8);

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

  }

  }

  2.AES(Advanced Encryption Standard):AES是一种高级加密标准,密钥长度可以是128、192或256位。Java中同样可以使用javax.crypto包中的Cipher类进行AES加密。

  import javax.crypto.Cipher;

  import javax.crypto.SecretKey;

  import javax.crypto.SecretKeySpec;

  import java.nio.charset.StandardCharsets;

  import java.security.Key;

  import java.util.Base64;

  public class AESEncryptionExample {

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

  // 生成AES密钥

  String keyString = "your_key";

  byte[] keyData = keyString.getBytes(StandardCharsets.UTF_8);

  Key aesKey = new SecretKeySpec(keyData, "AES");

  // 创建AES加密对象

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

  // 初始化加密模式

  cipher.init(Cipher.ENCRYPT_MODE, aesKey);

  // 加密数据

  String plaintext = "Hello, World!";

  byte[] encryptedData = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));

  // 对加密数据进行Base64编码

  String encryptedText = Base64.getEncoder().encodeToString(encryptedData);

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

  // 初始化解密模式

  cipher.init(Cipher.DECRYPT_MODE, aesKey);

  // 解密数据

  byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedText));

  // 输出解密结果

  String decryptedText = new String(decryptedData, StandardCharsets.UTF_8);

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

  }

  }

  3.DESede(Triple DES):DESede是对称加密算法的一种,使用3个不同的密钥对数据进行加密。Java中同样可以使用javax.crypto包中的Cipher类进行DESede加密。

  import javax.crypto.Cipher;

  import javax.crypto.SecretKey;

  import javax.crypto.SecretKeySpec;

  import java.nio.charset.StandardCharsets;

  import java.security.Key;

  import java.util.Base64;

  public class DESedeEncryptionExample {

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

  // 生成DESede密钥

  String keyString = "your_key";

  byte[] keyData = keyString.getBytes(StandardCharsets.UTF_8);

  Key desedeKey = new SecretKeySpec(keyData, "DESede");

  // 创建DESede加密对象

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

  // 初始化加密模式

  cipher.init(Cipher.ENCRYPT_MODE, desedeKey);

  // 加密数据

  String plaintext = "Hello, World!";

  byte[] encryptedData = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));

  // 对加密数据进行Base64编码

  String encryptedText = Base64.getEncoder().encodeToString(encryptedData);

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

  // 初始化解密模式

  cipher.init(Cipher.DECRYPT_MODE, desedeKey);

  // 解密数据

  byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedText));

  // 输出解密结果

  String decryptedText = new String(decryptedData, StandardCharsets.UTF_8);

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

  }

  }

  以上是使用Java进行对称加密的示例。请注意,在实际应用中,保证密钥的安全性非常重要。对称加密算法通常用于加密较小的数据,如果需要加密大量数据或保证更高的安全性,可以考虑使用混合加密方案,结合非对称加密算法进行密钥交换和数据加密。

查看其它两个剩余回答
在线咨询 免费试学 教程领取