用AES加密密钥长度问题

qufei0325 2008-10-28 10:56:29
我想用AES加密信息,其中密钥是根据我输入的一个字符串生成的,运行时系统报错说“Exception in thread "main" java.security.InvalidKeyException: Invalid AES key length: 8 bytes”,出错的语句是“aesCipher.init(Cipher.ENCRYPT_MODE, aesKey);”,下面是我的代码:

import javax.crypto.spec.*;
import javax.crypto.*;

public class Experiment {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
SecretKeySpec aesKey = new SecretKeySpec("12345678".getBytes(), "AES");
Cipher aesCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
aesCipher.init(Cipher.ENCRYPT_MODE, aesKey); //这句报错了
byte[] plaintext = "Hello!".getBytes();
byte[] ciphertext = aesCipher.doFinal(plaintext);
aesCipher.init(Cipher.DECRYPT_MODE, aesKey);
byte[] decrypted = aesCipher.doFinal(ciphertext);
System.out.println(decrypted);

}

}

问题出在哪里?应该怎么改?请大家帮帮忙!
...全文
10993 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dragonccc 2009-06-16
  • 打赏
  • 举报
回复
SecretKeySpec aesKey = new SecretKeySpec("1234567812345678".getBytes(), "AES"); //Key要16位的
因为AES是128\192\256。。。

请问 怎么改才能解决问题呢??
getter 2008-12-10
  • 打赏
  • 举报
回复
晚了
laughter75 2008-11-17
  • 打赏
  • 举报
回复 1
SecretKeySpec aesKey = new SecretKeySpec("1234567812345678".getBytes(), "AES"); //Key要16位的
因为AES是128\192\256。。。
l_wenb 2008-10-28
  • 打赏
  • 举报
回复
帮顶!

51,396

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧