「java加密解密rsa」java加密解密算法
今天给各位分享java加密解密rsa的知识,其中也会对java加密解密算法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java RSA 加密解密,汉字乱码怎么办
- 2、java中RSA用私钥加密公钥解密问题
- 3、非对称加密解密RSA的实现例子
- 4、Java 第三方公钥 RSA加密求助
- 5、java RSA实现对文件加密解密
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用私钥加密公钥解密问题
公钥和私钥可以互换的用,用公钥加密私钥解密,用私钥加密公钥解密都ok,方法一样
非对称加密解密RSA的实现例子
最近有接触到加密相关的内容,本期以非对称加密为例子,做个简单的总结和记录。首先了解下非对称加密,简单来说非对称指的是加密和解密用不同的秘钥,典型的RSA,这个算法名称是基于三个发明人的名字首字母取的;而对称加密必须要在加解密使用相同的秘钥,典型的AES。这里细节不多展开阐述,涉及到很多数学原理,如大数的质因数分解等,感兴趣的可以找找李永乐等网上比较优秀的科普。这篇文章只是java原生实现的加解密例子。至于其他的如md5,hash等,如果从主观可读的角度来说,也可以称为加密。
如下的示例是使用Java原生实现RSA的加密解密,包括用公钥加密,然后私钥解密;或者使用私钥加密,然后公钥解密。注意不同key大小,限制的解密内容大小也不一样,感兴趣的同学可以试试修改key大小和加密内容长度来试试。还有要注意的是RSA加密有一定的性能损耗。
想了解原理相关的内容可以看如下的参考内容。
[1]. RSA原理
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 MapString,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);
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();
//将生成的密钥对返回
MapString,String map = new HashMapString,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实现对文件加密解密
这个我不清楚。
对文件加密,我使用的是超级加密3000.
超级加密3000采用国际上成熟的加密算法和安全快速的加密方法,可以有效保障数据安全!
具体操作方法:
1下载安装超级加密3000。
2 然后在需要加密的文件上单击鼠标右键选择加密。
3 在弹出的文件加密窗口中设置文件加密密码就OK了。
超级加密3000的下载地址你可以在百度上搜索超级加密3000,第一个就是。
java加密解密rsa的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java加密解密算法、java加密解密rsa的信息别忘了在本站进行查找喔。