java 加密解密不透明加密密钥_Java:密码包(加密和解密)。无效的密钥错误

我正在使用静态方法在类中使用javax.crypto加密和解密消息。我有2个使用cipher和dcipher的静态方法,以完成他们应该做的事情,我需要初始化一些变量(也是静态的)。但是,当我尝试使用它时,我得到的InvalidKeyException与我提供给ecipher.init(…)的参数。我找不到原因。这是代码:

private static byte[] raw = {-31, 17, 7, -34, 59, -61, -60, -16,

26, 87, -35, 114, 0, -53, 99, -116,

-82, -122, 68, 47, -3, -17, -21, -82,

-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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值