打算寫這個類用於spark插件加密對話消息用
RSA的Java實現不能一次加密很大的字符
Base
其他的保存公鑰到文件等簡單的實現
==============================================
import java
import java
import java
import java
import java
import javax
import java
public class Encryptor {
private static final String KEY_FILENAME =
private static final String OTHERS_KEY_FILENAME =
// private static final int KEY_SIZE =
// private static final int BLOCK_SIZE =
// private static final int OUTPUT_BLOCK_SIZE =
private static final int KEY_SIZE =
private static final int BLOCK_SIZE =
//這個值與 KEY_SIZE 已經padding方法有關
//可能
private static final int OUTPUT_BLOCK_SIZE =
private SecureRandom secrand;
private Cipher rsaCipher;
private KeyPair keys;
private Map<String
public Encryptor() throws Exception {
try {
allUserKeys = new HashMap<String
secrand = new SecureRandom();
//SunJCE Provider 中只支持ECB mode
//NOPadding導致解壓出來的都是blocksize長度的數據
//參見 l
//
//另外根據 Open
// 中代碼的注釋
//如果用doFinal來加密超過的一個操作所允許的長度數據將拋出異常
//根據keysize的長度
//一次doFinal調用只能加密
//(
//想用來加密大量數據的只能自己用其他辦法實現了
rsaCipher = Cipher
} catch (NoSuchAlgorithmException e) {
e
} catch (NoSuchPaddingException e) {
e
throw e;
}
ObjectInputStream in;
try {
in = new ObjectInputStream(new FileInputStream(KEY_FILENAME));
} catch (FileNotFoundException e) {
if (false == GenerateKeys())
{
throw e;
}
LoadKeys();
return;
}
keys = (KeyPair) in
in
LoadKeys();
}
/*
* 生成自己的公鑰和私鑰
*/
private Boolean GenerateKeys() {
try {
KeyPairGenerator keygen = KeyPairGenerator
// secrand = new SecureRandom();
// sedSeed之後會造成 生成的密鑰都是一樣的
// secrand
//key長度至少
keygen
keys = keygen
AddKey(
} catch (NoSuchAlgorithmException e) {
e
return false;
}
ObjectOutputStream out;
try {
out = new ObjectOutputStream(new FileOutputStream(KEY_FILENAME));
} catch (IOException e) {
e
return false;
}
try {
out
} catch (IOException e) {
e
return false;
} finally {
try {
out
} catch (IOException e) {
e
return false;
}
}
return true;
}
public String EncryptMessage(String toUser
Key pubkey = allUserKeys
if ( pubkey == null )
{
throw new IOException(
}
try {
//PublicKey pubkey = keys
rsaCipher
//System
//System
//byte[] encryptedData = rsaCipher
byte[] data = Message
int blocks = data
int lastBlockSize = data
byte [] encryptedData = new byte[ (lastBlockSize ==
for (int i=
{
//int thisBlockSize = ( i +
rsaCipher
}
if (lastBlockSize !=
rsaCipher
}
//System
//數量比較小時
//System
//System
//System
//這個getOutputSize 只對 輸入小於最大的block時才能得到正確的結果
return Base
} catch (InvalidKeyException e) {
e
throw new IOException(
}catch (ShortBufferException e) {
e
throw new IOException(
}
catch (UnsupportedEncodingException e) {
e
throw new IOException(
} catch (IllegalBlockSizeException e) {
e
throw new IOException(
} catch (BadPaddingException e) {
e
throw new IOException(
}finally {
//catch 中 return 或者throw之前都會先調用一下這裡
}
}
public String DecryptMessage(String Message) throws IOException {
byte[] decoded = Base
PrivateKey prikey = keys
try {
rsaCipher
int blocks = decoded
ByteArrayOutputStream decodedStream = new ByteArrayOutputStream(decoded
for (int i =
{
decodedStream
}
return new String(decodedStream
} catch (InvalidKeyException e) {
e
throw new IOException(
} catch (UnsupportedEncodingException e) {
e
throw new IOException(
} catch (IllegalBlockSizeException e) {
e
throw new IOException(
} catch (BadPaddingException e) {
e
throw new IOException(
} finally {
// catch 中 return 或者throw之前都會先調用一下這裡
}
}
public boolean AddKey(String user
PublicKey publickey;
try {
publickey = DecodePublicKey(key);
} catch (Exception e) {
return false;
}
allUserKeys
SaveKeys();
return true;
}
private boolean LoadKeys() {
BufferedReader input;
try {
input = new BufferedReader(new InputStreamReader(
new FileInputStream(OTHERS_KEY_FILENAME)));
} catch (FileNotFoundException e
// e
return false;
}
try {
allUserKeys
String line;
while ((line = input
String[] temp = line
String user = temp[
PublicKey key = DecodePublicKey(temp[
allUserKeys
}
} catch (Exception e) {
return false;
} finally {
try {
input
} catch (Exception e) {
return false;
}
}
return true;
}
private boolean SaveKeys() {
FileWriter output;
try {
output = new FileWriter(OTHERS_KEY_FILENAME);
} catch (IOException e
//
return false;
}
try {
for (String user : allUserKeys
Key key = allUserKeys
output
}
} catch (IOException e
//
return false;
} finally {
try {
output
} catch (Exception e) {
return false;
}
}
return true;
}
/**
* 解密base
*
* @param key
* 密鑰字符串(經過base
* @throws Exception
*/
public static PublicKey DecodePublicKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = Base
X
KeyFactory keyFactory = KeyFactory
PublicKey publicKey = keyFactory
return publicKey;
}
/**
* 解密base
*
* @param key
* 密鑰字符串(經過base
* @throws Exception
*/
public static PrivateKey DecodePrivateKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = Base
PKCS
KeyFactory keyFactory = KeyFactory
PrivateKey privateKey = keyFactory
return privateKey;
}
/**
* 編碼key為base
*
* @return
*/
public static String EncodeKey(Key key) {
byte[] keyBytes = key
// System
String s = Base
return s;
}
}
===============測試類============================
import java
import java
import java
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// Provider [ ] providers = Security
// for ( int i =
// {
// String name = providers[i]
// String info = providers[i]
// double version = providers[i]
// System
// System
// System
// System
//
// for ( Iterator iter = providers[i]
// {
// String key = (String) iter
// System
//
// providers[i]
// }
//
// System
//
//
// }
//
// int i =
// System
// i =
// System
// i =
// System
// i =
// System
//String str =
//String str =
String str =
try {
Encryptor ddd = new Encryptor();
str = ddd
// System
String str
str
str
str = ddd
} catch (Exception e) {
if (e
System
}
}
// str = ddd
//
//System
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25887.html