我正在研究一个使用 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