資源簡介
JAVA 實現的RSA加密算法,一個base64工具 ,一個RSA算法

代碼片段和文件信息
package?cn.com.csii.utility;
import?java.io.ByteArrayInputStream;
import?java.io.ByteArrayOutputStream;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.FileOutputStream;
import?java.io.InputStream;
import?java.io.OutputStream;
?
import?org.apache.commons.codec.binary.base64;
?
/**
?*?
?*?base64編碼解碼工具包
?*?
?*?
?*?依賴javabase64-1.3.1.jar
?*?
?*?
?*?@author?IceWee
?*?@date?2012-5-19
?*?@version?1.0
?*/
public?class?base64Utils?{
?
????/**
?????*?文件讀取緩沖區大小
?????*/
????private?static?final?int?CACHE_SIZE?=?1024;
?????
????/**
?????*?
?????*?base64字符串解碼為二進制數據
?????*?
?????*?
?????*?@param?base64
?????*?@return
?????*?@throws?Exception
?????*/
????public?static?byte[]?decode(String?base64)?throws?Exception?{
???? base64?base64In?=?new?base64();
????????return?base64In.decode(base64.getBytes());
????}
?????
????/**
?????*?
?????*?二進制數據編碼為base64字符串
?????*?
?????*?
?????*?@param?bytes
?????*?@return
?????*?@throws?Exception
?????*/
????public?static?String?encode(byte[]?bytes)?throws?Exception?{
???? base64?base64In?=?new?base64();
????????return?new?String(base64In.encode(bytes));
????}
?????
????/**
?????*?
?????*?將文件編碼為base64字符串
?????*?
?????*?
?????*?大文件慎用,可能會導致內存溢出
?????*?
?????*?
?????*?@param?filePath?文件絕對路徑
?????*?@return
?????*?@throws?Exception
?????*/
????public?static?String?encodeFile(String?filePath)?throws?Exception?{
????????byte[]?bytes?=?fileToByte(filePath);
????????return?encode(bytes);
????}
?????
????/**
?????*?
?????*?base64字符串轉回文件
?????*?
?????*?
?????*?@param?filePath?文件絕對路徑
?????*?@param?base64?編碼字符串
?????*?@throws?Exception
?????*/
????public?static?void?decodeToFile(String?filePath?String?base64)?throws?Exception?{
????????byte[]?bytes?=?decode(base64);
????????byteArrayToFile(bytes?filePath);
????}
?????
????/**
?????*?
?????*?文件轉換為二進制數組
?????*?
?????*?
?????*?@param?filePath?文件路徑
?????*?@return
?????*?@throws?Exception
?????*/
????public?static?byte[]?fileToByte(String?filePath)?throws?Exception?{
????????byte[]?data?=?new?byte[0];
????????File?file?=?new?File(filePath);
????????if?(file.exists())?{
????????????FileInputStream?in?=?new?FileInputStream(file);
????????????ByteArrayOutputStream?out?=?new?ByteArrayOutputStream(2048);
????????????byte[]?cache?=?new?byte[CACHE_SIZE];
????????????int?nRead?=?0;
????????????while?((nRead?=?in.read(cache))?!=?-1)?{
????????????????out.write(cache?0?nRead);
????????????????out.flush();
????????????}
????????????out.close();
????????????in.close();
????????????data?=?out.toByteArray();
?????????}
????????return?data;
????}
?????
????/**
?????*?
?????*?二進制數據寫文件
?????*?
?????*?
?????*?@param?bytes?二進制數據
?????*?@param?filePath?文件生成目錄
?????*/
????public?static?void?byteArrayToFile(byte[]?bytes?String?filePath)?throws?Exception?{
????????InputStream?in?=?new?ByteArrayInputStream(by
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????3647??2016-02-18?16:56??ba
?????文件???????10507??2016-02-18?16:21??RSAUtils.java
- 上一篇:Student_Manage.zip
- 下一篇:jpcap資源
評論
共有 條評論