Java:密码包(加密和解密)。无效的密钥错误
AES-256(和AES-192)要求为JRE安装无限强度管辖策略文件(最后一次下载为http://java.sun.com/javase/downloads/index.jsp)。如在您的类中那样,如果没有这种支持,则会在尝试使用192位或256位密钥时导致InvalidKeyException。
《Java6的JCA参考指南》中记录了AES允许的最大密钥大小,没有无限的强度,因此恰好是128位。
解决方法我正在使用静态方法在类中使用javax.crypto加密和解密消息。我有2个使用cipher和dcipher的静态方法,以完成他们应该做的事情,我需要初始化一些变量(也是静态的)。但是,当我尝试使用它时,我得到的InvalidKeyException与我提供给ecipher.init(…)的参数。我找不到原因。这是代码:
private static byte[] raw = {-31,17,7,-34,59,-61,-60,-16,26,87,-35,114,-53,99,-116,-82,-122,68,47,-3,-17,-21,-50,126,119,-106,-119,-5,109,98}; private static SecretKeySpec skeySpec; private static Cipher ecipher; private static Cipher dcipher; static {try { skeySpec = new SecretKeySpec(raw,'AES'); // Instantiate the cipher ecipher = Cipher.getInstance('AES'); dcipher = Cipher.getInstance('AES'); ecipher.init(Cipher.ENCRYPT_MODE,skeySpec); dcipher.init(Cipher.DECRYPT_MODE,skeySpec);} catch (NoSuchAlgorithmException e) { throw new UnhandledException('No existe el algoritmo deseado',e);} catch (NoSuchPaddingException e) { throw new UnhandledException('No existe el padding deseado',e);} catch (InvalidKeyException e) { throw new UnhandledException('Clave invalida',e);} }
相关文章:
1. javascript - 求一款靠谱点的移动端图片查看器插件,老司机速进!2. javascript - angular左右滑动如何切换路由状态,????3. jupyter-notebook - Mac下启动jupyter notebook后没有Python的选项?4. 数据库创建失败5. css3的transform问题6. android自带时钟应用的这个效果是怎么做的7. javascript - 如何在外部点击,跳转到网页后,显示指定的模块。8. 百度地图api - Android 百度地图点击线路图的问题?9. 这是什么情况???10. mysql sql where id in(25,12,87) 结果集如何用按照 25 12 87排序?

网公网安备