|
背景:java端有一套现成的代码,现在需要迁移到lua。, M/ u v7 [, N5 }$ S! e9 H" [
测试时:使用的key为同一个key = "1938703285589872452";1.java加密代码pom
% h8 S$ K' H7 U1 s( Z! i- <dependency>
7 }6 |$ v; @ z& U& u1 P4 M' [' W4 L - <groupId>org.bouncycastle</groupId>: S G h0 c3 ~1 Y# n- s* B0 ~
- <artifactId>bcprov-jdk15on</artifactId>8 k" y0 j* j6 g1 @8 ~+ ?
- <version>1.55</version>5 [! g+ y1 _) A8 m6 a* l- \
- </dependency>* J/ C2 k, s8 o K# ], H4 \
- B0 J2 h; w; B$ Z0 H9 o( |
- <dependency>* H: x) f- G) E! ^: v. t. G0 M4 ?
- <groupId>org.bouncycastle</groupId>* Y' E( O+ }' x: l; i: l: W) e
- <artifactId>bcpkix-jdk15on</artifactId>, w! G2 K7 D2 k" B1 y( m$ }+ {
- <version>1.55</version>) [, V- t3 {1 j+ ~: P* S
- </dependency>* F* Z0 v7 n1 H2 n. G' x2 L
- ! Z; o0 W9 L0 p5 p. a
- <dependency>
7 j, \9 u# q$ L0 A! A: i1 { - <groupId>commons-codec</groupId>
) v3 h6 ^! Y+ v2 o7 x. k - <artifactId>commons-codec</artifactId>
' a% p" E! ]0 I - <version>1.10</version> </dependency>
4 x- S7 c" P" ]) h0 g: K * R1 s g9 k/ b
代码 # f( _% A7 X: b: Q
- import org.apache.commons.codec.binary.Base64;
2 |- v: M+ |/ j - import org.bouncycastle.crypto.engines.AESEngine;
# { v Q1 M8 J+ j1 ?6 i! f: m - import org.bouncycastle.crypto.modes.CBCBlockCipher;
5 Q; {' Z/ U( @ u0 K" I+ I9 R - import org.bouncycastle.crypto.paddings.PKCS7Padding;% R: J4 y8 @5 m' L5 z& }4 o8 x
- import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;; J# R$ n7 y! J( n2 E* a. n. b' ~: D
- import org.bouncycastle.crypto.params.KeyParameter;6 k, M1 x& w' b5 C
- + O$ s( h$ k/ C: g4 \
- import java.nio.ByteBuffer;
9 ^# @/ |( D: b - import java.nio.charset.StandardCharsets;
# H( J9 L/ `7 y/ z% f/ f9 l! }
' k9 ]% j+ |7 h- F- // ...6 c: t! m! Q m$ _ F
4 O& ?" w! r0 }- // 加密方法
$ x& A1 ~' E: e5 |# K b: D* B - public static String encryptWithBC(String data, String key) throws Exception {# k* A4 u9 ~/ s5 o8 M0 }% D1 c
- // key
6 h7 _0 L) r0 m( y - ByteBuffer keyBuffer = ByteBuffer.allocate(32);; e g% f( B0 c, Q6 q4 V# }# l
- keyBuffer.put(key.getBytes());) B/ _" c% e4 q1 Q9 q
- KeyParameter keyParameter = new KeyParameter(keyBuffer.array());0 L5 M1 V7 a5 ]
- // 请求数据) F' B4 g) `( Y' v
- byte[] dataByteArr = data.getBytes(StandardCharsets.UTF_8);
. A& y8 D; ]- r! ^8 t: n/ x; C
1 o- p6 {5 H( x) g2 m1 l1 O# J8 r; r- // init9 @: Q& d; u) z0 R' }
- CBCBlockCipher aes = new CBCBlockCipher(new AESEngine());
7 b( Y0 k. N7 h$ z! p- `' x1 D - PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(aes, new PKCS7Padding());
5 G0 |7 j) i7 S( r7 |% \ - cipher.init(true, keyParameter);5 b' z8 K2 @, }* Z* `( J5 E* S8 v
- ! y8 _* U* N, F) f
- byte[] output = new byte[cipher.getOutputSize(dataByteArr.length)];# N# x5 ?; J+ d3 N
- int len = cipher.processBytes(dataByteArr, 0, dataByteArr.length, output, 0);
; M; M8 B$ y# Z$ \" x - cipher.doFinal(output, len);
% j, \6 d) q( w8 }) P5 c$ p7 @
; N; b: e3 {9 H/ A8 E- return Base64.encodeBase64String(output); }
0 R" J1 y9 u6 Y8 C
; j; A6 ~0 _4 B3 o# Y: E 2.lua的加密代码
( q, [' ?8 ^4 u4 k9 D- -- AES加密. c1 o! n' C( t. x
- local aes = require "resty.aes"+ @9 B* _0 ?: N- G; c. p( S4 c0 n
! N# u5 {0 K: H' ?$ N0 t8 d- -- ...
8 h# v* B$ ]( j0 P - 1 X; C5 H( v" n% L% E4 _/ `/ h
- -- 加密方法3 b( H3 s5 s4 h6 {( h
- function _M.encrypt_128_cbc_pkcs7(en_data, aes_key)
7 L, v. P% B# C7 X - --local aes_256_cbc_with_padding = aes:new(key, nil, aes.cipher(256,"cbc"), {iv = string.sub(key, 1, 16)}, nil, nil, enable_padding)
0 n6 m* X7 S5 [ - local aes_128_cbc_pkcs7 = aes:new(aes_key, nil, aes.cipher(128, "cbc"), nil, nil, nil, "KCS7")
% y8 S2 n* A+ T2 z6 H& C - local encrypted = aes_128_cbc_pkcs7:encrypt(en_data)/ x" I' O' }) U" y) C+ e
- -- 转base64
* }8 j5 N- M* D2 w5 {* c) E. [ - local encrypted_base64 = wkg_hex_utils.str_to_base64(encrypted)+ l) s# z ?7 D3 _
- local encrypted_hex = wkg_hex_utils.str_to_hex(encrypted_base64)+ q6 y& Y. i5 H# s3 G$ y7 ?
- 9 D, e. K: t& l7 ]* U0 o
- 0 ~- \7 N0 t4 U$ {
- wkg_log_utils.log("AES加密结果(BASE64): {}", encrypted_base64)
0 @0 O0 N$ X) i: p - wkg_log_utils.log("AES加密结果(Hex): {}", encrypted_hex)* g* q, ?- g2 S' {' P k) E
- return encrypted_base64end$ {& j. \, c' I: z5 q
+ n3 D- z# _% W5 O6 Ilua是参考的git:
D# `( @+ q( i0 Khttps://github.com/openresty/...
2 J7 f5 |/ w; K- ]" z c( e) h5 ^% a. t
我只能看出cbc、pkcs7Padding这几个信息,现在结果值完全对不上。& J& j' }3 a+ Y! H
) f+ e! U% r; i3 X1 g. z2 S7 S' P没有方向,还望各位指导一下,谢谢啦。我知道答案 回答被采纳将会获得12 金钱 已有0人回答 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|