帮你完善了下代码。
import java.io.Fileimport java.io.FileOutputStream
import java.io.FileReader
import java.io.OutputStream
import java.io.PrintWriter
import java.io.Reader
import java.util.Map
public class Test {
static String publicKey
static String privateKey
public Test() throws Exception {
// TODO Auto-generated constructor stub
Map<String, Object> keyMap = RSAUtils.genKeyPair()
publicKey = RSAUtils.getPublicKey(keyMap)
privateKey = RSAUtils.getPrivateKey(keyMap)
// 保存密钥,名字分别为publicKey。txt 和privateKey。txt
PrintWriter pw1 = new PrintWriter(new FileOutputStream(
"D:/publicKey.txt"))
PrintWriter pw2 = new PrintWriter(new FileOutputStream(
"D:/privateKey.txt"))
pw1.print(publicKey)
pw2.print(privateKey)
pw1.close()
pw2.close()
// 从保存的目录读取刚才的保存的公钥,
String pubkey = readFile("D:/publicKey.txt")// 读取的公钥内容;
String data = readFile("D:/1.txt") // 需要公钥加密的文件的内容(如D:/1.txt)
byte[] encByPubKeyData = RSAUtils.encryptByPublicKey(data.getBytes(),
pubkey)
//将加密数据base64后写入文件
writeFile("D:/Encfile.txt", Base64Utils.encode(encByPubKeyData).getBytes("UTF-8"))
// 加密后的文件保存在
String prikey = readFile("D:/privateKey.txt")// 从保存的目录读取刚才的保存的私钥,
String Encdata = readFile("D:/Encfile.txt")// 刚才加密的文件的内容
byte[] encData = Base64Utils.decode(Encdata)
byte[] decByPriKeyData = RSAUtils.decryptByPrivateKey(encData, prikey)
// 解密后后的文件保存在D:/Decfile.txt
writeFile("D:/Decfile.txt", decByPriKeyData)
}
private static String readFile(String filePath) throws Exception {
File inFile = new File(filePath)
long fileLen = inFile.length()
Reader reader = new FileReader(inFile)
char[] content = new char[(int) fileLen]
reader.read(content)
System.out.println("读取到的内容为:" + new String(content))
return new String(content)
}
private static void writeFile(String filePath, byte[] content)
throws Exception {
System.out.println("待写入文件的内容为:" + new String(content))
File outFile = new File(filePath)
OutputStream out = new FileOutputStream(outFile)
out.write(content)
if (out != null) out.close()
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
new Test()
}
}
测试结果:
读取到的内容为:MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDVr9D9hYPD4kS5k86dRm+utyt5XGOSTPwT0YeoBnQmgeISkNsbtUFkY6txyodVl26IM1H5iwJ1jMQ63+lXfZxzNpeA+rHaxmeQ2qI+5ES9AF7G6KIwjzakKsA08Ly+1y3dp0BnoyHF7/Pj3AS28fDmE5piea7w36vp4E3Ts+F9vwIDAQAB读取到的内容为:锘县ahaha
RSA的Java实现不能一次加密很大的字符,自己处理了一下,见下面的代码。Base64编码类用的是一个Public domain Base64 for java http://iharder.sourceforge.net/current/java/base64/其他的保存公钥到文件等简单的实现,就不详细说了,看代码吧。==============================================import java.security.*import java.security.spec.PKCS8EncodedKeySpecimport java.security.spec.X509EncodedKeySpecimport java.util.HashMapimport java.util.Mapimport javax.crypto.*import java.io.*public class Encryptor {private static final String KEY_FILENAME = "c:\\mykey.dat"private static final String OTHERS_KEY_FILENAME = "c:\\Otherskey.dat"// private static final int KEY_SIZE = 1024// private static final int BLOCK_SIZE = 117// private static final int OUTPUT_BLOCK_SIZE = 128private static final int KEY_SIZE = 2048//RSA key 是多少位的private static final int BLOCK_SIZE = 245 //一次RSA加密 *** 作所允许的最大长度//这个值与 KEY_SIZE 已经padding方法有关。因为 1024的key的输出是128,2048key输出是256字节//可能11个字节用于保存padding信息了,所以最多可用的就只有245字节了。private static final int OUTPUT_BLOCK_SIZE = 256private SecureRandom secrandprivate Cipher rsaCipherprivate KeyPair keysprivate Map<String, Key>allUserKeyspublic Encryptor() throws Exception {try {allUserKeys = new HashMap<String, Key>()secrand = new SecureRandom()//SunJCE Provider 中只支持ECB mode,试了一下只有PKCS1PADDING可以直接还原原始数据,//NOPadding导致解压出来的都是blocksize长度的数据,还要自己处理//参见 http://java.sun.com/javase/6/docs/technotes/guides/security/SunProviders.html#SunJCEProvider////另外根据 Open-JDK-6.b17-src( http://www.docjar.com/html/api/com/sun/crypto/provider/RSACipher.java.html)// 中代码的注释,使用RSA来加密大量数据不是一种标准的用法。所以现有实现一次doFinal调用之进行一个RSA *** 作,//如果用doFinal来加密超过的一个 *** 作所允许的长度数据将抛出异常。//根据keysize的长度,典型的1024个长度的key和PKCS1PADDING一起使用时//一次doFinal调用只能加密117个byte的数据。(NOPadding 和1024 keysize时128个字节长度)//(2048长度的key和PKCS1PADDING 最多允许245字节一次)//想用来加密大量数据的只能自己用其他办法实现了。可能RSA加密速度比较慢吧,要用AES才行rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING")} catch (NoSuchAlgorithmException e) {e.printStackTrace()} catch (NoSuchPaddingException e) {e.printStackTrace()throw e}ObjectInputStream intry {in = new ObjectInputStream(new FileInputStream(KEY_FILENAME))} catch (FileNotFoundException e) {if (false == GenerateKeys()){throw e}LoadKeys()return}keys = (KeyPair) in.readObject()in.close()LoadKeys()}/** 生成自己的公钥和私钥*/private Boolean GenerateKeys() {try {KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA")// secrand = new SecureRandom()// sedSeed之后会造成 生成的密钥都是一样的// secrand.setSeed("chatencrptor".getBytes())// 初始化随机产生器//key长度至少512长度,不过好像说现在用2048才算比较安全的了keygen.initialize(KEY_SIZE, secrand)// 初始化密钥生成器keys = keygen.generateKeyPair()// 生成密钥组AddKey("me", EncodeKey(keys.getPublic()))} catch (NoSuchAlgorithmException e) {e.printStackTrace()return false}ObjectOutputStream outtry {out = new ObjectOutputStream(new FileOutputStream(KEY_FILENAME))} catch (IOException e) {e.printStackTrace()return false}try {out.writeObject(keys)} catch (IOException e) {e.printStackTrace()return false} finally {try {out.close()} catch (IOException e) {e.printStackTrace()return false}}return true}public String EncryptMessage(String toUser, String Message) throws IOException {Key pubkey = allUserKeys.get(toUser)if ( pubkey == null ){throw new IOException("NoKeyForThisUser") }try {//PublicKey pubkey = keys.getPublic()rsaCipher.init(Cipher.ENCRYPT_MODE, pubkey, secrand)//System.out.println(rsaCipher.getBlockSize())返回0,非block 加密算法来的?//System.out.println(Message.getBytes("utf-8").length)//byte[] encryptedData = rsaCipher.doFinal(Message.getBytes("utf-8"))byte[] data = Message.getBytes("utf-8")int blocks = data.length / BLOCK_SIZE int lastBlockSize = data.length % BLOCK_SIZE byte [] encryptedData = new byte[ (lastBlockSize == 0 ? blocks : blocks + 1)* OUTPUT_BLOCK_SIZE]for (int i=0i <blocksi++){//int thisBlockSize = ( i + 1 ) * BLOCK_SIZE >data.length ? data.length - i * BLOCK_SIZE : BLOCK_SIZE rsaCipher.doFinal(data,i * BLOCK_SIZE, BLOCK_SIZE, encryptedData ,i * OUTPUT_BLOCK_SIZE)}if (lastBlockSize != 0 ){rsaCipher.doFinal(data, blocks * BLOCK_SIZE, lastBlockSize,encryptedData ,blocks * OUTPUT_BLOCK_SIZE)}//System.out.println(encrypted.length)如果要机密的数据不足128/256字节,加密后补全成为变为256长度的。//数量比较小时,Base64.GZIP产生的长度更长,没什么优势//System.out.println(Base64.encodeBytes(encrypted,Base64.GZIP).length())//System.out.println(Base64.encodeBytes(encrypted).length())//System.out.println (rsaCipher.getOutputSize(30))//这个getOutputSize 只对 输入小于最大的block时才能得到正确的结果。其实就是补全 数据为128/256 字节return Base64.encodeBytes(encryptedData)} catch (InvalidKeyException e) {e.printStackTrace()throw new IOException("InvalidKey") }catch (ShortBufferException e) {e.printStackTrace()throw new IOException("ShortBuffer") }catch (UnsupportedEncodingException e) {e.printStackTrace()throw new IOException("UnsupportedEncoding") } catch (IllegalBlockSizeException e) {e.printStackTrace()throw new IOException("IllegalBlockSize") } catch (BadPaddingException e) {e.printStackTrace()throw new IOException("BadPadding") }finally {//catch 中 return 或者throw之前都会先调用一下这里}}public String DecryptMessage(String Message) throws IOException {byte[] decoded = Base64.decode(Message)PrivateKey prikey = keys.getPrivate()try {rsaCipher.init(Cipher.DECRYPT_MODE, prikey, secrand)int blocks = decoded.length / OUTPUT_BLOCK_SIZEByteArrayOutputStream decodedStream = new ByteArrayOutputStream(decoded.length)for (int i =0 i <blocks i ++ ){decodedStream.write (rsaCipher.doFinal(decoded,i * OUTPUT_BLOCK_SIZE, OUTPUT_BLOCK_SIZE))}return new String(decodedStream.toByteArray(), "UTF-8")} catch (InvalidKeyException e) {e.printStackTrace()throw new IOException("InvalidKey")} catch (UnsupportedEncodingException e) {e.printStackTrace()throw new IOException("UnsupportedEncoding")} catch (IllegalBlockSizeException e) {e.printStackTrace()throw new IOException("IllegalBlockSize")} catch (BadPaddingException e) {e.printStackTrace()throw new IOException("BadPadding")} finally {// catch 中 return 或者throw之前都会先调用一下这里。}}public boolean AddKey(String user, String key) {PublicKey publickeytry {publickey = DecodePublicKey(key)} catch (Exception e) {return false}allUserKeys.put(user, publickey)SaveKeys()return true}private boolean LoadKeys() {BufferedReader inputtry {input = new BufferedReader(new InputStreamReader(new FileInputStream(OTHERS_KEY_FILENAME)))} catch (FileNotFoundException e1) {// e1.printStackTrace()return false}【实例下载】本文介绍RSA2加密与解密,RSA2是RSA的加强版本,在密钥长度上采用2048, RSA2比RSA更安全,更可靠, 本人的另一篇文章RSA已经发表,有想了解的可以点开下面的RSA文章
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)