当前位置:首页 » 密码管理 » javarsa文件加密解密

javarsa文件加密解密

发布时间: 2022-06-14 14:39:08

java RSA实现对文件加密解密

这个我不清楚。

对文件加密,我使用的是超级加密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这个任务书没有吗

热点内容
php微信开发教程 发布:2025-07-01 10:28:24 浏览:840
访问学者联系导师 发布:2025-07-01 10:27:44 浏览:982
服务器端的http地址 发布:2025-07-01 10:04:35 浏览:176
php好处 发布:2025-07-01 10:01:45 浏览:877
安卓车机什么屏幕好 发布:2025-07-01 09:46:15 浏览:546
编译原理re不能描述的串 发布:2025-07-01 09:34:24 浏览:75
本地电脑怎么用服务器ip 发布:2025-07-01 09:10:03 浏览:831
有趣的python代码 发布:2025-07-01 09:10:02 浏览:361
威龙奇兵ftp 发布:2025-07-01 09:07:38 浏览:694
linuxedit 发布:2025-07-01 09:03:36 浏览:424