Blackberry10 使用js+html5開發 RSA加密解密時遇到的問題:BB10端使用js加密與解密, 伺服器端使用java加密與解密
1,加密非常的簡單代碼機會上沒怎麼修改,另外js加密可能出現的問題在BB10 AES加密中已經說過,js RSA加密需要導入3個js檔案 分别是Barrett.js,BigInt.js,RSA.js
2,js中的公鑰為:10001,modulus為:98565b6f053c21f78c645c64aec952989876279a357efe30ab3c75d482c7ef1c16927b2d5a255ad8beb
7059792929885b2054ae005ba4fcaa70da5737e16305d7ecc852802e0360b4ab3c0ba92991a4e41ab49b
4403cd46f038c859accba8e9229d6dbd8a1ce606b4681d8aea433073bea86ed6514e8600c30e32d9472
0ddafd(在java端生成)
3,java端與BB6,7的沒有很大的差別隻是填充方式不同,有一點注意的是跟WinPhone8的一樣java解密後的順序需要反過來:
String pwd = sb.reverse().toString();
4,加密後的封包也會出現多處一位的情況,與BB6,7的解決方法一樣.
5,在windows伺服器與在linux擷取秘鑰檔案的路徑不同參考:
public static String getRSAPairFilePath() {
String urlPath = RSAUtils.class.getResource("/").getPath();
String path=(new File(urlPath).getParent() +File.separator+ RSAKeyStore);
return path;
}
不多說Blackberry10 的RSA加密的java端與Blackberry6,7的幾乎上一樣.直接上代碼:
js 端:需要的3個js檔案在下面參考處給對外連結接
function getRSAkey_Encrpty(){
setMaxDigits(200);
key_rsa = new RSAKeyPair("10001","","98565b6f053c21f78c645c64aec952989876279a357efe30ab3c75d482c7ef1c16927b2d5a255ad8beb7059792929885b2054ae005ba4fcaa70da5737e16305d7ecc852802e0360b4ab3c0ba92991a4e41ab49b4403cd46f038c859accba8e9229d6dbd8a1ce606b4681d8aea433073bea86ed6514e8600c30e32d94720ddafd");
return key_rsa;
}
java端:
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
import org.apache.log4j.Logger;
import com.heaven.sowbb.server.SowServlet;
public class RSAUtil {
private static String RSAKeyStore = "RSAKey.txt";
public static Logger log = Logger.getLogger(RSAUtil.class);
/**
* * 生成密鑰對 *
*
* @return KeyPair *
* @throws EncryptException
*/
public static KeyPair generateKeyPair() throws Exception {
try {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
// 沒什麼好說的了,這個值關系到塊加密的大小,可以更改,但是不要太大,否則效率會低
final int KEY_SIZE = 1024;
keyPairGen.initialize(KEY_SIZE, new SecureRandom());
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
System.out.println(publicKey.getModulus().toString(16));
System.out.println(publicKey.getPublicExponent().toString(16));
System.out.println(privateKey.getPrivateExponent().toString(16));
System.out.println(publicKey.getPublicExponent().toString(16).length());
System.out.println(privateKey.getPrivateExponent().toString(16).length());
System.out.println("99e28e237fa52ee766e38db10b2bbbcc3fc8f0549781547bbef2d75e952d1f5fda9bb0b61964760fdab21c3f4a940a51e80781da50dd9623ac589a152998e1710b549677e40b74d81bbb039eb8196bda28a9e8c53920f5da1c23e559b9afc39d08dfce7f00fdef88059bec49063f57179628aa259e63d2bf837057b0b25cadf3".length());
saveKeyPair(keyPair);
return keyPair;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public static String getRSAPairFilePath() {
String urlPath = RSAUtils.class.getResource("/").getPath();
String path=(new File(urlPath).getParent() +File.separator+ RSAKeyStore);
try{
log.error(path);
}catch(Exception e){}
return path;
// return (new File(urlPath).getParent() + RSAKeyStore);
}
public static KeyPair getKeyPair() throws Exception {
String path=getRSAPairFilePath();
System.out.println(path);
FileInputStream fis = new FileInputStream(path);
ObjectInputStream oos = new ObjectInputStream(fis);
KeyPair kp = (KeyPair) oos.readObject();
oos.close();
fis.close();
return kp;
}
public static void saveKeyPair(KeyPair kp) throws Exception {
FileOutputStream fos = new FileOutputStream(RSAKeyStore);
ObjectOutputStream oos = new ObjectOutputStream(fos);
// 生成密鑰
oos.writeObject(kp);
oos.close();
fos.close();
}
/**
* * 生成公鑰 *
*
* @param modulus *
* @param publicExponent *
* @return RSAPublicKey *
* @throws Exception
*/
public static RSAPublicKey generateRSAPublicKey(byte[] modulus,
byte[] publicExponent) throws Exception {
KeyFactory keyFac = null;
try {
keyFac = KeyFactory.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
} catch (NoSuchAlgorithmException ex) {
throw new Exception(ex.getMessage());
}
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(
modulus), new BigInteger(publicExponent));
try {
return (RSAPublicKey) keyFac.generatePublic(pubKeySpec);
} catch (InvalidKeySpecException ex) {
throw new Exception(ex.getMessage());
}
}
/**
* * 生成私鑰 *
*
* @param modulus *
* @param privateExponent *
* @return RSAPrivateKey *
* @throws Exception
*/
public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus,
byte[] privateExponent) throws Exception {
KeyFactory keyFac = null;
try {
keyFac = KeyFactory.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
} catch (NoSuchAlgorithmException ex) {
throw new Exception(ex.getMessage());
}
RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(
modulus), new BigInteger(privateExponent));
try {
return (RSAPrivateKey) keyFac.generatePrivate(priKeySpec);
} catch (InvalidKeySpecException ex) {
throw new Exception(ex.getMessage());
}
}
/**
* * 加密 *
*
* @param key
* 加密的密鑰 *
* @param data
* 待加密的明文資料 *
* @return 加密後的資料 *
* @throws Exception
*/
public static byte[] encrypt(PublicKey pk, byte[] data) throws Exception {
try {
Cipher cipher = Cipher.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, pk);
// 獲得加密塊大小,如:加密前資料為128個byte,而key_size=1024
int blockSize = cipher.getBlockSize();
// 加密塊大小為127
// byte,加密後為128個byte;是以共有2個加密塊,第一個127
// byte第二個為1個byte
// 獲得加密塊加密後塊大小
int outputSize = cipher.getOutputSize(data.length);
int leavedSize = data.length % blockSize;
int blocksSize = leavedSize != 0 ? data.length / blockSize + 1
: data.length / blockSize;
byte[] raw = new byte[outputSize * blocksSize];
int i = 0;
while (data.length - i * blockSize > 0) {
if (data.length - i * blockSize > blockSize)
cipher.doFinal(data, i * blockSize, blockSize, raw, i
* outputSize);
else
cipher.doFinal(data, i * blockSize, data.length - i
* blockSize, raw, i * outputSize);
// 這裡面doUpdate方法不可用,檢視源代碼後發現每次doUpdate後并
// 沒有什麼實際動作除了把byte[]放到ByteArrayOutputStream中,
//而最後doFinal的時候才将所有的byte[]進行加密,可是到了此時加
// 密塊大小很可能已經超出了OutputSize是以隻好用dofinal方法。
i++;
}
return raw;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
/**
* * 解密 *
*
* @param key
* 解密的密鑰 *
* @param raw
* 已經加密的資料 *
* @return 解密後的明文 *
* @throws Exception
*/
public static byte[] decrypt(PrivateKey pk, byte[] raw) throws Exception {
try {
Cipher cipher = Cipher.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher.init(cipher.DECRYPT_MODE, pk);
int blockSize = cipher.getBlockSize();
ByteArrayOutputStream bout = new ByteArrayOutputStream(64);
int j = 0;
//判斷數組首元素是否為0,若是,則将其删除,保證模的位數是128
if(raw[0]==0 && raw.length==129)
{
byte[] temp=new byte[raw.length-1];
for(int i=0;i<raw.length-1;i++)
{
temp[i]=raw[i+1];
}
raw=temp;
}
while (raw.length - j * blockSize > 0) {
bout.write(cipher.doFinal(raw, j * blockSize, blockSize));
j++;
}
return bout.toByteArray();
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public static String decrypt(String result) throws Exception{
byte[] en_result = new BigInteger(result, 16).toByteArray();
try{
byte[] de_result = decrypt(getKeyPair().getPrivate(),
en_result);
StringBuffer sb = new StringBuffer();
sb.append(new String(de_result));
//将字元串順序反過來
String pwd = sb.reverse().toString();
return pwd;
}catch(Exception e){
throw new Exception(e.getMessage());
}
}
}
參考網站:http://sosuny.iteye.com/blog/793327
blackberry10 端RSA加密 js:http://download.csdn.net/detail/g56667426/6834475