资源简介
php与java互通rsa加密解密
代码片段和文件信息
/**
* 作者:军师
* 邮箱:68079320@qq.com
* 个人博客:http://www.webiji.com
*/
package crypt;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
public class Rsa {
private String priKey;
private String pubKey;
public static void main(String[] args) {
Rsa rsa = new Rsa();
String str = “我要加密这段文字。“;
System.out.println(“原文:“+“我要加密这段文字。“);
String crypt = rsa.encryptByPrivateKey(str);
System.out.println(“私钥加密密文:“+crypt);
String result = rsa.decryptByPublicKey(crypt);
System.out.println(“原文:“+result);
System.out.println(“---“);
str = “我要加密这段文字。“;
System.out.println(“原文:“+“我要加密这段文字。“);
crypt = rsa.encryptByPublicKey(str);
System.out.println(“公钥加密密文:“+crypt);
result = rsa.decryptByPrivateKey(crypt);
System.out.println(“原文:“+result);
System.out.println(“---“);
str = “我要签名这段文字。“;
System.out.println(“原文:“+str);
String str1 = rsa.signByPrivateKey(str);
System.out.println(“签名结果:“+str1);
if(rsa.verifyByPublicKey(str1 str)){
System.out.println(“成功“);
} else {
System.out.println(“失败“);
}
}
public Rsa(){
priKey = readStringFromFile(“java_private.pem“);
pubKey = readStringFromFile(“java_public.pem“);
}
/**
* 使用私钥加密
* @see decByPriKey
*/
public String encryptByPrivateKey(String data) {
// 加密
String str = ““;
try {
byte[] pribyte = base64decode(priKey.trim());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pribyte);
KeyFactory fac = KeyFactory.getInstance(“RSA“);
RSAPrivateKey privateKey = (RSAPrivateKey) fac.generatePrivate(keySpec);
Cipher c1 = Cipher.getInstance(“RSA/ECB/PKCS1Padding“);
c1.init(Cipher.ENCRYPT_MODE privateKey);
str = base64encode(c1.doFinal(data.getBytes()));
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
/**
* 使用私钥解密
* @see decByPriKey
*/
public String decryptByPrivateKey(String data) {
// 加密
String str = ““;
try {
byte[] pribyte = base64decode(priKey.trim());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pribyte);
KeyFactory fac = KeyFactory.getInstance(“RSA“);
RSAPrivateKey privateKey = (RSAPrivateKey) fac.generatePrivate(keySpec);
Cipher c1 = Cipher.getInstance(“RSA/ECB/PKCS1Padding“);
c1.init(Cipher.DECRYPT_MODE privateKey);
byte[] temp = c1.doFinal(base64decode(data));
str = new String(temp);
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
/**
* 使用公钥加密
* @see decByPriKey
*/
public String encryptByPublicKey(String data) {
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 861 2016-04-20 18:31 PHP_JAVA_RSA加密\java_private.pem
文件 219 2016-04-20 18:32 PHP_JAVA_RSA加密\java_public.pem
文件 916 2016-02-17 15:07 PHP_JAVA_RSA加密\php_private.pem
文件 272 2016-02-17 15:07 PHP_JAVA_RSA加密\php_public.pem
文件 7145 2016-04-21 11:57 PHP_JAVA_RSA加密\Rsa.java
文件 3444 2016-04-21 11:57 PHP_JAVA_RSA加密\RSA.php
文件 542 2016-04-21 12:00 PHP_JAVA_RSA加密\说明.txt
目录 0 2016-04-21 11:53 PHP_JAVA_RSA加密
----------- --------- ---------- ----- ----
13399 8
评论
共有 条评论