Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我如何知道我';m使用正确版本的三重Des算法实现?_Java_Encryption_Tripledes - Fatal编程技术网

Java 我如何知道我';m使用正确版本的三重Des算法实现?

Java 我如何知道我';m使用正确版本的三重Des算法实现?,java,encryption,tripledes,Java,Encryption,Tripledes,我在互联网上搜索了Java的三重Des算法实现 我找到了很多解决方案,并选择了一个(对我来说有更好的文档)。 我测试过了,工作正常 然后,我搜索了一个用于Java的AES算法实现。找到了一个好的。与三重Des算法实现非常相似,但不完全相同 所以我想,如果我使用AES算法实现,但将密码实例参数从“AES”更改为“DESede”,会有什么附加内容? 我做了更改,测试了代码,工作正常。但是,返回的字符串与我以前的Triple-Des算法实现中返回的字符串不同 那么,就像标题所说的,我如何知道我是否使用

我在互联网上搜索了Java的三重Des算法实现

我找到了很多解决方案,并选择了一个(对我来说有更好的文档)。 我测试过了,工作正常

然后,我搜索了一个用于Java的AES算法实现。找到了一个好的。与三重Des算法实现非常相似,但不完全相同

所以我想,如果我使用AES算法实现,但将密码实例参数从“AES”更改为“DESede”,会有什么附加内容? 我做了更改,测试了代码,工作正常。但是,返回的字符串与我以前的Triple-Des算法实现中返回的字符串不同

那么,就像标题所说的,我如何知道我是否使用了三重Des算法实现的正确版本

这是第一次实施:

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=
字符串输入:6985475896580019
当我用第一个代码加密时返回的字符串:Kœ¼I…“”“-25ä«”]
密码。init(模式,密钥)
生成一个随机IV。这实际上是最安全的使用方法;您应该使用
.getIV()
并将其与加密文本一起返回(这也是自动的;Java将其固定在加密流的前几个字节上,这就是它们解密OK的方式)。不同的IV和不同的密钥一样会改变结果,但它不需要保密,只是为了确保相同的东西不会进行相同的加密


要强制使用IV来比较算法,或使用未包含的已知字符串进行解密,请使用
cipher.init(模式、密钥、新IvParameterSpec(IV))

那么,您是否也使用这两个字符串测试了解密?我有一个预感,第一个代码会失败…是的,两个都正常工作,并返回相同的原始字符串如果你想要相同的输出,你使用相同的编码的密文字节。在第一个示例中,您使用ByteArrayOutputStream的toString()方法,这毫无意义。在第二种情况下,您使用base64编码,这是有意义的。其他提示:永远不要使用默认值,例如
Cipher.getInstance(“desed”)
使用默认模式和默认填充。始终显式地指定这两者。总是这样。@GregS好的,很好的信息。你能给我推荐一些调式和填充物吗?。另外,您认为使用选项2?是个好主意,谢谢