TA的每日心情 | 怒 2016-6-8 11:09 |
---|
签到天数: 1 天 [LV.1]初学乍练
普通会员
- 积分
- 18
|
发表于 2016-6-8 11:21:44
|
显示全部楼层
import static org.junit.Assert.*;
$ ?5 f; T3 s0 P% z
5 n2 {: s$ }/ cimport org.junit.Before; / V- N" K' f; f: |$ X2 B8 h
import org.junit.Test;
$ Q2 L& N1 e7 a3 j: f7 A- E3 D
0 g' N5 R9 d3 U4 ]import java.util.Map; . B5 {9 n1 A: D* n1 f! h, {
) G' v: G; a$ k9 x/** ) @. _; H7 G4 U# h! w: c5 r6 n9 ], D2 I
* 4 A/ C# r5 k$ U! j; c+ E
* @author 梁栋 : D7 l& [) ~7 T4 v! M
* @version 1.0
" c+ [8 R7 I1 O+ m b* a * @since 1.0
6 x4 @9 `+ {- y5 L! p */
6 Z3 n, z! m1 B0 K8 Y& lpublic class RSACoderTest {
# b" O; m- g2 e$ u) ^# a private String publicKey;
; t$ n6 k* J2 ?, }/ v* c private String privateKey; 3 S/ Z L6 V) y: Z7 D$ }
9 H% v$ G" u% P5 O
@Before
; j E( R% o2 W2 J2 H' W5 M public void setUp() throws Exception { - d" p# f; B! w: B+ R( o$ g
Map<String, Object> keyMap = RSACoder.initKey(); - j! I9 H; E$ I" c7 C( X
3 e# P& q% A% c
publicKey = RSACoder.getPublicKey(keyMap);
' ?& v! u) D. p9 F: H( I5 I, F privateKey = RSACoder.getPrivateKey(keyMap);
$ W% L6 \, w- u( m8 y System.err.println("公钥: \n\r" + publicKey); 9 s2 Y. D' \: _ s% f, V
System.err.println("私钥: \n\r" + privateKey);
5 p; s% r$ S9 p } 1 G, Q9 @7 u7 R7 T
5 f7 r4 v5 T) }- l5 J
@Test 3 B" e5 B3 X9 R1 }4 \7 t: p3 H
public void test() throws Exception { 9 L2 c# e5 k2 C3 _6 u; Q+ t: A
System.err.println("公钥加密——私钥解密"); & i5 ?" K* Q# t2 }) Y, W$ t) o3 o
String inputStr = "abc"; ; N# J" p. ~1 B- E ^- y. c/ M
byte[] data = inputStr.getBytes();
9 q9 A' b n* Q- X0 S
! `! Z" Z4 I7 n" ] byte[] encodedData = RSACoder.encryptByPublicKey(data, publicKey);
- U7 ?) p4 I8 U+ F. a
- H9 i- R" n3 K% }: j2 K) E byte[] decodedData = RSACoder.decryptByPrivateKey(encodedData, 7 c5 t8 O- }- C5 {
privateKey); % _: B2 J! \5 Q3 [: X5 m
) h5 L# V# A9 V# t* G
String outputStr = new String(decodedData);
0 `. z' d3 [1 C Y. v" d System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);
! }1 p" d) k! z8 F+ T assertEquals(inputStr, outputStr);
8 A* I* [% Q0 { , |( h. j1 x/ G$ J
}
5 y* q" i" Q9 t; a ( k5 J- N( L+ ?4 E% ^4 w1 Y
@Test
% N: i; {( s9 j& }% E public void testSign() throws Exception { / `1 b( s8 }$ d! g- ?' P- H
System.err.println("私钥加密——公钥解密");
( ~4 K9 `8 D2 V/ E1 s String inputStr = "sign";
* o) d7 Y- o$ z c: e byte[] data = inputStr.getBytes();
3 C8 E* Y' i ]: A - d$ m6 B/ A9 f0 J7 ]* L
byte[] encodedData = RSACoder.encryptByPrivateKey(data, privateKey); + x- T% c6 t2 a0 G
, \) b' S% U7 t+ ]( G
byte[] decodedData = RSACoder
: m; X6 p+ |; d5 s+ p# w+ X' U- A f2 w .decryptByPublicKey(encodedData, publicKey);
5 E+ A2 A9 X! z7 r$ @5 ~ ; v% c1 `0 a4 D% S# W( B
String outputStr = new String(decodedData);
. _7 b( |( o, Q5 ]' x& r% K4 P8 ], o System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);
# g$ Z- c; c0 V! A assertEquals(inputStr, outputStr);
/ ]# n, A" O7 l1 J+ x
8 n' H" h, Z: h8 u8 S" k System.err.println("私钥签名——公钥验证签名"); : C5 h/ `4 Y5 o/ ~
// 产生签名
) L8 O2 k7 e4 J& y) m6 j+ J4 p String sign = RSACoder.sign(encodedData, privateKey);
$ i6 ^) W- c+ k6 b1 C2 E6 |6 c$ E System.err.println("签名:\r" + sign);
7 V( i, N' `+ t+ m ~% N ! w) R- z+ t# b" c; D# C& f& d5 }( q
// 验证签名 ) V/ a0 X$ |) E* C
boolean status = RSACoder.verify(encodedData, publicKey, sign); # K( z3 V5 f: n5 O4 a3 l
System.err.println("状态:\r" + status);
. j( f. {! E2 f2 T assertTrue(status); . k& r$ K6 G% @% H7 j4 H: Q1 n
. d$ D# L3 ^4 r( W1 a
} ' v6 S$ Z3 r& t+ ?. j4 x: r
/ u" M2 [9 U6 O} |
|