为了账号安全,请及时绑定邮箱和手机立即绑定

Java AES并使用我自己的密钥

Java AES并使用我自己的密钥

潇湘沐 2019-10-24 10:30:23
我想使用我自己的密钥使用AES加密字符串。但是我在密钥的位长度上遇到了麻烦。您能否查看我的代码并查看需要修复/更改的内容。public static void main(String[] args) throws Exception {    String username = "bob@google.org";    String password = "Password1";    String secretID = "BlahBlahBlah";    String SALT2 = "deliciously salty";    // Get the Key    byte[] key = (SALT2 + username + password).getBytes();    System.out.println((SALT2 + username + password).getBytes().length);    // Need to pad key for AES    // TODO: Best way?    // Generate the secret key specs.    SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");    // Instantiate the cipher    Cipher cipher = Cipher.getInstance("AES");    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);    byte[] encrypted = cipher.doFinal((secrectID).getBytes());    System.out.println("encrypted string: " + asHex(encrypted));    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);    byte[] original = cipher.doFinal(encrypted);    String originalString = new String(original);    System.out.println("Original string: " + originalString + "\nOriginal string (Hex): " + asHex(original));}现在,我得到一个异常“ 无效的AES密钥长度:86个字节 ”。我是否需要垫钥匙?我该怎么办?还需要为ECB或CBC设置任何内容吗?
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 2568 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信