Java 3DES 实现(学术)

标签 java encryption cryptography des 3des

到目前为止我的代码是:

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));
    }
}

我收到错误:

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 行使用不同的 key 解密时,它给出了上面的错误,这是因为加密的文本大于生成的 key ,但我不太确定。

有人可以帮忙吗,因为我无法找出问题所在。

最佳答案

我指的是最初发布的带有 cipher 的代码, cipher2cipher3实例:问题是关于 cipher2 的填充和cipher3 。仅cipher可以使用PKCS5Padding , cipher2cipher3必须申请NoPadding .

生成的密文实际上与 3DES 生成的密文相同,前提是 secretKey 的连接字节, secretKey2secretKey3用作 3DES key 。

顺便说一句,ECB 是一种不安全模式。 here .


关于评论:
对于使用字符集编码对密文进行解码,请参见例如Problems when converting from byte array to string and back to byte array 。关于缺少的编码规范,s。例如String.getBytes() in different default charsets .

关于Java 3DES 实现(学术),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67273314/

相关文章:

encryption - 从文件中读入

java - HTTPS证书问题

Linux 上 Path 变量中的 Java

java - 如何判断一个资源文件是来自jar还是来自file?

java - 如何中断 ScheduledExecutorService

security - 生成 OAuth token 的最佳实践?

mysql - 将 MySQL 凭据存储在 MySQL 数据库中

algorithm - 短消息公钥加密算法

c++ - 我可以使用std::array的自定义分配器来获得安全的加密 key 吗?

.net - 可以将 RSA 私钥+公钥转换为 .pfx 文件吗?