簡體   English   中英

我如何知道我使用的是Triple Des Algorithm實現的正確版本?

[英]How do I know if I'm using the right version of Triple Des Algorithm implementation?

我在互聯網上搜索了Java的Triple Des Algorithm實現。

我找到了很多解決方案,然后選擇了一種(對我來說文檔更好的解決方案)。 我測試並正常工作。

然后,我搜索了Java的AES算法實現。 並找到一個好的。 確實類似於Triple Des Algorithm的實現,但不完全相同。

因此,我認為,如果我使用AES算法實現,但將Cipher實例參數從“ AES”更改為“ DESede”,該怎么辦? 我進行了更改,測試了代碼並正常工作。 但是,返回的字符串與我先前的Triple Des Algorithm實現中返回的字符串不同。

因此,就像標題中所說的那樣,我怎么知道我是否使用的是Triple Des Algorithm實現的正確版本?

這是第一個實現:

public String encrypt(SecretKey key, String stringIn){

    String outString = "";      

    if (stringIn.isEmpty() || stringIn.toUpperCase().equals("NULL")){
        return "";
    }

    try {   

        if (key == null)
            key = this.key;


        InputStream in = new ByteArrayInputStream(stringIn.getBytes("UTF-8"));

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        // Create and initialize the encryption engine
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        // Create a special output stream to do the work for us
        CipherOutputStream cos = new CipherOutputStream(out, cipher);

        // Read from the input and write to the encrypting output stream
        byte[] buffer = new byte[2048];

        int bytesRead;

        while ((bytesRead = in.read(buffer)) != -1) {
            cos.write(buffer, 0, bytesRead);
        }

        cos.close();

        // For extra security, don't leave any plaintext hanging around memory.
        java.util.Arrays.fill(buffer, (byte) 0);

        outString = out.toString();

    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();

    } catch (NoSuchAlgorithmException e) {

        e.printStackTrace();

    } catch (NoSuchPaddingException e) {

        e.printStackTrace();

    } catch (InvalidKeyException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    } finally {

        return outString;
    }

}

這是第二個:

public String encrypt(SecretKey key, String stringIn){

    String outString = "";      

    if (stringIn.isEmpty() || stringIn.toUpperCase().equals("NULL")){
        return "";
    }


    try {   

        if (key == null)
            key = this.key;

        Cipher ecipher = Cipher.getInstance("DESede");

        ecipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] bytes = stringIn.getBytes("UTF8");

        byte[] encVal = ecipher.doFinal(bytes);

        outString = new sun.misc.BASE64Encoder().encode(encVal);

    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();

    } catch (NoSuchAlgorithmException e) {

        e.printStackTrace();

    } catch (NoSuchPaddingException e) {

        e.printStackTrace();

    } catch (InvalidKeyException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    } finally {

        return outString;
    }

}

這是測試用例:

    String In: 6985475896580019
    String Returned when I Encripted with First code: Kœ¼i …€‡ä«‘]<žéù âpU
    String Returned when I Encripted with Second code: w1ujopasjH6ccFKgUtOgansFNBtxbWe8YwDhso2pZN8=

對不起,我英語不好。

謝謝你的幫助

cipher.init(mode,key)生成隨機IV。 實際上,這是最安全的使用方式。 您應該使用.getIV()並返回帶有加密文本的文本(這也是自動的; Java將其附加到cryptostream的前幾個字節上,這是它們解密OK的方式)。 不同的IV更改結果的方式與更改不同的密鑰一樣多,但是它不必保密,只是確保相同的內容不會被相同地加密。

要強制IV進行算法比較或使用未包含的已知算法進行解密,請使用cipher.init(mode,key,new IvParameterSpec(iv))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM