javarsa-九游会j9娱乐平台
rsa演算法加密本來就是很慢的。一般對某個文件加密的話用des演算法加密,然後對密鑰用rsa演算法加密。rsa加密慢的問題目前無法解決;
『貳』 java rsa演算法實現256位密鑰怎麼做
【下載實例】本文介紹rsa2加密與解密,rsa2是rsa的加強版本,在密鑰長度上採用2048, rsa2比rsa更安全,更可靠, 本人的另一篇文章rsa已經發表,有想了解的可以點開下面的rsa文章
『叄』 基於java的rsa對文件加密。java寫的後台
一定要把邏輯寫進jsp嗎?如果是,可以把你的java類import進jsp,然後直接在jsp的<%%>里new一個調用就行。比如<%myclass myclass = new myclass(); string encdata = myclass.rsaencrypt(mydata);%>
『肆』 java加密問題,rsa演算法
無法找到文件異常。樓主確定路徑正確。後綴名正確
fileinputstream f=new fileinputstream("skey_rsa_pub.dat");
樓主你的文件放在什麼位置
『伍』 求救求救。。。剛學習java,有沒有java的rsa完整演算法急。。。
例如:import java.awt.*;
即把java包中awt組件包中所有的類添加進來了,類似的,你要指定某個類就要指定到他的具體位置,可以在類所在的文件包上用「*」,後者吧「*」換成制定的類名;
不知道回答的是否你想要的,希望會對你有幫助!努力加油!
『陸』 怎樣用java實現rsa加密
提供加密,解密,生成密鑰對等方法。�梢願�模��遣灰��螅�裨蛐�駛岬� keypairgen.initialize(key_size, new securerandom()); keypair keypair = keypairgen.genkeypair(); return keypair; } catch (exception e) { throw new exception(e.getmessage()); } } /** * 生成公鑰 * @param molus * @param publicexponent * @return rsapublickey * @throws exception */ public static rsapublickey generatersapublickey(byte[] molus, byte[] publicexponent) throws exception { keyfactory keyfac = null; try { keyfac = keyfactory.getinstance("rsa", new org.bouncycastle.jce.provider.bouncycastleprovider()); } catch (nosuchalgorithmexception ex) { throw new exception(ex.getmessage()); } rsapublickeyspec pubkeyspec = new rsapublickeyspec(new biginteger(molus), new biginteger(publicexponent)); try { return (rsapublickey) keyfac.generatepublic(pubkeyspec); } catch (invalidkeyspecexception ex) { throw new exception(ex.getmessage()); } } /** * 生成私鑰 * @param molus * @param privateexponent * @return rsaprivatekey * @throws exception */ public static rsaprivatekey generatersaprivatekey(byte[] molus, byte[] privateexponent) throws exception { keyfactory keyfac = null; try { keyfac = keyfactory.getinstance("rsa", new org.bouncycastle.jce.provider.bouncycastleprovider()); } catch (nosuchalgorithmexception ex) { throw new exception(ex.getmessage()); } rsaprivatekeyspec prikeyspec = new rsaprivatekeyspec(new biginteger(molus), new biginteger(privateexponent)); try { return (rsaprivatekey) keyfac.generateprivate(prikeyspec); } catch (invalidkeyspecexception ex) { throw new exception(ex.getmessage()); } } /** * 加密 * @param key 加密的密鑰 * @param data 待加密的明文數據 * @return 加密後的數據 * @throws exception */ public static byte[] encrypt(key key, byte[] data) throws exception { try { cipher cipher = cipher.getinstance("rsa", new org.bouncycastle.jce.provider.bouncycastleprovider()); cipher.init(cipher.encrypt_mode, key); int blocksize = cipher.getblocksize();//獲得加密塊大小� i ; } return raw; } catch (exception e) { throw new exception(e.getmessage()); } } /** * 解密 * @param key 解密的密鑰 * @param raw 已經加密的數據 * @return 解密後的明文 * @throws exception */ public static byte[] decrypt(key key, byte[] raw) throws exception { try { cipher cipher = cipher.getinstance("rsa", new org.bouncycastle.jce.provider.bouncycastleprovider()); cipher.init(cipher.decrypt_mode, key); int blocksize = cipher.getblocksize(); bytearrayoutputstream bout = new bytearrayoutputstream(64); int j = 0; while (raw.length - j * blocksize > 0) { bout.write(cipher.dofinal(raw, j * blocksize, blocksize)); j ; } return bout.tobytearray(); } catch (exception e) { throw new exception(e.getmessage()); } } /** * * @param args * @throws exception */ public static void main(string[] args) throws exception { file file = new file("c:/test.html"); fileinputstream in = new fileinputstream(file); bytearrayoutputstream bout = new bytearrayoutputstream(); byte[] tmpbuf = new byte[1024]; int count = 0; while ((count = in.read(tmpbuf)) != -1) { bout.write(tmpbuf, 0, count); tmpbuf = new byte[1024]; } in.close(); byte[] orgdata = bout.tobytearray(); keypair keypair = rsa.generatekeypair(); rsapublickey pubkey = (rsapublickey) keypair.getpublic(); rsaprivatekey prikey = (rsaprivatekey) keypair.getprivate(); byte[] pubmodbytes = pubkey.getmolus().tobytearray(); byte[] pubpubexpbytes = pubkey.getpublicexponent().tobytearray(); byte[] primodbytes = prikey.getmolus().tobytearray(); byte[] pripriexpbytes = prikey.getprivateexponent().tobytearray(); rsapublickey recoverypubkey = rsa.generatersapublickey(pubmodbytes,pubpubexpbytes); rsaprivatekey recoveryprikey = rsa.generatersaprivatekey(primodbytes,pripriexpbytes); byte[] raw = rsa.encrypt(prikey, orgdata); file = new file("c:/encrypt_result.dat"); outputstream out = new fileoutputstream(file); out.write(raw); out.close(); byte[] data = rsa.decrypt(recoverypubkey, raw); file = new file("c:/decrypt_result.html"); out = new fileoutputstream(file); out.write(data); out.flush(); out.close(); } } (責任編輯:雲子)
『柒』 關於java中rsa的問題
【實例下載】本文介紹rsa2加密與解密,rsa2是rsa的加強版本,在密鑰長度上採用2048, rsa2比rsa更安全,更可靠, 本人的另一篇文章rsa已經發表,有想了解的可以點開下面的rsa文章
『捌』 java通過rsa演算法獲取公私鑰對 將公鑰提供出去 如何獲取字元串的公鑰
直接將公匙byte數組轉換為16進制的串啊
private static char hextable[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
public static string tohexstring(byte bytes[])
{
stringbuilder sb = new stringbuilder();
for (int i = 0; i < bytes.length; i )
{
char chars[] = new char[2];
int d = (bytes[i] & 240) >> 4;
int m = bytes[i] & 15;
chars[0] = hextable[d];
chars[1] = hextable[m];
sb.append(chars);
}
return sb.tostring();
}
『玖』 java中rsa的方式如何實現非對稱加密的示例
代碼如下,需要依賴一個jar包commons-codec-1.9.jar,用於base64轉換,請自行下載。
importorg.apache.commons.codec.binary.base64;
importjavax.crypto.badpaddingexception;
importjavax.crypto.cipher;
importjavax.crypto.illegalblocksizeexception;
importjava.io.bytearrayoutputstream;
importjava.io.unsupportedencodingexception;
importjava.security.*;
importjava.security.interfaces.rsaprivatekey;
importjava.security.interfaces.rsapublickey;
importjava.security.spec.pkcs8encodedkeyspec;
importjava.security.spec.x509encodedkeyspec;
publicclassrsautils{
//加密方式
="rsa";
//簽名演算法
_algorithm="sha1withrsa";
//創建密鑰對初始長度
privatestaticfinalintkey_size=512;
//字元編碼格式
="utf-8";
//rsa最大加密明文大小
privatestaticfinalintmax_encrypt_block=117;
//rsa最大解密密文大小
privatestaticfinalintmax_decrypt_block=128;
privatekeyfactorykeyfactory;
publicrsautils(){
keyfactory=keyfactory.getinstance(algorithm);
}
/**
*私鑰加密
*
*@paramcontent待加密字元串
*@paramprivatekey私鑰
*@return加密後字元串(base64編碼)
*/
(stringcontent,stringprivatekey)throwsexception{
stringresult;
try(bytearrayoutputstreamout=newbytearrayoutputstream()){
byte[]keybytes=newbase64().decode(privatekey);
=newpkcs8encodedkeyspec(keybytes);
privatekeypkey=keyfactory.generateprivate(pkcs8keyspec);
ciphercipher=cipher.getinstance(algorithm);
cipher.init(cipher.encrypt_mode,pkey);
byte[]data=content.getbytes(charset);
write2stream(cipher,data,out);
byte[]resultbytes=out.tobytearray();
result=base64.encodebase64string(resultbytes);
}catch(exceptione){
thrownewexception(e);
}
returnresult;
}
/**
*公鑰解密
*
*@paramcontent已加密字元串(base64加密)
*@parampublickey公鑰
*@return
*/
(stringcontent,stringpublickey)throwsexception{
stringresult="";
try(bytearrayoutputstreamout=newbytearrayoutputstream()){
byte[]keybytes=newbase64().decode(publickey);
x509encodedkeyspecx509keyspec=newx509encodedkeyspec(keybytes);
publickeypkey=keyfactory.generatepublic(x509keyspec);
ciphercipher=cipher.getinstance(algorithm);
cipher.init(cipher.decrypt_mode,pkey);
byte[]data=base64.decodebase64(content);
write2stream(cipher,data,out);
byte[]resultbytes=out.tobytearray();
result=newstring(resultbytes);
}catch(exceptione){
thrownewexception(e);
}
returnresult;
}
/**
*公鑰加密
*
*@paramcontent待加密字元串
*@parampublickey公鑰
*@return加密後字元串(base64編碼)
*/
(stringcontent,stringpublickey)throwsexception{
stringresult="";
try(bytearrayoutputstreamout=newbytearrayoutputstream()){
byte[]keybytes=newbase64().decode(publickey);
x509encodedkeyspecx509keyspec=newx509encodedkeyspec(keybytes);
publickeypkey=keyfactory.generatepublic(x509keyspec);
ciphercipher=cipher.getinstance(algorithm);
cipher.init(cipher.encrypt_mode,pkey);
byte[]data=content.getbytes(charset);
write2stream(cipher,
data,out);
byte[]resultbytes=out.tobytearray();
result=base64.encodebase64string(resultbytes);
}catch(exceptione){
thrownewexception(e);
}
returnresult;
}
/**
*私鑰解密
*
*@paramcontent已加密字元串
*@paramprivatekey私鑰
*@return解密後字元串
*/
(stringcontent,stringprivatekey)throwsexception{
stringresult="";
try(bytearrayoutputstreamout=newbytearrayoutputstream()){
byte[]keybytes=newbase64().decode(privatekey);
=newpkcs8encodedkeyspec(keybytes);
privatekeypkey=keyfactory.generateprivate(pkcs8keyspec);
ciphercipher=cipher.getinstance(algorithm);
cipher.init(cipher.decrypt_mode,pkey);
byte[]data=base64.decodebase64(content);
write2stream(cipher,data,out);
byte[]resultbytes=out.tobytearray();
result=newstring(resultbytes);
}catch(exceptione){
thrownewexception(e);
}
returnresult;
}
privatestaticvoidwrite2stream(ciphercipher,byte[]data,bytearrayoutputstreamout)throws
badpaddingexception,illegalblocksizeexception{
intdatalen=data.length;
intoffset=0;
byte[]cache;
inti=0;
//對數據分段解密
while(datalen-offset>0){
if(datalen-offset>max_decrypt_block){
cache=cipher.dofinal(data,offset,max_decrypt_block);
}else{
cache=cipher.dofinal(data,offset,datalen-offset);
}
out.write(cache,0,cache.length);
i ;
offset=i*max_decrypt_block;
}
}
/**
*用私鑰對信息生成數字簽名
*
*@paramdata已加密數據
*@paramprivatekey私鑰(base64編碼)
*@returnsign
*/
publicstringsign(stringdata,stringprivatekey)throwsexception{
stringresult="";
try{
byte[]keybytes=newbase64().decode(privatekey);
=newpkcs8encodedkeyspec(keybytes);
privatekeyprivatek=keyfactory.generateprivate(pkcs8keyspec);
signaturesignature=signature.getinstance(signature_algorithm);
signature.initsign(privatek);
signature.update(parse2hexstr(data).getbytes(charset));
result=newbase64().encodetostring(signature.sign());
}catch(exceptione){
thrownewexception(e);
}
returnresult;
}
/**
*校驗數字簽名
*
*@paramdata已加密數據
*@parampublickey公鑰(base64編碼)
*@paramsign數字簽名
*@return
*@throwsexception
*/
publicbooleanverify(stringdata,stringpublickey,stringsign)throwsexception{
booleanresult;
try{
byte[]keybytes=newbase64().decode(publickey);
x509encodedkeyspeckeyspec=newx509encodedkeyspec(keybytes);
publickeypublick=keyfactory.generatepublic(keyspec);
signaturesignature=signature.getinstance(signature_algorithm);
signature.initverify(publick);
signature.update(parse2hexstr(data).getbytes(charset));
result=signature.verify(newbase64().decode(sign));
}catch(exceptione){
thrownewexception(e);
}
returnresult;
}
/**
*將二進制轉換成16進制
*
*@paramdata
*@return
*/
(stringdata)throwsexception{
stringresult="";
try{
byte[]buf=data.getbytes(charset);
stringbuffersb=newstringbuffer();
for(inti=0;istringhex=integer.tohexstring(buf[i]&0xff);
if(hex.length()==1){
hex='0' hex;
}
sb.append(hex.touppercase());
}
result=sb.tostring();
}catch(unsupportedencodingexceptione){
thrownewexception(e);
}
returnresult;
}
/**
*生成公鑰與私鑰
*/
publicstaticvoidcreatekey()throwsexception{
try{
=keypairgenerator.getinstance(algorithm);
keypairgenerator.initialize(key_size);
keypairkeypair=keypairgenerator.generatekeypair();
rsapublickeyrsapublickey=(rsapublickey)keypair.getpublic();
rsaprivatekeyrsaprivatekey=(rsaprivatekey)keypair.getprivate();
stringpublickey=base64.encodebase64string(rsapublickey.getencoded());
stringprivatekey=base64.encodebase64string(rsaprivatekey.getencoded());
system.out.println("publickey=" publickey " privatekey=" privatekey);
}catch(nosuchalgorithmexceptione){
thrownewexception(e);
}
}
publicstaticvoidmain(string[]args)throwsexception{
stringprivate_key=" m /fns1bmgfjhi8lhr/o/hy8efb/i/ddylcccu4bcltxpki8edc kjr2wvyyfnvmwee// w5c leseoaqdo5nahrzsl8bidoxten2j dsa///1qx t8f5wd8i/8gu702pecwkgi5ymrarq / /nkeftq0snpudvbgxvpji9/fouf";
stringpublic_key=" lc///nfovkvqndzdh60dzlgomde0nbrtn/5zejgwjbvdlvcfoihwidaqab";
rsautilsrsautil=newrsautils();
stringencryptbypublickey=rsautil.encryptbypublickey("你好!",public_key);
system.out.println(encryptbypublickey);
stringdecryptbyprivatekey=rsautil.decryptbyprivatekey(encryptbypublickey,private_key);
system.out.println(decryptbyprivatekey);
stringencryptbyprivatekey=rsautil.encryptbyprivatekey("你好!",private_key);
system.out.println(encryptbyprivatekey);
stringdecryptbypublickey=rsautil.decryptbypublickey(encryptbyprivatekey,public_key);
system.out.println(decryptbypublickey);
stringsign=rsautil.sign("1234",private_key);
system.out.println("sign:" sign);
system.out.println(rsautil.verify("1234",public_key,sign));
}
}