全国旗舰校区

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

北京

深圳

上海

广州

郑州

大连

武汉

成都

西安

杭州

青岛

重庆

长沙

哈尔滨

南京

太原

沈阳

合肥

贵阳

济南

下一个校区
就在你家门口
+
当前位置:首页  >  技术问答  > 详情

java对称加密有哪些

匿名提问者2023-09-20

java对称加密有哪些

推荐答案

  在Java中,对称加密算法有许多选择,其中最常用的包括DES、AES和DESede。下面我会详细介绍每个算法的操作方法。

Java教程

  1.DES(Data Encryption Standard):DES是一种基于56位密钥长度的对称加密算法。下面是使用Java进行DES加密和解密的示例代码:

  import javax.crypto.Cipher;

  import javax.crypto.SecretKey;

  import javax.crypto.SecretKeyFactory;

  import javax.crypto.spec.DESKeySpec;

  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);

  DESKeySpec desKeySpec = new DESKeySpec(keyData);

  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

  SecretKey desKey = keyFactory.generateSecret(desKeySpec);

  // 创建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进行AES加密和解密的示例代码:

  import javax.crypto.Cipher;

  import javax.crypto.SecretKey;

  import javax.crypto.SecretKeyFactory;

  import javax.crypto.spec.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);

  SecretKeySpec aesKeySpec = new SecretKeySpec(keyData, "AES");

  // 创建AES加密对象

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

  // 初始化加密模式

  cipher.init(Cipher.ENCRYPT_MODE, aesKeySpec);

  // 加密数据

  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, aesKeySpec);

  // 解密数据

  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进行DESede加密和解密的示例代码:

  import javax.crypto.Cipher;

  import javax.crypto.SecretKey;

  import javax.crypto.SecretKeyFactory;

  import javax.crypto.spec.DESedeKeySpec;

  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);

  DESedeKeySpec desedeKeySpec = new DESedeKeySpec(keyData);

  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");

  SecretKey desedeKey = keyFactory.generateSecret(desedeKeySpec);

  // 创建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进行对称加密的示例代码。为了确保数据的安全性,请谨慎保管密钥,并采用适当的密钥管理策略。

相关问答

java分布式缓存的操作方法

java动态参数列表的操作方法

java对称加密有哪些

Java连接数据库的操作方法

java获取环境变量配置方法

开班信息 更多>>

课程名称
全部学科
咨询

HTML5大前端

Java分布式开发

Python数据分析

Linux运维+云计算

全栈软件测试

大数据+数据智能

智能物联网+嵌入式

网络安全

全链路UI/UE设计

Unity游戏开发

新媒体短视频直播电商

影视剪辑包装

游戏原画

    在线咨询 免费试学 教程领取