當前位置:首頁 » 密碼管理 » 加密解密組件java

加密解密組件java

發布時間: 2025-10-14 05:23:24

『壹』 如何用java實現文件(不只是txt文本)的整體加密解密

importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.security.KeyPair;
importjava.security.KeyPairGenerator;
importjava.security.NoSuchAlgorithmException;
importjava.security.interfaces.RSAPrivateKey;
importjava.security.interfaces.RSAPublicKey;
importjavax.crypto.Cipher;

/**
*文件加密解密
*加解密需要依靠以下四個屬性,
;
staticKeyPairkeyPair;
staticRSAPrivateKeyprivateKey;
staticRSAPublicKeypublicKey;
*@authoryoung
*
*/
publicclassRSAEncrypt{

;

staticKeyPairkeyPair;

staticRSAPrivateKeyprivateKey;

staticRSAPublicKeypublicKey;

static{
try{
//實例類型
keyPairGen=KeyPairGenerator.getInstance("RSA");
//初始化長度
keyPairGen.initialize(512);
//聲場KeyPair
keyPair=keyPairGen.generateKeyPair();
//Generatekeys
privateKey=(RSAPrivateKey)keyPair.getPrivate();
publicKey=(RSAPublicKey)keyPair.getPublic();
}catch(NoSuchAlgorithmExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}

publicstaticvoidmain(String[]args){
RSAEncryptencrypt=newRSAEncrypt();
Filefile=newFile(
"C:\DocumentsandSettings\Administrator.DCB5E0D91E0D436\桌面\sdf.txt");
FilenewFile=newFile(
"C:\DocumentsandSettings\Administrator.DCB5E0D91E0D436\桌面\sdf1.txt");
encrypt.encryptFile(encrypt,file,newFile);
Filefile1=newFile(
"C:\DocumentsandSettings\Administrator.DCB5E0D91E0D436\桌面\sdf1.txt");
FilenewFile1=newFile(
"C:\DocumentsandSettings\Administrator.DCB5E0D91E0D436\桌面\sdf2.txt");
encrypt.decryptFile(encrypt,file1,newFile1);
}

/**
*加密文件
*@paramencryptRSAEncrypt對象
*@paramfile源文件
*@paramnewFile目標文件
*/
publicvoidencryptFile(RSAEncryptencrypt,Filefile,FilenewFile){
try{
InputStreamis=newFileInputStream(file);
OutputStreamos=newFileOutputStream(newFile);

byte[]bytes=newbyte[53];
while(is.read(bytes)>0){
byte[]e=encrypt.encrypt(RSAEncrypt.publicKey,bytes);
bytes=newbyte[53];
os.write(e,0,e.length);
}
os.close();
is.close();
System.out.println("writesuccess");
}catch(Exceptione){
e.printStackTrace();
}
}

/**
*解密文件
*@paramencryptRSAEncrypt對象
*@paramfile
*@paramnewFile
*/
publicvoiddecryptFile(RSAEncryptencrypt,Filefile,FilenewFile){
try{
InputStreamis=newFileInputStream(file);
OutputStreamos=newFileOutputStream(newFile);
byte[]bytes1=newbyte[64];
while(is.read(bytes1)>0){
byte[]de=encrypt.decrypt(RSAEncrypt.privateKey,bytes1);
bytes1=newbyte[64];
os.write(de,0,de.length);
}
os.close();
is.close();
System.out.println("writesuccess");

}catch(Exceptione){
e.printStackTrace();
}
}

/**
*加密實現
**EncryptString.*
*
*@returnbyte[]加密後的位元組數組
*/
protectedbyte[]encrypt(RSAPublicKeypublicKey,byte[]obj){
if(publicKey!=null){
try{
Ciphercipher=Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE,publicKey);
returncipher.doFinal(obj);
}catch(Exceptione){
e.printStackTrace();
}
}
returnnull;
}

/**
*解密實現
**Basicdecryptmethod*
*
*@returnbyte[]解密後的位元組數組
*/
protectedbyte[]decrypt(RSAPrivateKeyprivateKey,byte[]obj){
if(privateKey!=null){
try{
Ciphercipher=Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE,privateKey);
returncipher.doFinal(obj);
}catch(Exceptione){
e.printStackTrace();
}
}
returnnull;
}
}

『貳』 java實現ase加密解密

這個演算法java SDK自帶的額 參考代碼如下:

/**解密

*@paramcontent待解密內容

*@parampassword解密密鑰

*@return

*/

publicstaticbyte[]decrypt(byte[]content,Stringpassword){

try{

KeyGeneratorkgen=KeyGenerator.getInstance("AES");

kgen.init(128,newSecureRandom(password.getBytes()));

SecretKeysecretKey=kgen.generateKey();

byte[]enCodeFormat=secretKey.getEncoded();

SecretKeySpeckey=newSecretKeySpec(enCodeFormat,"AES");

Ciphercipher=Cipher.getInstance("AES");//創建密碼器

cipher.init(Cipher.DECRYPT_MODE,key);//初始化

byte[]result=cipher.doFinal(content);

returnresult;//加密

}catch(NoSuchAlgorithmExceptione){

e.printStackTrace();

}catch(NoSuchPaddingExceptione){

e.printStackTrace();

}catch(InvalidKeyExceptione){

e.printStackTrace();

}catch(IllegalBlockSizeExceptione){

e.printStackTrace();

}catch(BadPaddingExceptione){

e.printStackTrace();

}

returnnull;

}



/**

*加密

*

*@paramcontent需要加密的內容

*@parampassword加密密碼

*@return

*/

publicstaticbyte[]encrypt(Stringcontent,Stringpassword){

try{

KeyGeneratorkgen=KeyGenerator.getInstance("AES");

kgen.init(128,newSecureRandom(password.getBytes()));

SecretKeysecretKey=kgen.generateKey();

byte[]enCodeFormat=secretKey.getEncoded();

SecretKeySpeckey=newSecretKeySpec(enCodeFormat,"AES");

Ciphercipher=Cipher.getInstance("AES");//創建密碼器

byte[]byteContent=content.getBytes("utf-8");

cipher.init(Cipher.ENCRYPT_MODE,key);//初始化

byte[]result=cipher.doFinal(byteContent);

returnresult;//加密

}catch(NoSuchAlgorithmExceptione){

e.printStackTrace();

}catch(NoSuchPaddingExceptione){

e.printStackTrace();

}catch(InvalidKeyExceptione){

e.printStackTrace();

}catch(UnsupportedEncodingExceptione){

e.printStackTrace();

}catch(IllegalBlockSizeExceptione){

e.printStackTrace();

}catch(BadPaddingExceptione){

e.printStackTrace();

}

returnnull;

}

http://blog.csdn.net/hbcui1984/article/details/5201247
圖像界面的話就不說了
熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:724
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:990
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:699
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:855
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:756
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1097
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:329
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:205
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:894
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:854