栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

带有Bouncy Castle的256位AES / CBC / PKCS5Padding

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

带有Bouncy Castle的256位AES / CBC / PKCS5Padding

这应该为您工作:

public String dec(String password, String salt, String encString)        throws Exception {    byte[] ivData = toByte(encString.substring(0, 32));    byte[] encData = toByte(encString.substring(32));    // get raw key from password and salt    PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), toByte(salt), 50, 256);    SecretKeyFactory keyFactory = SecretKeyFactory .getInstance("PBEWithSHA256And256BitAES-CBC-BC");    SecretKeySpec secretKey = new SecretKeySpec(keyFactory.generateSecret( pbeKeySpec).getEnpred(), "AES");    byte[] key = secretKey.getEnpred();    // setup cipher parameters with key and IV    KeyParameter keyParam = new KeyParameter(key);    CipherParameters params = new ParametersWithIV(keyParam, ivData);    // setup AES cipher in CBC mode with PKCS7 padding    BlockCipherPadding padding = new PKCS7Padding();    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESEngine()), padding);    cipher.reset();    cipher.init(false, params);    // create a temporary buffer to depre into (it'll include padding)    byte[] buf = new byte[cipher.getOutputSize(encData.length)];    int len = cipher.processBytes(encData, 0, encData.length, buf, 0);    len += cipher.doFinal(buf, len);    // remove padding    byte[] out = new byte[len];    System.arraycopy(buf, 0, out, 0, len);    // return string representation of depred bytes    return new String(out, "UTF-8");}

我假设您实际上是在进行十六进制编码,

toByte()
因为您的代码对IV使用32个字符(提供了必要的16个字节)。虽然我没有您用于加密的代码,但我确实验证了此代码将提供与您的代码相同的解密输出。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/497639.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号