我现有一个公匙,请问如何用RSA将一个字符串解密呢?
解决方案 »
- WinForm和数据库,新手求解
- webservers+AutoCompleteExtender的二次查询赋值,解决问题+100分
- 谁给点软件开发思想!
- 在WebBrowser1_DocumentCompleted过程中,我用了三种方法都得不到下载网页内容,是怎么回事,是取得网页的方法不对吗,请指教。见代码:
- C# winform 未将对象引用设置到对象的实例。 该怎么解决
- 求C#自定义数值类型的资料或实现代码
- vs.net下的工程是用source safe进行版本控制的问题,急!!!!!(在线等)
- 很奇怪的报错(数据绑定问题)
- 请问怎么让被绑定到TEXTBOX的SQL里DATETIME字段的内容只显示年月日,不显示时间0:00:0?
- 请问继承.NET FrameWork中的类,如InternalDataCollectionBase的序列化问题
- C#语法问题
- 各位街坊邻居,谁能给我一个简单的购物车的例子啊。小弟再次谢过了。。。
package com.panyudc.util;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;/**
* 用RSA算法对数据进行加密和解密
*/
public class DataEncryptDecrypt
{
private static PublicKey pubkey; private static PrivateKey prikey; /**
* 初始化公钥和私钥
*
*/
public static synchronized void initKey(String pubkeyfile, String prikeyfile) throws IOException,
NoSuchAlgorithmException, InvalidKeySpecException
{
FileInputStream in = null;
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] enckey = null; //读出公钥
in = new FileInputStream(pubkeyfile);
enckey = new byte[in.available()];
in.read(enckey);
in.close();
X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(enckey);
pubkey = keyFactory.generatePublic(pubKeySpec);
//读出私钥
in = new FileInputStream(prikeyfile);
enckey = new byte[in.available()];
in.read(enckey);
in.close();
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(enckey);
prikey = keyFactory.generatePrivate(priKeySpec);
} /**
* 对数据data进行解密或者加密,opmode 决定是加密还是解密,keymode决定是用公钥还是私钥
* 另外,进行加密时data后面附加了系统当前的时间,时间格式(yyyyMMddHHmmss)
*/
public static String getData(String data, int opmode, int keymode) throws NoSuchPaddingException,
InvalidKeyException, IllegalStateException, IllegalBlockSizeException, BadPaddingException,
NoSuchAlgorithmException, NoSuchProviderException, IOException, ClassNotFoundException,
InvalidKeySpecException
{
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
if (Cipher.ENCRYPT_MODE == opmode)//加密
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
if (Cipher.PUBLIC_KEY == keymode)//用公钥加密
{
cipher.init(Cipher.ENCRYPT_MODE, pubkey);
}
else if (Cipher.PRIVATE_KEY == keymode)
{
cipher.init(Cipher.ENCRYPT_MODE, prikey);
}
return byte2hex(cipher.doFinal((data + sdf.format(new Date(System.currentTimeMillis()))).getBytes("UTF8")));
}
else
//解密
{
if (Cipher.PRIVATE_KEY == keymode)//用私钥解密
{
cipher.init(Cipher.DECRYPT_MODE, prikey);
}
else if (Cipher.PUBLIC_KEY == keymode)//用公钥解密
{
cipher.init(Cipher.DECRYPT_MODE, pubkey);
}
return new String(cipher.doFinal(hex2byte(data)), "UTF8");
}
} /**
* 生成公、私密码对
*
* @return
*/
public static boolean generatekey(String pubkeyfile, String prikeyfile)
{
FileOutputStream out = null;
try
{
KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
keygen.initialize(1024);
KeyPair keys = keygen.genKeyPair();
PublicKey pubkey = keys.getPublic();
PrivateKey prikey = keys.getPrivate(); out = new FileOutputStream(prikeyfile);
out.write(prikey.getEncoded());
out.close(); out = new FileOutputStream(pubkeyfile);
out.write(pubkey.getEncoded());
out.close();
return true;
}
catch (Exception e)
{
e.printStackTrace();
if (out != null)
{
try
{
out.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
return false;
}
} /*
* 将byte换成hex字符串
*/
public static String byte2hex(byte[] b)
{
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++)
{
stmp = (Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp; }
return hs.toUpperCase();
} /*
* 将hex串换成byte数组
*/
public static byte[] hex2byte(String s)
{
byte[] b = new byte[s.length() / 2];
for (int i = 0; i < s.length() / 2; i++)
{
b[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);
}
return b;
} public static void main(String[] args) throws InvalidKeyException, NoSuchPaddingException, IllegalStateException,
IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchProviderException,
InvalidKeySpecException, IOException, ClassNotFoundException
{ }
}