TA的每日心情 | 怒 2016-6-8 11:09 |
---|
签到天数: 1 天 [LV.1]初学乍练
普通会员

- 积分
- 18
|
发表于 2016-6-8 11:21:44
|
显示全部楼层
import static org.junit.Assert.*; % T: Z `& V+ W6 M7 L0 D1 M
) f* S F& z! d/ d& Cimport org.junit.Before;
( ^' {! k' w% o' F/ S1 X9 _import org.junit.Test;
) a& S+ S5 t9 T {! N5 [ 9 f8 d# g# \, P3 `) ?. N. A
import java.util.Map; 9 W/ }: Z# a6 @8 p, P2 u% C
, Q; |- _( w: V% f9 ~9 `: A2 ~
/** 7 E8 j. ~* d/ c0 H6 E
* ' @4 M* J# t& s4 B5 h( P* F
* @author 梁栋
8 ]! Q1 A7 k% A. v * @version 1.0
8 H7 q- e6 d0 A% g * @since 1.0
" E$ L9 i% o3 ~+ I; ~( D; u$ R */ 9 b$ M( Z) M& h2 D7 t
public class RSACoderTest {
5 ~3 y, M$ I' \2 ^/ { private String publicKey;
% O9 f/ Q0 b% E private String privateKey;
: f; K/ I+ k0 ` 3 V1 _6 G5 Z& H" H1 M& N4 M
@Before
+ x( |/ Y# g3 O6 c; P s public void setUp() throws Exception {
) o. c0 t+ u- R: U Map<String, Object> keyMap = RSACoder.initKey();
% u( S% [+ I1 E+ M) O8 I7 t 0 O( D2 n8 t$ @1 @' V
publicKey = RSACoder.getPublicKey(keyMap);
* u/ ^- V0 }; g0 b- H privateKey = RSACoder.getPrivateKey(keyMap); + `4 W7 b- E$ J- z& c
System.err.println("公钥: \n\r" + publicKey);
. V, o" H" I3 [4 Y9 O& i( f5 x6 } System.err.println("私钥: \n\r" + privateKey); 6 t' {2 @9 i$ F% _
}
, Y3 s' ?! @/ v + `3 I: ~' M; @, D. M, P0 B! O4 j
@Test
3 s9 m. p q, c! u# j public void test() throws Exception {
4 |6 d/ R* ]- z8 I+ Q, D7 @ System.err.println("公钥加密——私钥解密"); % _+ t, m* s+ e' F7 X: m
String inputStr = "abc"; - U- K( m" s! R. G
byte[] data = inputStr.getBytes(); " c8 Q& u( o6 l) o% p
' P3 C% Q l2 A" A N1 V byte[] encodedData = RSACoder.encryptByPublicKey(data, publicKey); ; s7 m* T: l* ~8 d0 L/ X
6 N5 L5 ?* U! K! F! ]: A8 T5 t byte[] decodedData = RSACoder.decryptByPrivateKey(encodedData, ) E1 j8 `4 |% t c/ J
privateKey); 7 ^( r6 b, R) w
5 Y/ v! `: n6 d, {! ]$ {. @9 K; D
String outputStr = new String(decodedData);
1 \! Z9 Q( f% m( Y' J1 p Y System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr); * V: W- l7 e/ H6 I; E
assertEquals(inputStr, outputStr); ( q( o3 y2 ~% R- W
/ K$ e) V; {4 I }
4 m: g4 t, O x+ e
2 d: |+ o5 b1 {; c7 u2 T3 E @Test
' Z& S$ d6 P; Q7 @4 F! z# d public void testSign() throws Exception {
. _, t' L2 Q) j System.err.println("私钥加密——公钥解密"); 1 ~* J0 w# _& z
String inputStr = "sign"; . g. r4 T6 ~5 r, F1 a
byte[] data = inputStr.getBytes(); 3 l5 _0 w5 p& J9 t' E2 y8 `7 b
J! V1 q; f9 n4 f% W5 Q byte[] encodedData = RSACoder.encryptByPrivateKey(data, privateKey);
9 {2 A6 E- w! [1 Q8 x8 m h0 o6 W ) p: @# u: B' s, N
byte[] decodedData = RSACoder
1 u; ~9 y- j' b( z .decryptByPublicKey(encodedData, publicKey);
, @8 V% A3 a3 @" x 1 G$ u6 i. ], P, N
String outputStr = new String(decodedData); - @2 b3 Q4 d0 c5 N$ J
System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);
" c& B; D9 F) R0 Q8 l' |- b assertEquals(inputStr, outputStr);
9 V9 e; o9 e, y% e A6 _+ c( `8 a7 ]: M* t
System.err.println("私钥签名——公钥验证签名");
* b7 ]! a" b1 C1 N) M; h5 B' k- P // 产生签名
( V" N! ?- z0 G; p: V" s String sign = RSACoder.sign(encodedData, privateKey);
. I" t7 t( I! B System.err.println("签名:\r" + sign);
, g( G5 t# [ Z3 n, a3 W* V
8 I/ H; C! L+ T' g // 验证签名 ' C" H; w0 o$ v$ ]0 G. [, @
boolean status = RSACoder.verify(encodedData, publicKey, sign); , ~2 f+ i. ]! y" u
System.err.println("状态:\r" + status);
4 K; V! v2 S# C assertTrue(status);
& s' k4 D1 V" z* w& j
' c8 ?: P [/ A+ H' Q } : i7 R. ]3 K7 N+ P
$ P6 [, \- n4 x8 f$ q1 {$ K3 e
} |
|