Java中的DES实现。无法获得明文

时间:2013-10-23 07:00:27

标签: java security encryption des

我正在使用Java内置库进行DES实现的简单示例。这是我的代码:

import it.sauronsoftware.base64.Base64;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


public class DES {

    public static void main(String [] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException
    {
        String msg="This is a secret message";
        byte [] msgBytes=msg.getBytes();        
        byte [] keyBytes  = {(byte)0xFE, (byte)0xDC, (byte)0xBA, (byte)0x98, (byte)0x76, (byte)0x54, (byte)0x32, (byte)0x10};
        SecretKeySpec myDesKey = new SecretKeySpec(keyBytes, "DES");

        //to encrypt a message
        String cipher=encryptMsg(msgBytes, myDesKey);

        //to decrypt a message
        String plain = decryptMsg(cipher.getBytes(), myDesKey);

        System.out.println("Original Message: "+ msg);
        System.out.println("Encrypted Message: "+ cipher);
        System.out.println("Decrypted Message: "+ plain);

    } //end main

    //encryption function
    public static String encryptMsg(byte [] msgBytes, SecretKey myDesKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
    {
        Cipher desCipher;
        // Create the cipher 
        desCipher = Cipher.getInstance("DES/ECB/NoPadding");
        desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
        byte[] textEncrypted = desCipher.doFinal(msgBytes);

        // converts to base64 for easier display.
        byte[] base64Cipher = Base64.encode(textEncrypted);
        return new String(base64Cipher);
    } //end encryptMsg

    public static String decryptMsg(byte [] cipherBytes, SecretKey myDesKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
    {
        Cipher desCipher; 
        desCipher = Cipher.getInstance("DES/ECB/NoPadding");
        desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
        byte[] textDecrypted=desCipher.doFinal(cipherBytes);

        // converts to base64 for easier display.
        byte[] base64Plain = Base64.encode(textDecrypted);
        return new String(base64Plain);
    } //end decryptMsg
} //end class

我得到的输出是:

Original Message: This is a secret message
Encrypted Message: hNFgTAoz2TN9f6FcLdbjnEBe5DrsU4sm
Decrypted Message: RFdk1JK0gG0vv2zndHueS9rRe0Oux44ACGObsRHQ+4E=

我需要我的密钥是固定的(非随机)值。这就是我在开始时将其定义为字节数组的原因。

我的问题是我的解密功能没有返回原始邮件。这意味着代码中存在问题,并且加密可能不正确。我对编码问题持怀疑态度,因为加密/解密非常简单。你能在我的代码中指出问题吗?

修改 在解密中,我根据其中一条评论的建议将encode更改为decode。这不起作用。我明白了:

Exception in thread "main" java.lang.RuntimeException: Unexpected I/O error
    at it.sauronsoftware.base64.Base64.decode(Unknown Source)
    at DES.decryptMsg(DES.java:55)
    at DES.main(DES.java:25)
Caused by: java.io.IOException: Bad base64 stream
    at it.sauronsoftware.base64.Base64InputStream.acquire(Unknown Source)
    at it.sauronsoftware.base64.Base64InputStream.read(Unknown Source)
    at java.io.InputStream.read(Unknown Source)
    at java.io.InputStream.read(Unknown Source)
    at it.sauronsoftware.base64.Base64.copy(Unknown Source)
    at it.sauronsoftware.base64.Base64.decode(Unknown Source)
    ... 3 more

1 个答案:

答案 0 :(得分:1)

您没有解码原始邮件的加密;您正在解码加密原始邮件的 base64编码

不是传递cipher.getBytes(),而是需要Base64.decode(cipher).getBytes(),或者让方法接受base64字符串并处理方法中的解码。

此外,无需对解密的内容进行base64解码。它已经是原始编码。换句话说,只需返回new String(textDecrypted)


您要尝试的流程是:明文 - >加密内容 - > base64 - >加密内容 - >纯文本。你正在做的流程是文字 - >加密内容 - > base64 - >解密的base64(废话) - > base64(废话)