http://www.asfx.xyz/p/e3124067382f4c6a9fb1b43fc799b5e5
AES 的加密解决算法网上一大堆,但是大部分都没有解决一个问题,就是 windows 上测试正常,但在部分 linux 操作系统下却出现加密解密异常!比如会发现加密的结果值一直会变,这是因为实例化 SecureRandom 的方式不对造成的。
默认初始化方式为:
<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; padding: 10px; margin: 0px 0px 16px; font: 400 12px / 1.6 Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; overflow: auto; overflow-wrap: break-word; word-break: break-all; white-space: pre-wrap; overscroll-behavior-x: contain; border-radius: 3px; z-index: 0; color: rgb(204, 204, 204); background: rgb(246, 246, 246); border: 1px solid rgb(221, 221, 221); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
generator.init(new SecureRandom(KEY_STR.getBytes()));
</pre>
需要改成
<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; padding: 10px; margin: 0px 0px 16px; font: 400 12px / 1.6 Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; overflow: auto; overflow-wrap: break-word; word-break: break-all; white-space: pre-wrap; overscroll-behavior-x: contain; border-radius: 3px; z-index: 0; color: rgb(204, 204, 204); background: rgb(246, 246, 246); border: 1px solid rgb(221, 221, 221); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(KEY_STR.getBytes());
keygen.init(128, random);
</pre>
网友评论