rsa.js
2.22 KB
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
var __RSAEcrypt__ = (function(){
return (function(){
var RSA_CODE = null;
var RSA_PUB = null;
var RSA_FLAG = null;
return {
rsa_data_encrypt:function(value){
var rsa_check_data = function(value,rsa_pub){
if(!value){
//alert("值不能为空!")
return false;
}else if(!rsa_pub){
//alert("公钥不能为空!请重新获取公钥!");
return false;
}
return true;
}
var checkResult = rsa_check_data(value,RSA_PUB);
if(checkResult){
var encrypt = new JSEncrypt();
encrypt.setPublicKey(RSA_PUB);
var data = null;
if(!RSA_CODE)RSA_CODE = "";
var groupLength = 240;
if(value.length>groupLength){//需要分段加密
data = "";
var length = value.length;
var groups = Math.floor(length/groupLength)+1;
var result = "";
for(var i = 0;i<groups; i++){
var v = "";
if(i!=groups-1){
v = value.substring(i*groupLength,(i+1)*groupLength);
}else{
v = value.substring(i*groupLength);
}
if(!!v){
data += encrypt.encrypt(v+RSA_CODE)+RSA_FLAG;
}
}
}else{
data = encrypt.encrypt(value+RSA_CODE)+RSA_FLAG;
}
return data;
}
return value;
},
rsa_encrypt:function(callback){
__RSAEcrypt__.initRsaCode(callback);
},
getRsaCode:function(){
return RSA_CODE;
},
refreshRsaCode:function(callback){
jQuery.ajax({
url:"/rsa/weaver.rsa.GetRsaInfo?ts="+new Date().getTime(),
type:"get",
dataType:"json",
success:function(data){
RSA_CODE = data.rsa_code;
RSA_PUB = data.rsa_pub;
RSA_FLAG = data.rsa_flag;
if(callback){
callback();
}
}
});
},
initRsaCode:function(callback){
if(RSA_CODE){
if(callback){
callback();
}
}else{
__RSAEcrypt__.refreshRsaCode(callback);
}
}
}
})();
})();
jQuery(document).ready(function(){
__RSAEcrypt__.initRsaCode();
});