日期:2014-05-20  浏览次数:20837 次

java 3des加密算法求解
我下载了一个3des加密的工具,得到如下结果:
原文:06111111FFFFFFFF
密钥:11111111111111111111111111111111
密文:C1335C0C5EC48958

这个也是我需要的结果,但是我用java始终没有得到这样的结果。求相关java代码,紧急,谢谢

------解决方案--------------------
我用java写的测试,生成的结果前面的8字节与你相同,只是多个8个字节,供参考:
Java code

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

public class TestDes {

    private static final String Algorithm = "DESede"; 
    
    public static byte[] encryptMode(byte[] keybyte, byte[] src) {
       try {
            
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

            
            Cipher c1 = Cipher.getInstance(Algorithm);
           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;
    }

    public static byte[] decryptMode(byte[] keybyte, byte[] src) {      
    try {
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

            Cipher c1 = Cipher.getInstance(Algorithm);
            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;
    }

    //转换成十六进制字符串
    
    public static String byte2hex(byte[] b) {
        String hs="";
        String stmp="";

        for (int n=0;n<b.length;n++) {
            stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length()==1) hs=hs+"0"+stmp;
            else hs=hs+stmp;
            if (n<b.length-1)  hs=hs+":";
        }
        return hs.toUpperCase();
    }
    
    public static void main(String[] args)
    {

         final byte[] keyBytes = { 0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11
                                   ,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11};
       byte[] szSrc = {0x06,0x11,0x11,0x11,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF};
        
        System.out.println("加密前的字符串:" + byte2hex(szSrc));
        
        byte[] encoded = encryptMode(keyBytes,szSrc );        
        System.out.println("加密后的字符串:" + byte2hex(encoded));
        
        byte[] srcBytes = decryptMode(keyBytes, encoded);
        System.out.println("解密后的字符串:" + byte2hex(srcBytes));
    }
}

------解决方案--------------------
下面的程序是改装火龙果大侠以前写的:

Java code


import java.security.Key;
import java.security.spec.KeySpec;

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

public class BcTest {
    static void test() throws Exception {
    byte[] data = { 0x06, 0x11, 0x11, 0x11, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF };

    byte[] key1 = new byte[] { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
        0x11 };
    byte[] key2 = new byte[] { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
        0x11 };
    byte[] key3 = new byte[] { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
        0x11 };

    // 3DES ciphertext = EK3(DK2(EK1(plaintext)))
    byte[] crypt = encrypt(decrypt(encrypt(data, key1), key2), key3);

    // 3DES plaintext = DK1(EK2(DK3(ciphertext)))
    byte[] plain = decrypt(encrypt(decrypt(crypt, key3), key2), key1);

    System.out.println("  key: " + ByteUtil.bytes2HexSpace(key1) + " "
        + ByteUtil.bytes2HexSpace(key2) + " "
        + ByteUtil.bytes2HexSpace(key3));
    System.out.println(" data: " + ByteUtil.bytes2HexSpace(data));
    System.out.println("crypt: " + ByteUtil.bytes2HexSpace(crypt));
    System.out.println("plain: " + ByteUtil.bytes2HexSpace(plain));

    }

    public static void main(String[] args) throws Exception {
    test();
    }

    public static byte[] decrypt(byte[] crypt, byte[] key) throws Exception {
    Key k = toKey(key);
    Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, k);
    return cipher.doFinal(crypt);
    }

    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
    Key k = toKey(key);
    Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, k);
    return cipher.doFinal(data);
    }

    public static SecretKey toKey(byte[] key) throws Exception {
    KeySpec dks = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    return keyFactory.generateSecret(dks);
    }
}

class ByteUtil {

    private static final char HEX[] = "0123456789abcdef".toCharArray();

    public static String bytes2HexSpace(byte bys[]) {
    char chs[] = new char[(bys.length * 2 + bys.length) - 1];
    int i = 0;
    int offset = 0;
    for (; i < bys.length; i++) {
        if (i > 0)
        chs[offset++] = ' ';
        chs[offset++] = HEX[bys[i] >> 4 & 15];
        chs[offset++] = HEX[bys[i] & 15];
    }
    return new String(chs);
    }
}
/*output:
key: 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
data: 06 11 11 11 ff ff ff ff
crypt: c1 33 5c 0c 5e c4 89 58
plain: 06 11 11 11 ff ff ff ff
*/