public static String encryptKey(String key,String text) throws CryptoException
{
String key1=null;
try {
System.out.println("input parameters length "+key.length()+ " " +text.length());
System.out.println("input parameters value "+key+ " " +text);
Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] inputBytes=text.getBytes();
//System.out.println(inputBytes.length);
byte[] outputBytes = cipher.doFinal(inputBytes);
// key= WriteArray.bytesToString(outputBytes);
key1=outputBytes.toString();
System.out.println("output parameters "+key1.length()+" "+ text.length());
System.out.println("output parameters value "+key1+ " " +text);
} 输入和输出字符串长度不同。这一切为什么要发生?
将字符串转换为字节数组的问题是转换吗?输出如下所示。
--------------------------------------
Level 1 Encryption Started
input parameters length 16 16
input parameters value 5iafq1b7d8i4hedu vg322qcfmnjbp3nj
output parameters 11 16
output parameters value [B@35851384 vg322qcfmnjbp3nj
--------------------------------------
Level 2 Encryption Started
input parameters length 16 11
input parameters value tvqfpjpul28ovo5c [B@35851384
output parameters 11 11
output parameters value [B@649d209a [B@35851384
--------------------------------------发布于 2015-04-10 22:49:53
切勿将加密数据视为字符串。它是二进制数据,当您调用outputBytes.toString()时,几乎可以肯定会发生一些不好的事情。
如果必须将二进制数据视为字符串,则应应用可以处理二进制数据并可靠地生成可用字符串的转换。base64编码就是一个例子。
在您的问题中,您似乎希望未加密的文本与转换为字符串的加密字节的长度相同。没有理由说这是真的。
你应该预料到,如果你加密,然后解密加密的字节,你会得到你的原始消息。然而,加密数据的大小几乎肯定是不同的。
https://stackoverflow.com/questions/29564059
复制相似问题