Chinaunix首页 | 论坛 | 博客
  • 博客访问: 427374
  • 博文数量: 79
  • 博客积分: 2886
  • 博客等级: 少校
  • 技术积分: 968
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-16 10:33
文章分类

全部博文(79)

文章存档

2013年(7)

2012年(17)

2011年(28)

2010年(25)

2009年(1)

2008年(1)

我的朋友

分类: Java

2010-04-27 15:40:23

 
1.what's DES
   Definition:对称算法,数据加密标准,速度较快,适用于加密大量数据的场合。
2.what's 3DES
   Definition:基于DES的对称算法,对一块数据用三个不同的密钥进行三次加密,强度更高。
   Algorithm:

Triple DES uses a "key bundle" which comprises three DES , K1, K2 and K3, each of 56 bits (excluding ). The encryption algorithm is:

ciphertext = EK3(DK2(EK1(plaintext)))

I.e., DES encrypt with K1, DES decrypt with K2, then DES encrypt with K3.

Decryption is the reverse:

plaintext = DK1(EK2(DK3(ciphertext)))

I.e., decrypt with K3, encrypt with K2, then decrypt with K1.

Each triple encryption encrypts of 64 bits of data.

The standards define three keying options:

  • Keying option 1: All three keys are independent.
  • Keying option 2: K1 and K2 are independent, and K3 = K1.
  • Keying option 3: All three keys are identical, i.e. K1 = K2 = K3.

eg:

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;

public class Des {
   
    private static final String Algorithm = "DESede";
    public static final byte[] keyBytes = {0x11, 0x22, 0x4F, 0x58, (byte)0x88, 0x10, 0x40, 0x38
                , 0x28, 0x25, 0x79, 0x51, (byte)0xCB, (byte)0xDD, 0x55, 0x66
                , 0x77, 0x29, 0x74, (byte)0x98, 0x30, 0x40, 0x36, (byte)0xE2};  
    //24字节的密钥
   
   /*
     * @ use DESede algorithm to encrpty the src
     * @ keybyte: secretkey 24 byte
     * @ src:the text needs to be encrypt
     * @ return the enc result
     */
    public static byte[] encryptMode (byte[] keybyte, byte[] src) {
        try {
            SecretKey deskey = new SecretKeySpec (keybyte, Algorithm);
            Cipher c1 = Cipher.getInstance ("DESede/ECB/PKCS5Padding");
            c1.init (Cipher.ENCRYPT_MODE, deskey);
            return c1.doFinal (src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace ();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace ();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace ();
        }
        return null;
    }
   
    /*
     * @ use DESede algorithm to decrypt the src
     * @ keybyte: secretkey 24 byte
     * @ src:the text needs to be dec
     * @ return the dec result
     */
    public static byte[] decryptMode (byte[] keybyte, byte[] src) {
        try {
            SecretKey deskey = new SecretKeySpec (keybyte, Algorithm);
            Cipher c1 = Cipher.getInstance ("DESede/ECB/PKCS5Padding");
            c1.init (Cipher.DECRYPT_MODE, deskey);
            return c1.doFinal (src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace ();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace ();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace ();
        }
        return null;
    }

 
阅读(554) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~