RSA加密和解密使用經歷說明

2021-10-02 08:10:22 字數 3360 閱讀 5618

rsa加密和解密使用經歷說明

/**

* rsa金鑰資訊生成工具類

*/public class rsaencrypt2048 catch (nosuchalgorithmexception e)

// 初始化金鑰對生成器,金鑰大小為96-1024位

keypairgen.initialize(init_key_size, new securerandom());

// 生成乙個金鑰對,儲存在keypair中

keypair keypair = keypairgen.generatekeypair();

return keypair; }

/*** 私鑰解密過程

** @param privatekey 私鑰

* @param cipherdata 密文資料

* @return 明文

* @throws exception 解密過程中的異常資訊

*/public static byte decrypt(rsaprivatekey privatekey, string cipherdata)

throws exception

cipher cipher = null;

//byte encrypteddata= cipherdata.getbytes();

try else

out.write(cache, 0, cache.length);

i++;

offset = i * max_decrypt_block;

}byte encrypteddata = out.tobytearray();

out.close();

// byte output = cipher.dofinal(encryptinfo);

return encrypteddata;

} catch (nosuchalgorithmexception e) catch (nosuchpaddingexception e) catch (invalidkeyexception e) catch (illegalblocksizeexception e) catch (badpaddingexception e)

}/**

* 公鑰解密過程

** @param publickey 公鑰

* @param cipherdata 密文資料

* @return 明文

* @throws exception 解密過程中的異常資訊

*/public static byte decrypt(rsapublickey publickey, string cipherdata)

throws exception

cipher cipher = null;

try else

out.write(cache, 0, cache.length);

i++;

offset = i * max_decrypt_block;

}byte encrypteddata = out.tobytearray();

out.close();

return encrypteddata;

} catch (nosuchalgorithmexception e) catch (nosuchpaddingexception e) catch (invalidkeyexception e) catch (illegalblocksizeexception e) catch (badpaddingexception e)

}public static rsaprivatekey loadprivatekeybystr(string privatekeystr)

throws exception catch (nosuchalgorithmexception e) catch (invalidkeyspecexception e) catch (nullpointerexception e)

}/**

* 從字串中載入公鑰

** @param publickeystr 公鑰資料字串

* @throws exception 載入公鑰時產生的異常

*/public static rsapublickey loadpublickeybystr(string publickeystr)

throws exception catch (nosuchalgorithmexception e) catch (invalidkeyspecexception e) catch (nullpointerexception e)

}/**

* 從檔案中輸入流中載入公鑰

** @throws exception 載入公鑰時產生的異常

* @para path 公鑰輸入流

*/public static string loadpublickeybyfile(string path) throws exception

br.close();

return sb.tostring();

} catch (ioexception e) catch (nullpointerexception e)

}/**

* 公鑰加密過程

** @param publickey 公鑰

* @param plaintextdata 明文資料

* @return

* @throws exception 加密過程中的異常資訊

*/public static byte encrypt(rsapublickey publickey, string plaintextdata)

throws exception

cipher cipher = null;

try else

out.write(cache, 0, cache.length);

i++;

offset = i * max_encrypt_block;

}encryptinfo = out.tobytearray();

out.close();

// byte output = cipher.dofinal(plaintextdata.getbytes());

return encryptinfo;

} catch (nosuchalgorithmexception e) catch (nosuchpaddingexception e) catch (invalidkeyexception e) catch (illegalblocksizeexception e) catch (badpaddingexception e)

}}

RSA 加密解密使用例項

本文不討論rsa加密解密本身,只記錄使用方法及遇到的坑,rsa原理及注意事項可在網上查詢。背景 公司的乙個需求,要求對接客戶的乙個平台,通訊方式為mqtt,資料報含token及json內容,在通訊過程中傳送的mqtt資訊必須帶上token,這個token是要從客戶平台獲取,通過http協議,post...

加密解密 使用對稱密碼加密檔案

造冰箱的大熊貓 cnblogs 2019 5 24 1 加密 gpg c file to be encrypted 其中,file to be encrypted 是待加密檔案。gpg只能加密檔案,不能加密資料夾。要對資料夾進行加密,需先將資料夾進行打包。加密後的檔案為 file to be enc...

C 使用RSA證書檔案加密和解密示例

修改msdn上的示例,使之可以通過rsa證書檔案加密和解密,中間遇到乙個小問題。q 執行exportparameters 方法時,回報cryptographicexception 該項不適於在指定狀態下使用 key not valid for use in specified state a 匯入帶...