|
背景:java端有一套现成的代码,现在需要迁移到lua。9 {: q1 R8 d/ ~+ r
测试时:使用的key为同一个key = "1938703285589872452";1.java加密代码pom
' M: B m/ b, @; K# [- <dependency>
! H; w! Q+ S% B6 e" V - <groupId>org.bouncycastle</groupId>
( h5 A' p/ v( i; l - <artifactId>bcprov-jdk15on</artifactId>* e5 A# S+ E# I& e7 |/ k0 u
- <version>1.55</version>
P, d7 x& O4 D7 g+ D; K' g - </dependency>2 Q( `, Q9 q5 G% J
- ( y$ d3 T2 ]' _0 L7 Z( E8 A
- <dependency>; ]6 b9 Z0 ^6 f; |9 d
- <groupId>org.bouncycastle</groupId>8 [2 X& b) f7 j% K8 T
- <artifactId>bcpkix-jdk15on</artifactId>
" H! K9 Y+ T/ q/ R5 M - <version>1.55</version>$ w5 e5 F+ ^$ l/ g2 b
- </dependency>
5 K/ C6 w1 w9 F - 8 n! a4 L: j' ~. q7 c; }8 m
- <dependency>) g' i( }# o' V3 M% z6 x+ n5 D
- <groupId>commons-codec</groupId>
! I1 u# t+ [- y+ C, ]: \' U7 l: _9 | - <artifactId>commons-codec</artifactId>; P, R( j0 o- s! R' t* }) m8 j3 c3 ~; e
- <version>1.10</version> </dependency>+ L/ j( @7 \ |- `6 H$ L
/ M1 g8 D% ^" j5 E ^
代码
3 _# D; S; a l6 s* a- import org.apache.commons.codec.binary.Base64;
1 o, ^% t6 E2 n5 u; \ - import org.bouncycastle.crypto.engines.AESEngine;7 q3 h. w! Y; N4 N) a; b
- import org.bouncycastle.crypto.modes.CBCBlockCipher;
( h% u0 k G9 x' S - import org.bouncycastle.crypto.paddings.PKCS7Padding;& k3 x w; A7 P* X" {# j x7 g
- import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
$ O6 q' h& W E1 s' m: t9 U - import org.bouncycastle.crypto.params.KeyParameter;
$ y0 Z! }' s1 d, n {0 N - 5 E0 V! q" d8 D& u l8 E* V* x
- import java.nio.ByteBuffer;" x5 o; [& u$ t) H' s! h4 i! D
- import java.nio.charset.StandardCharsets;( ~! Z$ E) I% l; l1 J9 J
. n. |9 ?2 X5 ^/ G. A# z: e- // .... l) e6 ?& [: X1 Q4 {
3 x, U: C2 A% E$ |- // 加密方法
1 c" B. ^/ c( K5 l - public static String encryptWithBC(String data, String key) throws Exception {
. D5 {5 O% I( n( D9 L; D9 y! I# l - // key
1 _" w+ v/ C2 v8 ~ O - ByteBuffer keyBuffer = ByteBuffer.allocate(32);
1 b7 H2 m) u9 N* D. |6 y - keyBuffer.put(key.getBytes());
/ K m }5 W0 |% m" o - KeyParameter keyParameter = new KeyParameter(keyBuffer.array());
% D6 v4 W6 p: r+ ~' T/ y - // 请求数据
/ W3 f V! ~. y! r$ H' p - byte[] dataByteArr = data.getBytes(StandardCharsets.UTF_8);9 R5 [! \ A# e) k$ o
3 F3 K6 S! U" J( W* p, F' z) t- // init0 {+ G3 @% O* t- K( S' B
- CBCBlockCipher aes = new CBCBlockCipher(new AESEngine());; \ V6 J4 z6 I) z
- PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(aes, new PKCS7Padding());) s* I/ M4 R' @. d: e
- cipher.init(true, keyParameter);, ~: k" R0 T% r/ u9 O& D' T
- 3 Z9 r& H, _2 s" R, m1 r
- byte[] output = new byte[cipher.getOutputSize(dataByteArr.length)];
2 @; z7 _4 i) _, P# d - int len = cipher.processBytes(dataByteArr, 0, dataByteArr.length, output, 0);( m9 ^6 a! O1 w" R/ V% H
- cipher.doFinal(output, len);4 S5 a; W4 h: A% b) s
- / D& l- R+ a! g9 q
- return Base64.encodeBase64String(output); }: L/ j4 b5 _. V' ]
: E8 @4 }% i& Y/ [6 s
2.lua的加密代码3 Z: C _2 x, @9 k
- -- AES加密' `! i. Z' \* G4 J9 J. A# U
- local aes = require "resty.aes"+ f# G# E3 g- a* x4 l* K& T
- - l3 R' d+ p1 `: Q2 l; x5 L" i5 X
- -- ... z) A p! g# F: y( ~
" _# C- L& p+ M/ f3 B; C0 c0 N- -- 加密方法
, Z) a% k$ S5 g - function _M.encrypt_128_cbc_pkcs7(en_data, aes_key). u0 G9 z: I& q. T+ d
- --local aes_256_cbc_with_padding = aes:new(key, nil, aes.cipher(256,"cbc"), {iv = string.sub(key, 1, 16)}, nil, nil, enable_padding)6 {/ t2 o6 ~. t/ N% C" f# ?
- local aes_128_cbc_pkcs7 = aes:new(aes_key, nil, aes.cipher(128, "cbc"), nil, nil, nil, "
KCS7")5 N7 K b* Q5 m) @" b0 @
- local encrypted = aes_128_cbc_pkcs7:encrypt(en_data)
7 c2 L$ Z5 O1 u: U4 q - -- 转base64
9 ~5 Q+ G4 z+ y+ ~ L - local encrypted_base64 = wkg_hex_utils.str_to_base64(encrypted)
( q/ x1 s( k. g, o9 d$ p$ d9 Y - local encrypted_hex = wkg_hex_utils.str_to_hex(encrypted_base64)
; m9 ?' I8 h; L/ ` I" r, ~3 O - ' B1 H! J% e$ V* ?* ]6 j# t
- 7 _( [. t+ c h) r- M8 Z
- wkg_log_utils.log("AES加密结果(BASE64): {}", encrypted_base64)4 x' {: r9 V% O$ x& Q7 h
- wkg_log_utils.log("AES加密结果(Hex): {}", encrypted_hex)
0 c9 Q0 ^& X; D3 \& c( @. B* N - return encrypted_base64end
3 }5 n, w/ _9 m }3 k
* q' K. a ?: O. Z4 t+ Olua是参考的git:) y) h5 O: j' T- C
https://github.com/openresty/...0 R: E A3 [0 r: S/ `2 @+ F0 T
6 ]) R6 F8 i6 s4 {1 O2 M4 ?
我只能看出cbc、pkcs7Padding这几个信息,现在结果值完全对不上。
% G0 E( _7 D$ b& O S) @# ~' v% @7 E) {
没有方向,还望各位指导一下,谢谢啦。我知道答案 回答被采纳将会获得12 金钱 已有0人回答 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|