【发布时间】:2015-04-10 14:30:08
【问题描述】:
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
--------------------------------------
【问题讨论】:
-
Difference between new String(char[]) and char[].toString 的可能重复项我能找到的最好的,但无论您使用
byte[]还是char[],这都是同一个问题
标签: java encryption aes encryption-symmetric