TA的每日心情 | 怒 2016-6-8 11:09 |
---|
签到天数: 1 天 [LV.1]初学乍练
普通会员
- 积分
- 18
|
发表于 2016-6-8 11:21:44
|
显示全部楼层
import static org.junit.Assert.*; + q" [4 ?! _! O- d
- \# e Z1 J- N* t6 o2 R3 K( Dimport org.junit.Before;
, a) t( A H5 ]2 n) M C) O3 r% jimport org.junit.Test;
2 W) c- z$ c2 x) C$ B# Q% m) m3 Z; ] - ]6 _! L, }1 T; Q- C4 ^! ]: y
import java.util.Map;
* q! B6 i% y3 k, [9 f2 J
9 L8 ^7 P! B1 T. d" N! X/**
6 P4 T, G0 E( k: h5 o5 g4 Q * # _3 `7 E9 B( q! W% ` [
* @author 梁栋 + p2 b- z, o, ^$ m2 [' ~: [$ X
* @version 1.0
0 [/ w. h( D7 x$ {5 u * @since 1.0 ( m6 q" N$ W, \- r* ?* a# o
*/ 0 u! v' g% j# i/ t: Y7 H
public class RSACoderTest {
# `. v3 g, _2 i h. V, ^ private String publicKey;
. D( _3 a- v! c/ d3 Z6 J private String privateKey; . H2 n( |- k0 }& h4 [
( W& N3 J: f$ E3 @' T& ~
@Before ) { c/ M, y4 N+ c) Z7 o/ D
public void setUp() throws Exception {
^ p, T1 \; t Map<String, Object> keyMap = RSACoder.initKey(); ( C: }9 {, M5 `, j5 W
) K* E: B" `" n3 c. ]; E& O9 ]# m4 E& t
publicKey = RSACoder.getPublicKey(keyMap);
, Q3 w5 m) w* p- y' q privateKey = RSACoder.getPrivateKey(keyMap); / s. W. m2 F+ y# N3 V! X6 Z
System.err.println("公钥: \n\r" + publicKey); ' N5 O# J' e; s# f% _4 J, M& s @3 A
System.err.println("私钥: \n\r" + privateKey);
, B3 {4 R. y6 z2 j# U3 u: E+ o: e } F9 K5 P9 x2 x7 i0 ` Y
% W+ h+ _, }: N* ~+ H7 S @Test 3 Z5 t8 b, u. Y+ l9 P, K+ _
public void test() throws Exception { " I7 \9 k: P( s7 w
System.err.println("公钥加密——私钥解密");
4 P, j( V5 l3 M4 K# _ String inputStr = "abc"; # G% M4 z* L3 H4 S0 v! u
byte[] data = inputStr.getBytes(); * G" f3 @; Y- g: | w$ N. }
' C& H/ a% ]5 Q! E, k
byte[] encodedData = RSACoder.encryptByPublicKey(data, publicKey);
' ?& q( K6 G: t% `5 }5 c
6 j9 @1 n4 B1 X5 h; b8 F byte[] decodedData = RSACoder.decryptByPrivateKey(encodedData,
r0 _5 Z; V" y privateKey); $ u6 g S- O2 J6 G
+ Y8 k* n' J8 F- ^; v) o String outputStr = new String(decodedData);
; @2 ^% x1 _' @3 t! g) _ System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr); 1 Y q; s) M& B4 n
assertEquals(inputStr, outputStr);
9 I5 j2 b5 K, r+ e
" i5 @# r1 [3 _' t9 E9 h) _ }
- Q. X8 [7 Y, A1 a9 W3 _# U7 z $ M- P% ^! [" t$ E# S& f
@Test 8 u9 K% P3 e! l4 W8 }# Z* Y
public void testSign() throws Exception { . |; n# U+ p9 O$ F
System.err.println("私钥加密——公钥解密"); + p" K5 W6 k$ a- i3 M: `+ V
String inputStr = "sign";
' I3 M4 Q6 j; I byte[] data = inputStr.getBytes();
. M# Q8 l: V% [: \, m- Z
: ?+ J$ S, A) v, ? byte[] encodedData = RSACoder.encryptByPrivateKey(data, privateKey);
% X( ^2 |/ }, ]) s
0 l7 R+ y8 P9 z( u byte[] decodedData = RSACoder
9 l% L% w' e' P S .decryptByPublicKey(encodedData, publicKey);
4 E1 G' b/ v& X9 w9 X9 P 0 H% l) w& L2 D3 f/ |3 a
String outputStr = new String(decodedData); 7 _8 Y# K+ L9 M/ `7 j) S
System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr); " e& g" T# S- O: T. s2 W0 Z- S
assertEquals(inputStr, outputStr);
6 @7 {5 L3 f0 N# R0 L
0 h/ k2 k" L5 K( ~& Q System.err.println("私钥签名——公钥验证签名"); " G" V- R& K3 [& ~/ P( B O
// 产生签名
" e4 c0 P1 @; u2 T0 Y( N4 m' q) y' N String sign = RSACoder.sign(encodedData, privateKey);
! g( J+ P; n. x# Q7 r Z; X System.err.println("签名:\r" + sign);
; W/ O( s+ u! R7 S 9 _7 x2 b o; k& g
// 验证签名 " w; s$ E7 A; k
boolean status = RSACoder.verify(encodedData, publicKey, sign);
2 n+ Q$ p: ~- Q* F5 H, L$ m System.err.println("状态:\r" + status);
# X- @, }0 n1 A! g0 E- e7 X/ I1 { assertTrue(status); * i4 C& N, b) G4 a& E
! k( H: w! X4 E4 L. `
}
) o3 w( X3 ^/ q+ ? , {9 m' O, X4 \' Z# z3 b
} |
|