TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
2 G$ X* O" Y$ `# P! c- import java.util.Date;) [3 @2 a: i. @+ l c
- 1 W$ J0 M% ^4 x: H0 i3 H* p
-
c, x0 h8 d# _2 | - 4 S/ N5 q& R9 O& ?
- import org.slf4j.Logger;
# \% ^( y0 r4 G. n. Q3 F# @/ h1 L - & L, L! G2 ]& r. o
- import org.slf4j.LoggerFactory;4 j6 F( n; ~8 o% I9 M8 r
- / g, f4 w/ W+ S8 c4 P# H
- import org.apache.commons.codec.binary.Base64;
9 y, L/ } D9 R0 Q - / z8 E5 G! g! w
-
7 A2 @ _3 P5 c - 8 j$ A& d. p! T% V7 W. D% I& l- N* L) Q
- public class Base64Helper {$ C+ ^5 W: [0 z5 {! z, n
5 C, r% f, D A9 Q- private static final Logger logger = LoggerFactory.getLogger(Base64Helper.class);
. W* @( C# r& Z( G7 h9 ~0 z q - 3 A/ P, f6 u3 I) L: F9 k! ?, U
-
2 \- g0 T3 n% U2 C) H+ e9 v1 q
# w& G+ v) t% u; E* b2 \- /**! R8 J3 i" K( v3 _5 s& h
- T5 e) Z7 k+ q$ F7 ]. P# T% a- * BASE64解密% _- S! b, W1 s) l
0 f$ l# o5 j4 [! }% O$ H% ]- *
* _9 a6 a' @$ d - * C8 B3 |0 ~6 n' t3 C( z
- * @param key
9 _5 d. W0 w- A: A: R
9 L+ A- L; Y% N) [5 d2 j( p- * @return% r1 h0 O6 s R# d" Z
- 5 i; h4 ]- _9 n1 [+ [1 a
- * @throws Exception# M' v, m; W9 O1 B( t3 |( W% x
. u, l* V: H- }8 r3 a8 x' M- */
( v" N( y; r% g7 b4 W
" u4 H7 _. A) C- public static String decryptBase64(String key) throws Exception{ 6 {! [6 Q7 b+ a2 G0 S
/ G% \! ?. R. n- key = key.replace("-", "/"); //解决URL参数带/问题。 H5 y- {* P2 {' U5 D8 T( y
( U) e; N h6 I- byte[] byteArray=Base64.decodeBase64(key); // decryptBASE64(key);
0 I! c& p2 ^. B U0 k5 D4 D - - ~1 l- Z1 G E
- String code = new String(byteArray);( r2 z- b/ k1 G# N& T7 L
) X$ \/ i; ?$ o& @- logger.debug(" decrypt code:{} ",code);1 l5 e3 K' N4 ]# Q5 J# {8 ~& F
8 W5 r1 v4 C4 I6 Q) [: a* f- return code;
% k9 I) [4 c, A( h: u - + E, \. c( _5 z: R5 ^
-
, \ {' d$ d3 x8 J) m5 f - ! `: L5 t. z2 r% N7 l ~
- }
3 U2 j) J. r; G: c - : d( s' T2 [& A+ j* F; n) r
- // 3 W6 @0 I3 b* |' i2 t' J
- 7 s1 X1 p3 k% W0 ]! X+ H b" Z/ J
-
; u6 x! H3 o1 E0 C' d( C6 b$ q5 L/ q - V- M( I0 H8 t; C
- /**
) ~3 C0 d8 {9 G% W" V6 k
V3 o" U& }. o, n- * BASE64加密
3 ^+ T% P6 [7 ^& d6 ]+ h
) t; f$ J `! u- * % v3 J7 E1 \' g
- ) m7 i( X0 `' A+ i, P# d. `0 R! y$ @
- * @param key
4 O6 ^. d$ W" H* B5 H
6 s6 k& G2 K# u$ d C$ R- * @return
7 ]4 f0 v% D% g" B5 o: E5 _, F8 ~ - ; q' S1 z2 B1 ^. I3 r* j! m* v" ~' _ E
- * @throws Exception- S2 F! `, m0 k! J5 g) D" D
- O! H* b' g- N' H$ r+ L0 F- */
8 ?) P: N1 j, I: Q# [' O
9 k. O* t# ~1 x8 h2 X- public static String encryptBase64(String key) throws Exception {
7 X& ]! ?. \; F y) O) p
) A6 [( z/ X3 M+ X+ W- byte[] binaryData = key.getBytes();; T7 |9 \8 O* e( E6 r; c
- 3 _; H" h9 z, F' D
- String code = Base64.encodeBase64String(binaryData); //encryptBASE64(byteArray);
: [1 e4 A+ j0 f) w$ n - # U$ e8 g- ?5 i4 w& p1 b* @
- logger.debug(" encrypt code:{} ",code);5 J y9 m+ j. i1 {. f3 _, B
- " T& T* t8 D- _ ~
- return code.replace("/", "-");//解决URL参数带/问题。6 L$ v! y* B5 K$ t, F# ]/ y9 V
- 3 a7 a: h8 T/ \1 |. p
- }
1 U' V& x8 _& B) N- k% W
. b! h) F& q1 ?- Z' ~% ]- public static void main(String[] args) throws Exception {
9 Q3 }" E5 R' z+ H2 k - 7 ]4 r% I( i& y* f3 ~7 n
- Date startTime = new Date();
" p9 `/ A* x) X& F - + [& ?# O; Y1 Z
- //BASE64加密
2 {0 m' Y5 J8 {# E/ f& @4 d3 f
. z( {4 s1 E7 x4 D: {. j- String code = encryptBase64("MP3/Mp4");
, ]; U' H& S8 O4 ]" m+ f - : p; ^+ s! P+ U/ k
- logger.debug("BASE64加密:{}",code); - Z( D( U7 q/ C L4 F
B6 v7 O; e8 {# o. I6 @! s- String key = decryptBase64(code);5 w3 f' R% @3 V" h9 b
( g- C* k+ j0 P- logger.debug("BASE64解密:{}",key);3 }' K. `+ W$ ?; I* _8 x4 N
4 k3 n8 |3 o' N8 U. w" B" z- Date endTime = new Date(); k% @$ R! l- \! K
- 5 w5 K! h3 V2 s2 D5 c2 {6 P- W
- long diff = endTime.getTime() - startTime.getTime();
' U% ^* B5 {% h4 h) R - ; {! g" M' q- M! \- W
- long days = diff;// / (1000 * 60 * 60 * 24); 7 b' O. Q0 F3 g4 a# h9 S
- / i4 \7 F9 @, g0 i
- logger.debug("time:{}",days);* Y, D" o5 P0 X0 H5 ]2 U3 `$ d6 q
$ T7 P/ O$ P( p$ K0 l7 y- }
# t4 W B) L( o4 z! \: q
0 D' a. P! y9 Y' [* w- }
复制代码 % B2 ^! y1 ~5 s4 u
' J* W$ _ _$ X, K8 v |
|