DES加密解密(JAVA)

来源:互联网 发布:java虚拟机经典书籍 编辑:程序博客网 时间:2024/05/03 13:27

今天在网上搜了一下DES加密算法(注释部分展开),总报如下错误:
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

最终原因是byte数组是不能强制转换成字符串的,换言之:字符串(2个字节)和byte数组在这种情况下不是互逆的;要避免这种情况,我们需要做一些修订,可以考虑将二进制数据转换成十六进制表示

在网上找了些代码,修改以后如下:

 

public class DES {
 private Key key;        //密钥
 /**
     * 根据参数生成KEY
     *
     * @param strKey 密钥字符串
     */
    public void getKey(String strKey) {
        try {
            KeyGenerator _generator = KeyGenerator.getInstance("DES");
            _generator.init(new SecureRandom(strKey.getBytes()));
            this.key = _generator.generateKey();
            _generator = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 加密String明文输入,String密文输出
     *
     * @param strMing String明文
     * @return String密文
     */
    public String getEncString(String strMing) {
        byte[] byteMi = null;
        byte[] byteMing = null;
        String strMi = "";
        try {
            /*byteMing = strMing.getBytes("UTF8");
            byteMi = this.getEncCode(byteMing);
            strMi = new String(byteMi, "UTF8");*/
         strMi = parseByte2HexStr(getEncCode(strMing.getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            byteMing = null;
            byteMi = null;
        }
        return strMi;
    }
    /**
     * 解密 以String密文输入,String明文输出
     *
     * @param strMi String密文
     * @return String明文
     */
    public String getDesString(String strMi) {
     byte[] byteMi = null;
        byte[] byteMing = null;
        String strMing = "";
        try {
         /*byteMi = strMi.getBytes("UTF8");
            byteMing = this.getDesCode(byteMi);
            strMing = new String(byteMing, "UTF8");*/
         strMing = new String(getDesCode(parseHexStr2Byte(strMi)));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            byteMing = null;
            byteMi = null;
        }
        return strMing;
    }
    /**
     * 加密以byte[]明文输入,byte[]密文输出
     *
     * @param byteS byte[]明文
     * @return byte[]密文
     */
    private  byte[] getEncCode(byte[] byteS) {
        byte[] byteFina = null;
        Cipher cipher;
        try {
            cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byteFina = cipher.doFinal(byteS);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return byteFina;
    }
    /**
     * 解密以byte[]密文输入,以byte[]明文输出
     *
     * @param byteD byte[]密文
     * @return byte[]明文
     */
    private  byte[] getDesCode(byte[] byteD) {
        Cipher cipher;
        byte[] byteFina = null;
        try {
            cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byteFina = cipher.doFinal(byteD);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return byteFina;
    }
   
    /**
     * 将二进制转换成16进制
     * @param buf
     * @return
     */
     public  String parseByte2HexStr(byte buf[]) { 
      StringBuffer sb = new StringBuffer(); 
   for (int i = 0; i < buf.length; i++) {
    String hex = Integer.toHexString(buf[i] & 0xFF);
    if (hex.length() == 1) {
     hex = '0' + hex;
    }
    sb.append(hex.toUpperCase());
   }
   return sb.toString();
     }
      
     /**
      * 将16进制转换为二进制
      * @param hexStr
      * @return
      */
  public byte[] parseHexStr2Byte(String hexStr) {
 
   if (hexStr.length() < 1)
 
    return null;
   byte[] result = new byte[hexStr.length() / 2];
   for (int i = 0; i < hexStr.length() / 2; i++) {
    int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
    int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
      16);
    result[i] = (byte) (high * 16 + low);
   }
   return result;
  }

 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  DES encrypt = new DES();  
  encrypt.getKey("嘿嘿");  
  String strEnc = encrypt.getEncString("hi,我是程序员");
  System.out.println(strEnc);
  String strDes = encrypt.getDesString(strEnc);
  System.out.println(strDes);
 }

}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 有东西进眼睛了怎么办 眼睛有东西磨眼怎么办 怀孕了眼睛肿疼怎么办 眼睛疼又红血丝怎么办 用眼过度眼睛疼怎么办 眼睛玩手机视力下降怎么办 看手机眼睛疼该怎么办 眼睛眨一下就痛怎么办 着火了怎么办教案详案 汽车尾灯磕破了怎么办 后尾灯灯罩裂了怎么办 七氟丙烷喷伤了怎么办 冒险岛2fps低怎么办 虐杀原形2很卡怎么办 玩虐杀原形2卡怎么办 虐杀原形2闪退怎么办 电脑显示不出u盘怎么办 电脑不显示u盘怎么办 u盘在电脑不显示怎么办 笔记本不识别u盘怎么办 u盘突然识别不了怎么办 xp电脑读不出u盘怎么办 电脑无法读取u盘怎么办 win7电脑不读u盘怎么办 电脑识别不出u盘怎么办 u盘电脑读不出来怎么办 u盘突然无法识别怎么办 u盘电脑无法识别怎么办 系统无法识别u盘怎么办 手机u盘无法识别怎么办 u盘无法被识别怎么办 电脑不能读取u盘怎么办 电脑装系统卡了怎么办 怀孕三个月胚胎停育怎么办 被蟑螂咬了怎么办图片 有家人进了传销怎么办 有亲人进了传销怎么办 误入传销违法了怎么办 tt游戏账号忘了怎么办 被臭蚊子咬了怎么办 狗狗鼻子掉皮了怎么办