概述
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
最后
以上就是从容奇异果为你收集整理的Blackberry 10 js+html5 RSA加密的全部内容,希望文章能够帮你解决Blackberry 10 js+html5 RSA加密所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复