Java 实现对称加密算法

2022-07-27,,

概述

采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,也称为单密钥加密。在对称加密算法中,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));//qdhh3hjbd+/tesxcv0yxc4ardlfr1mor

    //解密
    system.out.println(desdecrypt("qdhh3hjbd+/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));//qdhh3hjbd+/tesxcv0yxc4ardlfr1mor

    //解密
    system.out.println(desdecrypt("qdhh3hjbd+/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 实现对称加密算法的详细内容,更多关于java 对称加密算法的资料请关注其它相关文章!

《Java 实现对称加密算法.doc》

下载本文的Word格式文档,以方便收藏与打印。