import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.interfaces.RSAMultiPrimePrivateCrtKey;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPublicKeySpec;import javax.crypto.Cipher;
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
import org.bouncycastle.jce.provider.JCERSAPrivateCrtKey;public class RSATest {
private static BigInteger n=new BigInteger("d468d00029790221ad0a5556a2b43236910b36ab66642379dfd8ba7d8bfc37a01a76daa44a62afba2b9ba8a605aa3ef3a14afb73d7d3d4eb207e2605707e87d3",16);
private static BigInteger e=new BigInteger("10001",16);
private static BigInteger d=new BigInteger("1c35cf6a3617dc309ccac28abf112f9a0fa96d9955fff6b639048fc01cd7628ba2c6840b580c2a93dfb08db78cab33e365b650faffc1768d46ff0a9d01a4b001",16);
private static BigInteger p=new BigInteger("ff3fafc9d4a8074ba45121858ab3abd2b7b733dbd8e3d42dd88a30353ed0bfc5",16);
private static BigInteger q=new BigInteger("d508d9630ba03a501bdde5f2871e1676bf68c260ea55ef5d5a6a07cfd564cab7",16);
private static BigInteger dmp=new BigInteger("3682b75bf71a5b548027036dad8d69687aeb991b9e5a297a110092030d032fb1",16);
private static BigInteger dmq=new BigInteger("377e86a27674d6bbe786765027427f378fc50a5d94adae1cf3608b88685e493",16);
private static BigInteger qmp=new BigInteger("46ac264d5d16d83d2ac8512f3e2e652198c7724c16420927c48d3d8cacfa8526",16);
public byte[] hexToBytes(String src) {
int m=0,n=0;
int l=src.length()/2;
String str;
byte[] ret = new byte[l];
for (int i = 0; i < l; i++) {
m=i*2+1;
n=m+1;
str=src.substring(i*2, i*2+2);
//System.out.println(str+"\n");
ret[i] = (byte)(0xff & Integer.parseInt(str,16));
}
return ret;
} public static void main(String[] args) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("RSA", "BC");
RSAPrivateCrtKeySpec privKeySpec=new RSAPrivateCrtKeySpec(n,e,d,p,q,dmp,dmq,qmp);
KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
Key privKey = (Key)fact.generatePrivate(privKeySpec); RSATest rsa=new RSATest();
cipher.init(Cipher.DECRYPT_MODE, privKey);
String str="b7b9bb975736dfed166f5023af6c1fac63346eabbe1d8c6607ec0b8d83a51eb5fd8a1f1172a1fe85a6451aec6bfb1b4e7a56fd061ef8a2d4ee1f02494f68f0b7";
byte[] forumcookie=rsa.hexToBytes(str);
byte[] plainText = cipher.doFinal(forumcookie);
System.out.println("plainText is :"+new String(plainText));
}}
正确结果应该为plainText is :Mike-and-Year2008
但现在结果却为plainText is :Eb罉?2橻{??2?+^詖? 碃蜩镕d*-▁!橍頏=Mike-and-Year2008
前面不知道为什么有乱码,应该是最后三行函数有问题,前高人指点
byte[] forumcookie=rsa.hexToBytes(str);
byte[] plainText = cipher.doFinal(forumcookie);
System.out.println("plainText is :"+new String(plainText));
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.interfaces.RSAMultiPrimePrivateCrtKey;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPublicKeySpec;import javax.crypto.Cipher;
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
import org.bouncycastle.jce.provider.JCERSAPrivateCrtKey;public class RSATest {
private static BigInteger n=new BigInteger("d468d00029790221ad0a5556a2b43236910b36ab66642379dfd8ba7d8bfc37a01a76daa44a62afba2b9ba8a605aa3ef3a14afb73d7d3d4eb207e2605707e87d3",16);
private static BigInteger e=new BigInteger("10001",16);
private static BigInteger d=new BigInteger("1c35cf6a3617dc309ccac28abf112f9a0fa96d9955fff6b639048fc01cd7628ba2c6840b580c2a93dfb08db78cab33e365b650faffc1768d46ff0a9d01a4b001",16);
private static BigInteger p=new BigInteger("ff3fafc9d4a8074ba45121858ab3abd2b7b733dbd8e3d42dd88a30353ed0bfc5",16);
private static BigInteger q=new BigInteger("d508d9630ba03a501bdde5f2871e1676bf68c260ea55ef5d5a6a07cfd564cab7",16);
private static BigInteger dmp=new BigInteger("3682b75bf71a5b548027036dad8d69687aeb991b9e5a297a110092030d032fb1",16);
private static BigInteger dmq=new BigInteger("377e86a27674d6bbe786765027427f378fc50a5d94adae1cf3608b88685e493",16);
private static BigInteger qmp=new BigInteger("46ac264d5d16d83d2ac8512f3e2e652198c7724c16420927c48d3d8cacfa8526",16);
public byte[] hexToBytes(String src) {
int m=0,n=0;
int l=src.length()/2;
String str;
byte[] ret = new byte[l];
for (int i = 0; i < l; i++) {
m=i*2+1;
n=m+1;
str=src.substring(i*2, i*2+2);
//System.out.println(str+"\n");
ret[i] = (byte)(0xff & Integer.parseInt(str,16));
}
return ret;
} public static void main(String[] args) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("RSA", "BC");
RSAPrivateCrtKeySpec privKeySpec=new RSAPrivateCrtKeySpec(n,e,d,p,q,dmp,dmq,qmp);
KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
Key privKey = (Key)fact.generatePrivate(privKeySpec); RSATest rsa=new RSATest();
cipher.init(Cipher.DECRYPT_MODE, privKey);
String str="b7b9bb975736dfed166f5023af6c1fac63346eabbe1d8c6607ec0b8d83a51eb5fd8a1f1172a1fe85a6451aec6bfb1b4e7a56fd061ef8a2d4ee1f02494f68f0b7";
byte[] forumcookie=rsa.hexToBytes(str);
byte[] plainText = cipher.doFinal(forumcookie);
System.out.println("plainText is :"+new String(plainText));
}}
正确结果应该为plainText is :Mike-and-Year2008
但现在结果却为plainText is :Eb罉?2橻{??2?+^詖? 碃蜩镕d*-▁!橍頏=Mike-and-Year2008
前面不知道为什么有乱码,应该是最后三行函数有问题,前高人指点
byte[] forumcookie=rsa.hexToBytes(str);
byte[] plainText = cipher.doFinal(forumcookie);
System.out.println("plainText is :"+new String(plainText));
import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.spec.RSAPrivateCrtKeySpec;import javax.crypto.Cipher;public class RSATest
{
private static BigInteger n = new BigInteger("d468d00029790221ad0a5556a2b43236910b36ab66642379dfd8ba7d8bfc37a01a76daa44a62afba2b9ba8a605aa3ef3a14afb73d7d3d4eb207e2605707e87d3", 16);
private static BigInteger e = new BigInteger("10001", 16);
private static BigInteger d = new BigInteger("1c35cf6a3617dc309ccac28abf112f9a0fa96d9955fff6b639048fc01cd7628ba2c6840b580c2a93dfb08db78cab33e365b650faffc1768d46ff0a9d01a4b001", 16);
private static BigInteger p = new BigInteger("ff3fafc9d4a8074ba45121858ab3abd2b7b733dbd8e3d42dd88a30353ed0bfc5", 16);
private static BigInteger q = new BigInteger("d508d9630ba03a501bdde5f2871e1676bf68c260ea55ef5d5a6a07cfd564cab7", 16);
private static BigInteger dmp = new BigInteger("3682b75bf71a5b548027036dad8d69687aeb991b9e5a297a110092030d032fb1", 16);
private static BigInteger dmq = new BigInteger("377e86a27674d6bbe786765027427f378fc50a5d94adae1cf3608b88685e493", 16);
private static BigInteger qmp = new BigInteger("46ac264d5d16d83d2ac8512f3e2e652198c7724c16420927c48d3d8cacfa8526", 16);
public byte[] hexToBytes(String src)
{
int m = 0, n = 0;
int l = src.length() / 2;
String str;
byte[] ret = new byte[l];
for (int i = 0; i < l; i++)
{
m = i * 2 + 1;
n = m + 1;
str = src.substring(i * 2, i * 2 + 2);
//System.out.println(str+"\n");
ret[i] = (byte) (0xff & Integer.parseInt(str, 16));
}
return ret;
}
public static void main(String[] args) throws Exception
{
// Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("RSA");
RSAPrivateCrtKeySpec privKeySpec = new RSAPrivateCrtKeySpec(n, e, d, p, q, dmp, dmq, qmp);
KeyFactory fact = KeyFactory.getInstance("RSA");
Key privKey = (Key) fact.generatePrivate(privKeySpec);
RSATest rsa = new RSATest();
cipher.init(Cipher.DECRYPT_MODE, privKey);
String str = "b7b9bb975736dfed166f5023af6c1fac63346eabbe1d8c6607ec0b8d83a51eb5fd8a1f1172a1fe85a6451aec6bfb1b4e7a56fd061ef8a2d4ee1f02494f68f0b7";
byte[] forumcookie = rsa.hexToBytes(str);
byte[] plainText = cipher.doFinal(forumcookie);
System.out.println("plainText is :" + new String(plainText));
}
}
[code=BatchFile]
plainText is :Test'Mike-and-Year2008
[/code]