本程序能实现生成RSA密钥,保存到文件中并取出,再进行加解密,如需将密钥保存到数据库中,请参考http://blog.164288.com/post/21.html。
package init;
import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import org.bouncycastle.jce.provider.*;
import sun.misc.*;
public class Client1
{
public static void main(String[] args) throws Exception{
Client1.init();
KeyPair kp=Client1.generateKey();
PublicKey pu=kp.getPublic();
PrivateKey pr=kp.getPrivate();
String pu2=Client1.getKeyAsString(pu);
String pr2=Client1.getKeyAsString(pr);
//将密钥保存到文件中
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("client1PubKey1.key"));
osw.write(pu2,0,pu2.length());
osw.flush();
osw.close();
OutputStreamWriter osw2 = new OutputStreamWriter(new FileOutputStream("client1PriKey1.key"));
osw2.write(pr2,0,pr2.length());
osw2.flush();
osw2.close();
System.out.println("Save Key Success!");
//将密钥从文件中取出来
//取得client1的公钥,私钥
InputStreamReader isr = new InputStreamReader(new FileInputStream("client1PubKey1.key"));
String client1PubKey="";
int ch=0;
while((ch=isr.read())!=-1){
client1PubKey=client1PubKey+(char)ch;
}
isr.close();
InputStreamReader isr2 = new InputStreamReader(new FileInputStream("client1PriKey1.key"));
String client1PriKey="";
int ch2=0;
while((ch2=isr2.read())!=-1){
client1PriKey=client1PriKey+(char)ch2;
}
isr2.close();
System.out.println("client1PriKey="+client1PriKey);
System.out.println("client1PubKey="+client1PubKey);
String pt="12345"; //明文
//私钥加密,公钥解密
String ct=Client1.encrypt(pt, pr);
String pt2=Client1.decrypt(ct, pu);
System.out.println("plaText="+pt);
System.out.println("cipText="+ct);
System.out.println("plaText2="+pt2);
//公钥加密,私钥解密
String ctt=Client1.encrypt(pt, pu);
String ptt2=Client1.decrypt(ctt, pr);
System.out.println("cipTextt="+ctt);
System.out.println("plaTextt2="+ptt2);
}
public static final String ALGORITHM="RSA";
public static void init(){
Security.addProvider(new BouncyCastleProvider());
}
public static KeyPair generateKey() throws NoSuchAlgorithmException{
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
keyGen.initialize(1024);
KeyPair key = keyGen.generateKeyPair();
return key;
}
public String subSecEncrypt(String pText,Key key) throws Exception{
//分段加密,注意:分段加密的密文长度都是174字节
int pTextLen=pText.length();
int m=pTextLen/117;
int n=pTextLen-m*117;
String cText=""; //密文
//对117整数倍的子串加密,拼接
for(int i=0;i<m;i++){
String pTextSub=pText.substring(i*117, (i+1)*117); //获取子串
cText=cText+encrypt(pTextSub, key); //分段加密,拼接
}
//对剩下的子串加密,拼接
String pTextSub2=pText.substring(pTextLen-n, pTextLen);
cText=cText+encrypt(pTextSub2, key);
return cText;
}
public static String subSecDecrypt(String cText,Key key) throws Exception{
//分段解密,注意:分段加解密的密文长度都是174字节
int cTextLen=cText.length();
int m=cTextLen/174;
//int n=cTextLen-m*174;
String pText=""; //明文
for(int i=0;i<m;i++){
String cTextSub=cText.substring(i*174, (i+1)*174);
pText=pText+decrypt(cTextSub, key);
}
return pText;
}
public static byte[] encrypt(byte[] text, Key key) throws Exception
{
byte[] cipherText=null;
Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherText=cipher.doFinal(text);
return cipherText;
}
public static String encrypt(String text, Key key) throws Exception
{
String encryptedText;
byte[] cipherText=encrypt(text.getBytes("UTF8"),key);
encryptedText=encodeBASE64(cipherText);
return encryptedText;
}
public static byte[] decrypt(byte[] text, Key key) throws Exception
{
byte[] decryptedText=null;
Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
decryptedText=cipher.doFinal(text);
return decryptedText;
}
public static String decrypt(String text, Key key) throws Exception
{
String result;
byte[] dectyptedText=decrypt(decodeBASE64(text),key);
result=new String(dectyptedText,"UTF8");
return result;
}
//将String类型转换为PrivateKey类型
public static PrivateKey getPrivateKeyFromString(String key) throws Exception
{
KeyFactory keyFactory=KeyFactory.getInstance(ALGORITHM);
BASE64Decoder b64=new BASE64Decoder();
EncodedKeySpec privateKeySpec=new PKCS8EncodedKeySpec(b64.decodeBuffer(key));
PrivateKey privateKey=keyFactory.generatePrivate(privateKeySpec);
return privateKey;
}
//将String类型转换为PublicKey类型
public static PublicKey getPublicKeyFromString(String key) throws Exception
{
BASE64Decoder b64=new BASE64Decoder();
KeyFactory keyFactory=KeyFactory.getInstance(ALGORITHM);
EncodedKeySpec publicKeySpec=new X509EncodedKeySpec(b64.decodeBuffer(key));
PublicKey publicKey=keyFactory.generatePublic(publicKeySpec);
return publicKey;
}
//将Key类型转换为String类型
public static String getKeyAsString(Key key)
{
byte[] keyBytes = key.getEncoded();
BASE64Encoder b64 = new BASE64Encoder();
return b64.encode(keyBytes);
}
public static String encodeBASE64(byte[] bytes)
{
BASE64Encoder b64=new BASE64Encoder();
return b64.encode(bytes);
}
public static byte[] decodeBASE64(String text) throws IOException
{
BASE64Decoder b64=new BASE64Decoder();
return b64.decodeBuffer(text);
}
}
JAVA实现RSA算法与密钥文件操作

该程序展示了如何生成RSA密钥对,并将它们保存到文件中,之后能够从文件中读取密钥进行加密和解密操作。若要了解如何将密钥存储到数据库,可以参考指定链接。
2248

被折叠的 条评论
为什么被折叠?



