javarsa文件加密解密
這個我不清楚。
對文件加密,我使用的是超級加密3000.
超級加密3000採用國際上成熟的加密演算法和安全快速的加密方法,可以有效保障數據安全!
具體操作方法:
1下載安裝超級加密3000。
2 然後在需要加密的文件上單擊滑鼠右鍵選擇加密。
3 在彈出的文件加密窗口中設置文件加密密碼就OK了。
超級加密3000的下載地址你可以在網路上搜索超級加密3000,第一個就是。
② 有一段用java實現rsa加解密的程序看不懂,希望高手幫我做下注釋,詳細些,謝謝
//引入文件
import java.security.*;
import javax.crypto.*;
/**
* RSACryptography
* RSACryptography use the privated key to encrypt the plain text and decrypt
* the cipher text with the public key
*/
public class RSACryptography {
Cipher cipher;
/**
構造函數,就是你每次new這個對象RSACryptography 時候就會執行裡面的方法
返回一個Cipher對象(其實他就是用來加密解密的)
*/
public RSACryptography() {
try {
cipher = Cipher.getInstance("RSA");//返回一個cipher對象,該類
//應該是單例的
} catch (NoSuchAlgorithmException e) {//拋出異常,沒什麼說的
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}
/**
好了,重點來了,你需要加密解密的就調用這個方法encrypt_decrypt(),傳入一個byte[]的類型值byteInput,,就是你要加密的東西,在傳入一個key,這個key 就像鑰匙一樣,你根據這個key進行加密,也可以根據這個key進行解密的,boolean 類型的 crypto,如果true就是加密,false就是解密
*/
public byte[] encrypt_decrypt(byte[] byteInput, Key key, boolean crypto) {
try {
if(crypto){
cipher.init(Cipher.ENCRYPT_MODE,key);//加密前初始化
}else{
cipher.init(Cipher.DECRYPT_MODE,key);//解密前初始化
}
byte[] cipherByte = cipher.doFinal(byteInput);//進行加密或解密
return cipherByte;//返回你的加密或者解密值類型為byte[]
} catch (InvalidKeyException e) {//拋出異常
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
}
③ java RSA 加密解密,漢字亂碼怎麼辦
這個問題不是加密和解密的事件,而是沒能正確地使用相同的字元集編碼。
加密之前就應該約定雙方使用同一個字元集編碼來做編解碼,比如用 UTF8,我們在用一個 byte[] 創建一個 String 時應該明確地指定字元集編碼而不能使用默認值,因此默認值是跟操作系統或程序啟動時的命令行有依賴關系的,這很難預料誰會使用你的程序,又會如何使用你的程序,我們明確地指定字元集就不會受此影響。
String a = "漢字「
byte[] bytes = a.getBytes("UTF8");
byte[] encoded = encode(bytes);
byte[] decoded = decode(encoded);
String received = new String(decoded, "UTF8");
④ 怎樣用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(); } } (責任編輯:雲子)
⑤ 怎麼在ios進行rsa公鑰加密,java做rsa私鑰解密
1、用公鑰加密,用私鑰解密。
2、給別人發信息,就從伺服器上拉下來別人的公鑰,加密後發給他。
3、對方拿到信息後用自己的私鑰解密。
4、這樣,公鑰加密後除了私鑰持有人,別人都看不到信息。
5、若是用私鑰加密,那麼公鑰都能解密,還有何安全性可言?
6、私鑰加密的場合只有一個,那就是數字簽名,用來表明這個信息來源於你。
⑥ java中RSA用私鑰加密公鑰解密問題
公鑰和私鑰可以互換的用,用公鑰加密私鑰解密,用私鑰加密公鑰解密都ok,方法一樣
⑦ RSA演算法如何加密文件,請教。。。java
RSA演算法很簡單,就是基於歐拉定理的簡單演算法 M=5是明文,計算過程如下: n=p*q=33; (p-1)*(q-1)=20; 加密:y=密文,x=明文=5; y=x^e mod n = 5^7 mod 33 = 14; 解密: x=y^d mod n; d*e= 1 [mod(p-1)*(q-1)]; 7d=1(mod 20)所以d=3; 所以x=y^d mod n= 14^3 mod 33 = 5;解完 加密由5~14,解密由14~5,實現了RSA演算法的加密解密過程,證明了計算的正確性。
⑧ Java RSA解密
根據已知的公鑰m與e生成PublicKey,然後加密,需要用到bouncycastle這個庫,大致代碼如下:
//生成m與e
byte[]mBytes=Hex.decode("C535AD4F...略");
BigIntegerm=newBigInteger(1,mBytes);
BigIntegere=BigInteger.valueOf(0x10001);
//恢復公鑰
KeyFactorykeyFactory=KeyFactory.getInstance("RSA");
PublicKeypublicKey=keyFactory.generatePublic(newRSAPublicKeySpec(m,e));
/*根據經驗,那個JS加密後的密文用Java解出來是反轉的字元串,
*所以如果想要達到與JS完全一致的效果的話,需要將明文先反轉,
*即將"admin"變成"nimda"
*/
byte[]data=newStringBuilder("admin").reverse().toString().getBytes();
//現在可以用publicKey加密了
Ciphercipher=Cipher.getInstance("RSA",newBouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE,publicKey);
byte[]enc=cipher.doFinal(data);
System.out.println(Hex.toHexString(enc));
理論上代碼大概是這樣。加密結果是:
⑨ Java 第三方公鑰 RSA加密求助
下面是RSA加密代碼。
/**
* RSA演算法,實現數據的加密解密。
* @author ShaoJiang
*
*/
public class RSAUtil {
private static Cipher cipher;
static{
try {
cipher = Cipher.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}
/**
* 生成密鑰對
* @param filePath 生成密鑰的路徑
* @return
*/
public static Map<String,String> generateKeyPair(String filePath){
try {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
// 密鑰位數
keyPairGen.initialize(1024);
// 密鑰對
KeyPair keyPair = keyPairGen.generateKeyPair();
// 公鑰
PublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// 私鑰
PrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
//得到公鑰字元串
String publicKeyString = getKeyString(publicKey);
//得到私鑰字元串
String privateKeyString = getKeyString(privateKey);
//將密鑰對寫入到文件 想學習更多加我q u N 前面是二五七 中間是014後面是001
FileWriter pubfw = new FileWriter(filePath+"/publicKey.keystore");
FileWriter prifw = new FileWriter(filePath+"/privateKey.keystore");
BufferedWriter pubbw = new BufferedWriter(pubfw);
BufferedWriter pribw = new BufferedWriter(prifw);
pubbw.write(publicKeyString);
pribw.write(privateKeyString);
pubbw.flush();
pubbw.close();
pubfw.close();
pribw.flush();
pribw.close();
prifw.close();
//將生成的密鑰對返回
Map<String,String> map = new HashMap<String,String>();
map.put("publicKey",publicKeyString);
map.put("privateKey",privateKeyString);
return map;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 得到公鑰
*
* @param key
* 密鑰字元串(經過base64編碼)
* @throws Exception
*/
public static PublicKey getPublicKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
/**
* 得到私鑰
*
* @param key
* 密鑰字元串(經過base64編碼)
* @throws Exception
*/
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
/**
* 得到密鑰字元串(經過base64編碼)
*
* @return
*/
public static String getKeyString(Key key) throws Exception {
byte[] keyBytes = key.getEncoded();
String s = (new BASE64Encoder()).encode(keyBytes);
return s;
}
/**
* 使用公鑰對明文進行加密,返回BASE64編碼的字元串
* @param publicKey
* @param plainText
* @return
*/
public static String encrypt(PublicKey publicKey,String plainText){
try {
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] enBytes = cipher.doFinal(plainText.getBytes());
return (new BASE64Encoder()).encode(enBytes);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**
* 使用keystore對明文進行加密
* @param publicKeystore 公鑰文件路徑
* @param plainText 明文
* @return
*/
public static String encrypt(String publicKeystore,String plainText){
try {
FileReader fr = new FileReader(publicKeystore);
BufferedReader br = new BufferedReader(fr);
String publicKeyString="";
String str;
while((str=br.readLine())!=null){
publicKeyString+=str;
}
br.close();
fr.close();
cipher.init(Cipher.ENCRYPT_MODE,getPublicKey(publicKeyString));
byte[] enBytes = cipher.doFinal(plainText.getBytes());
return (new BASE64Encoder()).encode(enBytes);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 使用私鑰對明文密文進行解密
* @param privateKey
* @param enStr
* @return
*/
public static String decrypt(PrivateKey privateKey,String enStr){
try {
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] deBytes = cipher.doFinal((new BASE64Decoder()).decodeBuffer(enStr));
return new String(deBytes);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 使用keystore對密文進行解密
* @param privateKeystore 私鑰路徑
* @param enStr 密文
* @return
*/
public static String decrypt(String privateKeystore,String enStr){
try {
FileReader fr = new FileReader(privateKeystore);
BufferedReader br = new BufferedReader(fr);
String privateKeyString="";
String str;
while((str=br.readLine())!=null){
privateKeyString+=str;
}
br.close();
fr.close();
cipher.init(Cipher.DECRYPT_MODE, getPrivateKey(privateKeyString));
byte[] deBytes = cipher.doFinal((new BASE64Decoder()).decodeBuffer(enStr));
return new String(deBytes);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
⑩ Java程序設計一文件用RSA演算法進行加密和解密,請高人幫忙
計一文件用RSA演算法進行加密和解
z這個任務書沒有嗎
大