使用 Java 加密和解密:无法获得相同的输出 [英] Encrypting and Decrypting Using Java: Unable to get same output

查看:15
本文介绍了使用 Java 加密和解密:无法获得相同的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习和测试 java 1.6 加密/解密 API.我想知道我做错了什么以及我在知识方面缺少什么.

在下面的代码中,我创建了两种密码:一种用于加密,另一种用于解密.当我使用这些密码时,我用不同的 SecretKey 初始化它们,但我仍然能够得到相同的值.这是为什么?

 String algorithm = "DES";SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);byte[] encBytes = "12345678".getBytes("UTF8");byte[] decBytes = "56781234".getBytes("UTF8");DESKeySpec keySpecEncrypt = 新的 DESKeySpec(encBytes);DESKeySpec keySpecDecrypt = 新的 DESKeySpec(decBytes);SecretKey keyEncrypt = keyFactory.generateSecret(keySpecEncrypt);SecretKey keyDecrypt = keyFactory.generateSecret(keySpecDecrypt);Cipher cipherEncrypt = Cipher.getInstance(algorithm);Cipher cipherDecrypt = Cipher.getInstance(algorithm);字符串输入 = "john doe";cipherEncrypt.init(Cipher.ENCRYPT_MODE, keyEncrypt);byte[] inputBytes = cipherEncrypt.doFinal(input.getBytes());System.out.println("inputBytes:" + new String(inputBytes));cipherDecrypt.init(Cipher.DECRYPT_MODE, keyDecrypt);byte[] outputBytes = cipherDecrypt.doFinal(inputBytes);System.out.println("outputBytes:" + new String(outputBytes));

解决方案

这是 JDK 文档中的描述:

<前>DES密钥规范公共 DESKeySpec(字节 [] 密钥)抛出 InvalidKeyException使用密钥中的前 8 个字节作为 DES 密钥的密钥材料创建一个 DESKeySpec 对象.构成 DES 密钥的字节是 key[0] 和 key[7] 之间的字节.

DESKeySpec 仅使用 byte[] 的前 8 个字节作为密钥.因此,在您的示例中使用的实际密钥是相同的.

I am trying to learn and test the java 1.6 encryption/decryption API. I want to know what I am doing wrong and what I am missing in terms of knowledge.

In the code that follows below, I create two ciphers: one to encrypt and another to decrypt. When I use these ciphers, I initialize them with different SecretKey's, but I am still able to get the same value back out. Why is this?

    String algorithm = "DES";
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);

    byte[] encBytes = "12345678".getBytes("UTF8");
    byte[] decBytes = "56781234".getBytes("UTF8");

    DESKeySpec keySpecEncrypt = new DESKeySpec(encBytes);
    DESKeySpec keySpecDecrypt = new DESKeySpec(decBytes);


    SecretKey keyEncrypt = keyFactory.generateSecret(keySpecEncrypt);
    SecretKey keyDecrypt = keyFactory.generateSecret(keySpecDecrypt);

    Cipher cipherEncrypt = Cipher.getInstance(algorithm);
    Cipher cipherDecrypt = Cipher.getInstance(algorithm);

    String input = "john doe";

    cipherEncrypt.init(Cipher.ENCRYPT_MODE, keyEncrypt);
    byte[] inputBytes = cipherEncrypt.doFinal(input.getBytes());
    System.out.println("inputBytes: " + new String(inputBytes));

    cipherDecrypt.init(Cipher.DECRYPT_MODE, keyDecrypt);
    byte[] outputBytes = cipherDecrypt.doFinal(inputBytes);
    System.out.println("outputBytes: " + new String(outputBytes));

解决方案

Here's the description from JDK doc:

DESKeySpec
public DESKeySpec(byte[] key)
           throws InvalidKeyException
Creates a DESKeySpec object using the first 8 bytes in key as the key material for the DES key. 
The bytes that constitute the DES key are those between key[0] and key[7] inclusive. 

DESKeySpec uses only the first 8 bytes of byte[] as key. Thus the actual keys in used are identical in your example.

这篇关于使用 Java 加密和解密:无法获得相同的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆