RSA加密 rsa加密java密钥 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 RSA是密钥对,公钥+私钥,任何一个都可以加密,用另外一个解密。你有一个可以加密,但就解不了了 嗯,只要加密就行了,还有RSA加密C#格式转java格式清不清楚? 加密的时候 需要对数据进行补齐,padding,要公开padding方法 使用公钥加密public static byte[] encryptByPublicKey(byte[] data, byte[] keyBytes ) throws Exception { // 取得公钥 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(“RSA”); Key publicKey = keyFactory.generatePublic(x509KeySpec); // 对数据加密 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(data); } /** * 获取RSA加密信息 * @param info * @return */ public static String getRSAInfo(String dataStr){ byte[] en = encrypt(dataStr); String info = Base64.encode(en); try { info = URLEncoder.encode(info,"UTF-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return info; } public static byte[] encrypt(String info) { try { byte[] modulusBytes = Base64.decode("yrEAJvQfhhoZcWMzsQw0LZd2OqOTMgOzVl7Ja2w6R1rF+tl+V+6T08zvZAhWye2omdZ8JccefDQNkYTqIcNXu9XqYMTl79FaFVt32wykCIHnvfN8IafVnQKcuWpWDuCbZAocoBisT6LWugyiXWe7cr+2qFSW4nL5YIQ4jxXJn8U="); byte[] exponentBytes = Base64.decode("AQAB"); BigInteger modulus = new BigInteger(1, modulusBytes); BigInteger exponent = new BigInteger(1, exponentBytes); RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent); KeyFactory fact = KeyFactory.getInstance("RSA"); PublicKey pubKey = fact.generatePublic(rsaPubKey); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, pubKey); byte[] cipherData = cipher.doFinal(new String(info).getBytes()); return cipherData; } catch (Exception e) { e.printStackTrace(); } return null; } 用javamail实现webservice时遇到问题 struts1标签功能用struts2标签实现 swing输入框缩在一起了 j2ee项目中能否添加.net的功能模块? 关于proxool数据源的问题 总是出http500错误大家帮忙看看 struts中遇到的一个疑问!!请大家看看,帮我解决一下! 先学哪个框架? 在Tomcat中Include html文件时,如果该html文件有中文则产生乱码,怎么办? extjs4 如何根据id选中grid指定的行 关于mybatis的collection嵌套结果的问题。 hibernate 多级关联查询问题
throws Exception {
// 取得公钥
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(“RSA”);
Key publicKey = keyFactory.generatePublic(x509KeySpec); // 对数据加密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(data);
}
* 获取RSA加密信息
* @param info
* @return
*/
public static String getRSAInfo(String dataStr){
byte[] en = encrypt(dataStr);
String info = Base64.encode(en);
try {
info = URLEncoder.encode(info,"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return info;
}
public static byte[] encrypt(String info) {
try {
byte[] modulusBytes = Base64.decode("yrEAJvQfhhoZcWMzsQw0LZd2OqOTMgOzVl7Ja2w6R1rF+tl+V+6T08zvZAhWye2omdZ8JccefDQNkYTqIcNXu9XqYMTl79FaFVt32wykCIHnvfN8IafVnQKcuWpWDuCbZAocoBisT6LWugyiXWe7cr+2qFSW4nL5YIQ4jxXJn8U=");
byte[] exponentBytes = Base64.decode("AQAB");
BigInteger modulus = new BigInteger(1, modulusBytes);
BigInteger exponent = new BigInteger(1, exponentBytes);
RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKey = fact.generatePublic(rsaPubKey);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherData = cipher.doFinal(new String(info).getBytes());
return cipherData;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}