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 に答える