对称加密算法如何在Java项目中实现

发布时间:2020-11-20 14:37:42 作者:Leah
来源:亿速云 阅读:141

本篇文章为大家展示了对称加密算法如何在Java项目中实现,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

概述

采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,也称为单密钥加密。在对称加密算法中,DES算法最具有代表性,DESede是DES算法的变种,AES算法则作为DES算法的替代者。

DES

DES(Data Encryption Standard),即数据加密标准,是一种使用密钥加密的块算法,1977年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),并授权在非密级政府通信中使用,随后该算法在国际上广泛流传开来。

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class DesUtil {

  /**
   * DES加密
   * @param content 待加密数据
   * @param key 密钥
   * @return
   * @throws Exception
   */
  public static String desEncrypt(String content, String key) throws Exception {
    //指定加密算法、加密模式、填充模式
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    //创建加密规则:指定key和加密类型
    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "DES");
    //指定加密模式为加密,指定加密规则
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    //调用加密方法
    byte[] result = cipher.doFinal(content.getBytes());
    //用Base64编码
    return new String(Base64.getEncoder().encode(result));
  }

  /**
   * DES解密
   * @param content 待解密数据
   * @param key 密钥
   * @return
   * @throws Exception
   */
  public static String desDecrypt(String content, String key) throws Exception {
    //Base64解码
    byte[] result = Base64.getDecoder().decode(content);
    //指定加密算法、加密模式、填充模式
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    //创建加密规则:指定key和加密类型
    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "DES");
    //指定加密模式为解密,指定加密规则
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    return new String(cipher.doFinal(result));
  }

  public static void main(String[] args) throws Exception {
    //key要8位,不然会报错:java.security.InvalidKeyException: Wrong key size
    String key = "12345678";
    //待加密数据
    String content = "对称加密算法";

    //加密
    System.out.println(desEncrypt(content, key));//qDhh4hjbd+/TESXcV0YxC4ArDlFR1Mor

    //解密
    System.out.println(desDecrypt("qDhh4hjbd+/TESXcV0YxC4ArDlFR1Mor", key));//对称加密算法
  }
}

DESede

DESede是由DES改进后的一种对称加密算法,针对其密钥长度偏短和迭代次数偏少等问题做了相应改进,提高了安全强度。

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class DesedeUtil {

  /**
   * Desede加密
   * @param content 待加密数据
   * @param key 密钥
   * @return
   * @throws Exception
   */
  public static String desEncrypt(String content, String key) throws Exception {
    //指定加密算法、加密模式、填充模式
    Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    //创建加密规则:指定key和加密类型
    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "DESede");
    //指定加密模式为加密,指定加密规则
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    //调用加密方法
    byte[] result = cipher.doFinal(content.getBytes());
    //用Base64编码
    return new String(Base64.getEncoder().encode(result));
  }

  /**
   * Desede解密
   * @param content 待解密数据
   * @param key 密钥
   * @return
   * @throws Exception
   */
  public static String desDecrypt(String content, String key) throws Exception {
    //Base64解码
    byte[] result = Base64.getDecoder().decode(content);
    //指定加密算法、加密模式、填充模式
    Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    //创建加密规则:指定key和加密类型
    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "DESede");
    //指定加密模式为解密,指定加密规则
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    return new String(cipher.doFinal(result));
  }

  public static void main(String[] args) throws Exception {
    //key要24位,不然会报错:java.security.InvalidKeyException: Wrong key size
    String key = "123456781234567812345678";
    //待加密数据
    String content = "对称加密算法";

    //加密
    System.out.println(desEncrypt(content, key));//qDhh4hjbd+/TESXcV0YxC4ArDlFR1Mor

    //解密
    System.out.println(desDecrypt("qDhh4hjbd+/TESXcV0YxC4ArDlFR1Mor", key));//对称加密算法
  }
}

AES

AES(Advanced Encryption Standard),即高级加密标准,在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AesUtil {

  /**
   * aes加密
   * @param content 待加密数据
   * @param key 密钥
   * @return
   * @throws Exception
   */
  public static String aesEncrypt(String content, String key) throws Exception {
    //指定加密算法
    Cipher cipher = Cipher.getInstance("AES");
    //创建加密规则:指定key和加密类型
    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
    //指定加密模式为加密,指定加密规则
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    //调用加密方法
    byte[] result = cipher.doFinal(content.getBytes());
    //用Base64编码
    return new String(Base64.getEncoder().encode(result));
  }

  /**
   * aes解密
   * @param content 待解密数据
   * @param key 密钥
   * @return
   * @throws Exception
   */
  public static String aesDecrypt(String content, String key) throws Exception {
    //Base64解码
    byte[] result = Base64.getDecoder().decode(content);
    //指定加密算法
    Cipher cipher = Cipher.getInstance("AES");
    //创建加密规则:指定key和加密类型
    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
    //指定加密模式为解密,指定加密规则
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    return new String(cipher.doFinal(result));
  }

  public static void main(String[] args) throws Exception {
    //key要16/24/32位,不然会报错:java.security.InvalidKeyException: Wrong key size
    String key = "12345678123456781234567812345678";
    String content = "对称加密算法";

    //加密
    System.out.println(aesEncrypt(content, key));//yrdeR6atwBX0yeXzudk/al6q8K61gyPylX7GfwsKP9w=

    //解密
    System.out.println(aesDecrypt("yrdeR6atwBX0yeXzudk/al6q8K61gyPylX7GfwsKP9w=", key));
  }
}

上述内容就是对称加密算法如何在Java项目中实现,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Java中怎么使用DES对称加密算法
  2. 如何在java中实现DH非对称加密算法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java 对称加密算法 ava

上一篇:python cgi有什么用法

下一篇:使用Git实现删除所有在本地修改的文件的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》