您好,登錄后才能下訂單哦!
1 package jdbc.pro.lin; 2 3 import java.security.InvalidKeyException; 4 import java.security.NoSuchAlgorithmException; 5 import java.security.PrivateKey; 6 import java.security.PublicKey; 7 import java.security.Signature; 8 import java.security.SignatureException; 9 import java.util.Map; 10 11 import jdbc.pro.lin.MyRSA; 12 13 /** 14 * RSA數字簽名,借用MyRSA中的算法,不再重復 15 * 數字簽名遵循“私鑰簽名,公鑰驗簽”原則,因為私鑰是個人身份認證 16 * @author Kinsley 17 * 18 */ 19 public class MySignature { 20 21 /** 數字簽名算法。JDK只提供了MD2withRSA, MD5withRSA, SHA1withRSA,其他的算法需要第三方包才能支持 */ 22 public static final String SIGNATURE_ALGORITHM = "SHA1withRSA"; 23 24 public static final String PLAIN_TEXT = "MANUTD is the greatest club in the world"; 25 public static void main(String[] args) 26 { 27 //建立兩套公私鑰對 28 Map<String, byte[]> keyMap1 = MyRSA.generateKeyBytes(); 29 PublicKey publicKey1 = MyRSA.restorePublicKey(keyMap1.get(MyRSA.PUBLIC_KEY)); 30 PrivateKey privateKey1 = MyRSA.restorePrivateKey(keyMap1.get(MyRSA.PRIVATE_KEY)); 31 32 Map<String, byte[]> keyMap2 = MyRSA.generateKeyBytes(); 33 PublicKey publicKey2 =MyRSA.restorePublicKey(keyMap2.get(MyRSA.PUBLIC_KEY)); 34 PrivateKey privateKey2 =MyRSA.restorePrivateKey(keyMap2.get(MyRSA.PRIVATE_KEY)); 35 36 /** 假設現在A簽名后向B發送消息 37 * A用B的公鑰進行加密 38 * 用自己A的私鑰進行簽名 39 */ 40 byte[] encodedText = MyRSA.RSAEncode(publicKey2, PLAIN_TEXT.getBytes()); 41 byte[] signature = sign(privateKey1, PLAIN_TEXT.getBytes()); 42 43 /** 44 * 現在B收到了A的消息,進行兩步操作 45 * 用B的私鑰解密得到明文 46 * 將明文和A的公鑰進行驗簽操作 47 */ 48 49 byte[] decodedText = MyRSA.RSADecode(privateKey2, encodedText).getBytes(); 50 System.out.println("Decoded Text: " + new String(decodedText)); 51 52 System.out.println("Signature is " + verify(publicKey1, signature, decodedText)); 53 } 54 55 /** 56 * 簽名,三步走 57 * 1. 實例化,傳入算法 58 * 2. 初始化,傳入私鑰 59 * 3. 簽名 60 * @param key 61 * @param plainText 62 * @return 63 */ 64 public static byte[] sign(PrivateKey privateKey, byte[] plainText) 65 { 66 try { 67 //實例化 68 Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); 69 70 //初始化,傳入私鑰 71 signature.initSign(privateKey); 72 73 //更新 74 signature.update(plainText); 75 76 //簽名 77 return signature.sign(); 78 79 } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) { 80 // TODO Auto-generated catch block 81 e.printStackTrace(); 82 } 83 84 return null; 85 } 86 87 /** 88 * 驗簽,三步走 89 * 1. 實例化,傳入算法 90 * 2. 初始化,傳入公鑰 91 * 3. 驗簽 92 * @param publicKey 93 * @param signatureVerify 94 * @param plainText 95 * @return 96 */ 97 public static boolean verify(PublicKey publicKey, byte[] signatureVerify, byte[] plainText ) 98 { 99 try {100 //實例化101 Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);102 103 //初始化104 signature.initVerify(publicKey);105 106 //更新107 signature.update(plainText);108 109 //驗簽110 return signature.verify(signatureVerify);111 } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {112 // TODO Auto-generated catch block113 e.printStackTrace();114 }115 116 return false;117 }118 }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。