我正在使用Java来制作一个玩具程序,它使用DES加密来加密消息。我要加密的消息是:DES加密明文与密码长度
String msg="This is a secret message";
对此我转换为字节为:
byte [] msgBytes=msg.getBytes();
,并将其发送加密功能的工作原理如下:
//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/PKCS5Padding");
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
然后,我显示密码,密码和明文长度,我得到:
Encrypted Message: FDCU+kgWz25urbQB5HbFtqm0HqWHGlGBHlwwEatFTiI=
Original msg length: 24
Encrypted msg length: 44
请问您能澄清一下,为什么密码长度是44而原始消息长度是24?
编辑: 好的,我需要澄清的答案。密码总是以=结尾。这可能是因为填充?你能向我解释为什么/这个密码的长度是多少?总是以=结尾? 我的代码是否正确或出现错误?我对编码部分有疑问。