|
背景:java端有一套现成的代码,现在需要迁移到lua。9 j7 F4 u% f* p2 r) A2 b' Q; G: y
测试时:使用的key为同一个key = "1938703285589872452";1.java加密代码pom
( \: @% z s, U3 R5 |( P! N- <dependency>
, x! S( ]/ V' @1 {; z' D - <groupId>org.bouncycastle</groupId>
+ r+ O/ o; a5 J3 f: B1 `$ M- @ - <artifactId>bcprov-jdk15on</artifactId>" f0 G2 b0 S+ d: v
- <version>1.55</version>
) b- [- n r) W0 r! c - </dependency>
! F( n" m( W2 ?7 E6 P* A - " M: t4 f7 B9 w. I% G
- <dependency>
, n5 n0 y+ S' i$ Z - <groupId>org.bouncycastle</groupId>9 `: B7 f( j# A- d9 C/ M
- <artifactId>bcpkix-jdk15on</artifactId># X; x: |4 f8 E/ a
- <version>1.55</version>
% \. V6 q; v" A9 Q$ `) _0 Y. z - </dependency>& \; E# q: [$ L
; d- @/ c& R$ V- <dependency>
; T6 N9 K" L$ T% Y& C; z - <groupId>commons-codec</groupId>" I3 J ~8 ?" l c. }/ [7 K: D
- <artifactId>commons-codec</artifactId>
, g1 P; N z) \" n; m$ u4 H - <version>1.10</version> </dependency>1 v" k9 c7 M( x
. X3 `5 u" b# x, O L; _0 }
代码 , V5 `1 X" _1 b5 H
- import org.apache.commons.codec.binary.Base64;2 G8 ^' C! R, `6 R( B# s
- import org.bouncycastle.crypto.engines.AESEngine;
+ Z+ A$ i9 \; {3 ]1 P - import org.bouncycastle.crypto.modes.CBCBlockCipher; x5 K7 r3 J+ {/ t% J# A2 U
- import org.bouncycastle.crypto.paddings.PKCS7Padding;0 t; s2 c- }- ]
- import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
q+ G% Q% X4 ?4 K7 [ - import org.bouncycastle.crypto.params.KeyParameter;
; ~0 a! F5 i6 r0 {8 _! }- F - 5 p Q! I% ]; L
- import java.nio.ByteBuffer;
. G) @' I# t \, S+ e! H( m - import java.nio.charset.StandardCharsets;) y$ E; A$ X; y7 Z. @" n, [0 P* g
- 3 @' [6 y( h6 d) e* g) B. {" L
- // ...
; Y8 g' Y; O1 y, _ - $ o7 Y; @' W, E2 _5 G: q/ x7 c
- // 加密方法& \4 b" A1 s; y6 B @; F) w6 F8 R
- public static String encryptWithBC(String data, String key) throws Exception {' G) Y* T3 q: ^) J3 L/ ^& z
- // key* f/ d+ y- B( A9 o% S$ F/ D
- ByteBuffer keyBuffer = ByteBuffer.allocate(32);
$ z$ f* X! I6 i" F9 C! N2 q - keyBuffer.put(key.getBytes());" r7 f/ d% q) ^) N5 G$ r
- KeyParameter keyParameter = new KeyParameter(keyBuffer.array());9 N& U; }: P8 S* c% t8 _6 w
- // 请求数据; y$ H. m5 c) D% C" t1 }" P
- byte[] dataByteArr = data.getBytes(StandardCharsets.UTF_8);
, M: a6 p1 i1 Y2 f8 T - % \9 s/ ?0 ~. S+ O
- // init( N9 r2 Y" c/ D- k6 T
- CBCBlockCipher aes = new CBCBlockCipher(new AESEngine());
5 N. a3 {) j- F+ v0 L/ A) a9 z - PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(aes, new PKCS7Padding());
- V: q# C" I4 H. a6 }3 X) q - cipher.init(true, keyParameter);
9 Q0 V; V7 A3 n. f- R. ?; A- g
9 x0 Z- M+ \& I; t- byte[] output = new byte[cipher.getOutputSize(dataByteArr.length)];
; A- w! n2 b+ ^ - int len = cipher.processBytes(dataByteArr, 0, dataByteArr.length, output, 0);+ A( O" `$ @9 x
- cipher.doFinal(output, len);
) ?& P# O6 u& u7 T+ p; I3 Z5 m
6 g F* `1 e! ^6 |! x- return Base64.encodeBase64String(output); }0 r" N* Z3 r7 H0 n6 s
. S, P) I, A" `0 I5 o# M, R9 n
2.lua的加密代码
$ S! T$ w7 H/ ?- -- AES加密- O( |0 _$ l" B
- local aes = require "resty.aes": I0 F4 c# R$ e7 z
, a7 Q2 M* Z4 N! ]+ K- -- ...
, t9 C5 X& E- U5 {8 N0 M# Z3 J - 8 L1 ~- Y9 F) u2 f6 }/ r
- -- 加密方法
- d' `: p/ S; O( _ - function _M.encrypt_128_cbc_pkcs7(en_data, aes_key)8 f* t& L. f I6 t; h
- --local aes_256_cbc_with_padding = aes:new(key, nil, aes.cipher(256,"cbc"), {iv = string.sub(key, 1, 16)}, nil, nil, enable_padding); u$ }& Y! S" l8 q1 w$ o
- local aes_128_cbc_pkcs7 = aes:new(aes_key, nil, aes.cipher(128, "cbc"), nil, nil, nil, "KCS7")
0 u3 t5 F, Z& j- C& e - local encrypted = aes_128_cbc_pkcs7:encrypt(en_data)
5 z) T9 ]" m2 z! E6 b3 \ - -- 转base64
- x4 `$ ^6 K3 ^* U# A/ S2 @; H3 U - local encrypted_base64 = wkg_hex_utils.str_to_base64(encrypted)
, K E7 V1 }1 ~* U. z5 ~ - local encrypted_hex = wkg_hex_utils.str_to_hex(encrypted_base64)8 Q4 Z" q1 g j, v$ {$ r' Y/ s- ~ v
% N2 ^ J5 V% ^9 F9 n2 j2 D, k1 m& b
3 R5 i$ T0 x: d/ {- H; V- wkg_log_utils.log("AES加密结果(BASE64): {}", encrypted_base64): ?6 U8 M9 @' u; k
- wkg_log_utils.log("AES加密结果(Hex): {}", encrypted_hex)* p' b' a& R r
- return encrypted_base64end
0 m; Z1 }& y1 q" y- N5 V% F8 k8 l/ w + w V8 e5 G G7 i2 d$ X! G' w3 W
lua是参考的git:' I& l$ E* @8 Y4 r9 }9 }. ~
https://github.com/openresty/...5 y$ N6 `) b6 K) Y
. y6 l1 P4 O2 i8 ^# I/ I! y 我只能看出cbc、pkcs7Padding这几个信息,现在结果值完全对不上。" L& J, M9 F) v9 d
O. x) c7 c: k没有方向,还望各位指导一下,谢谢啦。我知道答案 回答被采纳将会获得12 金钱 已有0人回答 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|