0

数据将被加密:5140000000080401AEBFFFFFFFF7FBFE

关键是:00000000000000000000000000000000

和最终结果:DBBE8A87A4E37D95B5EDDD2BE6A4151F

//SecretKey to save data
    private SecretKey desKey;

    //Complete encryption and decryption work
    private Cipher c;

    //Save encryption results
    private byte[] cipherResultByte;

    private final static String Algorithm = "DESede/ECB/NoPadding";//Encryption method/operation mode

    private static DESedeKeySpec dks;

    private static SecretKeyFactory keyFactory;   

    public DESeseTest02() {

        Security.addProvider(new com.sun.crypto.provider.SunJCE());

        try {

            dks = new DESedeKeySpec("00000000000000000000000000000000".getBytes());   

             keyFactory = SecretKeyFactory.getInstance("DESede");


            //Generate key
            desKey=keyFactory.generateSecret(dks);


            //Generate Cipher object, specify its support DES algorithm
            c=Cipher.getInstance(Algorithm);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }
    }


    public byte[] createEncryptor(byte[] b){

        try {

//          System.out.println(str);
            //根据密钥,对Cipher进行初使化,DECRYPT_MODE加密模式
            c.init(Cipher.ENCRYPT_MODE, desKey);

//          byte[] input=str.getBytes();
//          System.out.println(input.length);

            //Encryption, the results will be preserved
            cipherResultByte=c.doFinal(b);

        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return cipherResultByte;
    }

但结果并不如预期,为什么?任何人的帮助将不胜感激。

4

1 回答 1

2

代替

"00000000000000000000000000000000".getBytes() 

new byte[34]

"0".getBytes() 与 new byte[]{0} 不同

于 2013-02-27T05:13:16.860 回答