【加密解密】三重数据加密3DES(Javascript实现)

三重数据加密,3DES,又称Triple DES,,是一种对称密钥加密块密码,相当于是对每个数据块应用三次数据加密标准(DES)算法。它使用3条56位的密钥对数据进行三次加密。数据加密标准(DES)是美国的一种由来已久的加密标准,它使用对称密钥加密法,并于1981年被ANSI组织规范为ANSI X.3.92。DES使用56位密钥和密码块的方法,而在密码块的方法中,文本被分成64位大小的文本块然后再进行加密。比起最初的DES,3DES更为安全。   

3DES(即Triple DES)是DES向AES过渡的加密算法(1999年,NIST将3-DES指定为过渡的加密标准),是DES的一个更安全的变形。它以DES为基本模块,通过组合分组方法设计出分组加密算法,其具体实现如下:

设Ek()和Dk()代表DES算法的加密和解密过程,K代表DES算法使用的密钥,P代表明文,C代表密文,

这样,   

3DES加密过程为:C=Ek3(Dk2(Ek1(P)))

3DES解密过程为:P=Dk1((EK2(Dk3(C)))

 

3DES使用“密钥包”,其包含3个DES密钥,K1,K2和K3,均为56位(除去奇偶校验位)。加密算法为:

密文 = EK3(DK2(EK1(平文)))

也就是说,使用K1为密钥进行DES加密,再用K2为密钥进行DES“解密”,最后以K3进行DES加密。

而解密则为其反过程:

平文 = DK1(EK2(DK3(密文)))

即以K3解密,以K2“加密”,最后以K1解密。

每次加密操作都只处理64位数据,称为一块。

无论是加密还是解密,中间一步都是前后两步的逆。这种做法提高了使用密钥选项2时的算法强度,并在使用密钥选项3时与DES兼容。

 

标准定义了三种密钥选项:

密钥选项1:三个密钥是独立的。

密钥选项2:K1和K2是独立的,而K3=K1

密钥选项3:三个密钥均相等,即K1=K2=K3

密钥选项1的强度最高,拥有3 x 56 = 168个独立的密钥位。

密钥选项2的安全性稍低,拥有2 x 56 = 112个独立的密钥位。该选项比简单的应用DES两次的强度较高,即使用K1和K2,因为它可以防御中途相遇攻击。

密钥选项3等同与DES,只有56个密钥位。这个选项提供了与DES的兼容性,因为第1和第2次DES操作相互抵消了。该选项不再为国家标准科技协会(NIST)所建议,亦不为ISO/IEC 18033-3所支持。

<form name="stuff">
  <table>
    <tbody>
      <tr>
        <td valign="top">输入信息</td>
        <td><input type="text" name="indata" size="25" value="abcdefgh"><br>
        <input type="radio" name="intype" checked="checked">ASCII</input>
        <input type="radio" name="intype">十六进制</input> </td>
      </tr>
      <tr>
        <td>DES密钥/3DES密钥A</td>
        <td><input type="text" name="key" value="3b3898371520f75e" size="25"></td>
      </tr>
      <tr>
        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/3DES密钥B</td>
        <td><input type="text" name="keyb" value="922fb510c71f436e" size="25"></td>
      </tr>
      <tr>
        <td colspan="2" align="center">
            <input type="button" value="DES加密"  onclick="do_des(true);">
            <input type="button" value="DES解密"  onclick="do_des(false);"><br>
            <input type="button" value="3DES加密" onclick="do_tdes(true);">
            <input type="button" value="3DES解密" onclick="do_tdes(false);"> </td>
      </tr>
      <tr>
        <td valign="top">输出信息</td>
        <td><input type="text" name="outdata" size="20"><br>
        <input type="radio" name="outtype" onclick="format_DES_output();">ASCII</input>
        <input type="radio" name="outtype" checked="checked" onclick="format_DES_output();">
        十六进制</input>
        </td>
      </tr>
    </tbody>
  </table>
  <hr>
细节:<br>
  <textarea name="details" id="details" rows="25" cols="90"></textarea>
</form>

java验证


  private byte[] keys = {
      0x3b,0x38,(byte)0x98,0x37, 0x15,0x20,(byte)0xf7,0x5e,
      (byte)0x92,0x2f,(byte)0xb5,0x10,(byte)0xc7,0x1f,0x43,0x6e,
      0x3b,0x38,(byte)0x98,0x37,0x15,0x20,(byte)0xf7,0x5e
  };
  // KeyGenerator 提供对称密钥生成器的功能,支持各种算法
  private KeyGenerator keygen;
  // SecretKey 负责保存对称密钥
  private SecretKey deskey;
  // Cipher负责完成加密或解密工作
  private Cipher c;
  // 该字节数组负责保存加密的结果
  private byte[] cipherByte;

  public DES3demo() throws NoSuchAlgorithmException, NoSuchPaddingException {
    Security.addProvider(new com.sun.crypto.provider.SunJCE());
//    // 实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
//    keygen = KeyGenerator.getInstance("DESede");
//    // 生成密钥
//    deskey = keygen.generateKey();
    deskey= new SecretKeySpec(keys, "DESede");
    System.out.println("密钥:" + UBytes.toHexString(deskey.getEncoded()));
    // 生成Cipher对象,指定其支持的DES算法
    c = Cipher.getInstance("DESede/ECB/NOPADDING");
  }

  /**
   * 对字符串加密
   *
   * @param str
   * @return
   * @throws InvalidKeyException
   * @throws IllegalBlockSizeException
   * @throws BadPaddingException
   */
  public byte[] Encrytor(String str) throws InvalidKeyException,
      IllegalBlockSizeException, BadPaddingException {
    // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
    c.init(Cipher.ENCRYPT_MODE, deskey);
    byte[] src = str.getBytes();
    System.out.println("明文:" + UBytes.toHexString(src));
    // 加密,结果保存进cipherByte
    cipherByte = c.doFinal(src);
    System.out.println("密文:" + UBytes.toHexString(cipherByte));
    return cipherByte;
  }

  /**
   * 对字符串解密
   *
   * @param buff
   * @return
   * @throws InvalidKeyException
   * @throws IllegalBlockSizeException
   * @throws BadPaddingException
   */
  public byte[] Decryptor(byte[] buff) throws InvalidKeyException,
      IllegalBlockSizeException, BadPaddingException {
    // 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
    c.init(Cipher.DECRYPT_MODE, deskey);
    cipherByte = c.doFinal(buff);
    return cipherByte;
  }

  /**
   * @param args
   * @throws NoSuchPaddingException
   * @throws NoSuchAlgorithmException
   * @throws BadPaddingException
   * @throws IllegalBlockSizeException
   * @throws InvalidKeyException
   */
  public static void main(String[] args) throws Exception {
    DES3demo des = new DES3demo();
    String msg ="abcdefgh";
    byte[] encontent = des.Encrytor(msg);
    byte[] decontent = des.Decryptor(encontent);
    System.out.println("解密:" + UBytes.toHexString(decontent));
    System.out.println("明文是:" + msg);
    System.out.println("加密后:" + new String(encontent));
    System.out.println("解密后:" + new String(decontent));

  }

结果:

密钥:3B 38 98 37 15 20 F7 5E 92 2F B5 10 C7 1F 43 6E 3B 38 98 37 15 20 F7 5E 
明文:61 62 63 64 65 66 67 68 
密文:EF 99 11 93 1D 8D A5 42 
解密:61 62 63 64 65 66 67 68 
明文是:abcdefgh
解密后:abcdefgh

 

转载于:https://my.oschina.net/dubenju/blog/834566

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值