zoukankan      html  css  js  c++  java
  • DES加密

    DES是一种对称加密算法

    英文名:Data Encrption Standard(数据加密标准)

    DES算法的加密与解密都是使用同一个密钥,密钥的长度为8个字节

    如下为加密算法:

    
    
    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;
    import javax.crypto.spec.IvParameterSpec;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    import java.net.URLDecoder;
    import java.net.URLEncoder;

    /**
    * 加密 * * @param inputContent 要加密的内容 * @param inputKey 加密用的KEY * @return * @throws Exception */ private static String encryptProcess(String inputContent, String inputKey) throws Exception { DESKeySpec desKeySpec = new DESKeySpec(inputKey.getBytes("UTF-8")); SecretKey secretKey = SecretKeyFactory.getInstance("DES").generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(inputKey.getBytes("UTF-8")); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); byte[] encodedBytes = cipher.doFinal(URLEncoder.encode(inputContent, "UTF-8").getBytes("UTF-8")); StringBuffer buffer = new StringBuffer(); for (byte b : encodedBytes) { String plainText = Integer.toHexString(0xff & b); if (plainText.length() < 2) plainText = "0" + plainText; buffer.append(plainText); } return buffer.toString(); }

    扩展:3DES (Triple DES: 三重数据加密算法),对每个数据块应用3次DES加密算法(使用3条56位的密钥对数据进行三次加密)

    3DES是DES向AES过渡时期的一种算法,在Java代码中,算法的String是"DESede",如:

    Cipher.getInstance("DESede");

  • 相关阅读:
    “嫦娥一号”探月卫星成功发射
    优化SQL Server数据库查询(转)
    虚拟网络连接设置
    字符串分割自定义函数(SQL)
    做程序的劫客
    Linux学习笔记12我的第一个C程序
    C#学习笔记——25个经典问题
    C#学习笔记——回调机制
    C#学习笔记——TCP通讯
    halcon学习笔记——实例篇(2)长度和角度测量
  • 原文地址:https://www.cnblogs.com/shuada/p/7016396.html
Copyright © 2011-2022 走看看