我是靠谱客的博主 从容奇异果,这篇文章主要介绍Blackberry 10 js+html5 RSA加密,现在分享给大家,希望可以做个参考。

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解密后的顺序需要反过来:

复制代码
1
String pwd = sb.reverse().toString();

4,加密后的报文也会出现多处一位的情况,与BB6,7的解决方法一样.

5,在windows服务器与在linux获取秘钥文件的路径不同参考:

复制代码
1
2
3
4
5
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文件在下面参考处给出链接

复制代码
1
2
3
4
5
6
7
function getRSAkey_Encrpty(){ setMaxDigits(200); key_rsa = new RSAKeyPair("10001","","98565b6f053c21f78c645c64aec952989876279a357efe30ab3c75d482c7ef1c16927b2d5a255ad8beb7059792929885b2054ae005ba4fcaa70da5737e16305d7ecc852802e0360b4ab3c0ba92991a4e41ab49b4403cd46f038c859accba8e9229d6dbd8a1ce606b4681d8aea433073bea86ed6514e8600c30e32d94720ddafd"); return key_rsa; }

java端:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部