JDK中JCA的簡單使用(二) RSA加簽驗籤

2021-08-29 07:48:30 字數 3144 閱讀 5116

請注意這裡是加簽驗籤,如有加密解密需求的,還請移步

signature類是乙個引擎類,提供加密的數字簽名演算法,例如dsa或rsawithmd5。加密安全簽名演算法採用任意大小的輸入和私鑰,並生成乙個相對較短(通常是固定大小)的位元組串——簽名。

簡而言之,

1.建立一對key:私鑰&公鑰。

2.使用私鑰建立簽名

3.使用公鑰對資料的真實性進行校驗。

值得注意的是,signature演算法目的並不在於對原資料加密,而是使用原資料和私鑰/公鑰生成簽名,驗簽時,使用公鑰/私鑰,簽名,原資料進行校驗。

應用場景:用於需要使用這份資料,又不能確定接收資料的可靠性。

使用流程:

1.獲取公私鑰對,**如下:

/**

* 獲取公私鑰對

** @return

*/private static mapgetkeypair() throws nosuchalgorithmexception

2.使用私鑰對資料進行加簽,獲得簽名字串,**如下:

/**

* 根據私鑰加簽

** @param privatekey

* @param data

* @return

*/public static string sign(privatekey privatekey, string data) throws exception

3.根據公鑰驗籤

/**

* 根據公鑰驗籤

** @param publickey

* @param signstr

* @param data

* @return

*/public static boolean vertify(publickey publickey, string signstr, string data) throws exception

完整的方法如下:

public final static string key_algorithm = "rsa";

public final static string algorithm = "md5withrsa";

public final static string private_key = "privatekey";

public final static string public_key = "publickey";

/*** 獲取公私鑰對

** @return

*/private static mapgetkeypair() throws nosuchalgorithmexception

/*** 根據字串獲取私鑰

** @param privatekeystr

* @return

*/public static privatekey getprivatekey(string privatekeystr) throws nosuchalgorithmexception, invalidkeyspecexception

/*** 根據字串獲取公鑰

** @param publickeystr

* @return

*/public static publickey getpublickey(string publickeystr) throws nosuchalgorithmexception, invalidkeyspecexception

/*** 根據keymap獲取私鑰字串

** @param keymap

* @return

*/public static string getprivatekeystr(mapkeymap)

/*** 根據私鑰獲取私鑰字串

** @param privatekey

* @return

*/public static string getprivatekeystr(privatekey privatekey)

/*** 根據keymap獲取公鑰字串

** @param keymap

* @return

*/public static string getpublickeystr(mapkeymap)

/*** 根據公鑰獲取公鑰字串

** @param publickey

* @return

*/public static string getpublickeystr(publickey publickey)

/*** 根據私鑰字串加簽

** @param privatekeystr 私鑰字串

* @param data 需要加簽的資料

* @return

*/public static string sign(string privatekeystr, string data) throws exception

/*** 根據私鑰加簽

** @param privatekey

* @param data

* @return

*/public static string sign(privatekey privatekey, string data) throws exception

/*** 根據公鑰字串驗籤

** @param publickeystr

* @param signstr

* @param data

* @return

*/public static boolean vertify(string publickeystr, string signstr, string data) throws exception

/*** 根據公鑰驗籤

** @param publickey

* @param signstr

* @param data

* @return

*/public static boolean vertify(publickey publickey, string signstr, string data) throws exception

JDK中JCA的簡單使用(四) Mac加密

訊息認證碼 mac 提供了一種檢查在不可靠介質上傳輸或儲存在不可靠介質中的資訊的完整性的方法,只有擁有正確金鑰的人才能驗證收到的訊息。基於加密雜湊函式的mac機制被稱為hmac。hmac可以與任何加密雜湊函式 例如,sha 256 一起使用,並結合秘密共享金鑰。應用場景 介面簽名校驗,防止抓包偽造請...

JDK中JCA的簡單使用(一) MD5加密

其實md5加密就是用到了messagedigest類的一種加密演算法 messagedigest類是乙個引擎類,提供加密的安全訊息功能,如sha 256,sha 512,md5。加密安全訊息摘要採用任意大小的輸入 位元組陣列 並生成固定大小的輸出。應用場景 加密某段資料,不需要解密,比如資料庫儲存密...

JDK8中Optional的方法使用

1.optional.of 或者optional.ofnullable 建立optional物件,差別在於of不允許引數是null,而ofnullable則無限制。test public void testoptional 2.ispresent 判斷值是否存在 ispresent判斷值是否存在 s...