首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java 3 3DES实现(学术)

Java 3 3DES实现(学术)
EN

Stack Overflow用户
提问于 2021-04-26 20:21:59
回答 1查看 159关注 0票数 2

到目前为止我的代码是:

代码语言:javascript
复制
public class TripleDES {
    /**
     * @param args the command line arguments
     * @throws java.security.NoSuchAlgorithmException
     * @throws javax.crypto.NoSuchPaddingException
     * @throws java.security.InvalidKeyException
     * @throws javax.crypto.IllegalBlockSizeException
     * @throws javax.crypto.BadPaddingException
     */
    public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        //Encrypt: C = EK3(DK2(EK1(P)))
        //Decrypt: P = DK3(EK2(DK1(C)))
        
        Scanner sc = new Scanner(System.in);
        
        //Generate key for DES
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        SecretKey secretKey = keyGenerator.generateKey();
        SecretKey secretKey2 = keyGenerator.generateKey();
        SecretKey secretKey3 = keyGenerator.generateKey();
        
        //Text Enc & Dec
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");



        
        //enter msg
        System.out.print("Enter a string: ");
        String x= sc.nextLine();
        
//enc
        cipher.init(Cipher.ENCRYPT_MODE,secretKey);
        byte[] message = x.getBytes();//text
        byte[] messageEnc = cipher.doFinal(message);//encryption with key1
        cipher.init(Cipher.DECRYPT_MODE,secretKey2);
        byte[] deck2 = cipher.doFinal(messageEnc);//decryption with key2
        cipher.init(Cipher.ENCRYPT_MODE,secretKey3);
        byte[] messageEnc1 = cipher.doFinal(deck2);//encryption with key3
        System.out.println("Cipher Text: " + new String(messageEnc1));
        
        //dec
        cipher.init(Cipher.DECRYPT_MODE,secretKey3);
        byte[] dec = cipher.doFinal(messageEnc1);//decryption with key1
        cipher.init(Cipher.ENCRYPT_MODE,secretKey2);
        byte[] messageEnc2 = cipher.doFinal(dec);//encryption with key2
        cipher.init(Cipher.DECRYPT_MODE,secretKey);
        byte[] deck3 = cipher.doFinal(messageEnc2);//decryption with key3
        System.out.println("Plain Text: " + new String(deck3));
    }
}

我知道错误:

代码语言:javascript
复制
Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:991)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
    at com.sun.crypto.provider.DESCipher.engineDoFinal(DESCipher.java:314)
    at javax.crypto.Cipher.doFinal(Cipher.java:2164)
    at tripledes.TripleDES.main(TripleDES.java:45)

我的猜测是,当我尝试在第45行用一个不同的密钥解密时,它给出了上面的错误,这是因为加密的文本比生成的密钥大,但我不太确定。

有人能帮忙吗?因为我找不出问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-26 21:06:49

我指的是最初发布的带有ciphercipher2cipher3实例的代码:问题在于cipher2cipher3的填充。只有cipher才能使用PKCS5Paddingcipher2cipher3必须应用NoPadding

生成的密文实际上与3 3DES生成的密文相同,条件是将secretKeysecretKey2secretKey3的级联字节用作3 3DES密钥。

顺便说一句,欧洲央行是一种不安全的模式,s. here.

关于评论意见:

有关使用字符集编码的密文的解码,请参见例如。关于缺少的编码规范,例如。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67273314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档