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

查看:109
本文介绍了使用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 = new DESKeySpec(encBytes);
DESKeySpec keySpecDecrypt = new DESKeySpec(decBytes);


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

密码cipherEncrypt = Cipher.getInstance(algorithm);
密码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));


解决方案

以下是JDK doc的描述:

 
DESKeySpec
public DESKeySpec(byte [] key)
throws InvalidKeyException
使用前8个字节创建一个DESKeySpec对象作为DES密钥的关键材料。
构成DES密钥的字节是密钥[0]和密钥[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天全站免登陆