做网站|网站建设,就上易助科技网

前端技术文档及相关资料下载

关于前端html、css、js等技术上的各种疑难棘手问题的解决方案探讨及相关资料下载!

JQuery加密解密插件jsencrypt
来源:易助科技网浏览量:6收藏

简介

jsencrypt是一jquery款字符串加密解密插件。


使用

1.  引入 jquery.min.js 和 jsencrypt.js 文件


2.  HTML

<h2>第一步:获取公钥</h2>
<button id="getKey">点击获取公钥</button>
<hr/>
<h2>第二步:将本地数据加密后,在远程进行解密</h2>
<input id="dataText1" placeholder="本地数据"/>
<button id="sendButton">发送</button>
<input id="dataText2" placeholder="远程解密"/>


3.  调用

(function(window) {
'use strict';

var rsa = function(params) {
$.extend(this.params, params);
this._init();
};

rsa.prototype = {
params: {
publicKey: '',
},

_init: function() {
var that = this;
that.getKeyClick();
that.sendButton();
},

/**
* 获取公钥
*/
getKeyClick: function() {
var that = this;

$('#getKey').click(function() {
$.get('testrsa.php', {act: 'get'}, function (result) {
result = $.parseJSON(result);
// 保存公钥
that.params.publicKey = result['publicKey'];
});
});
},

/**
* 发送测试数据
*/
sendButton: function() {
var that = this;

$('#sendButton').click(function() {
var dataText1 = $('#dataText1').val();

// step1:本地加密
var encrypt = new JSEncrypt();
encrypt.setPublicKey(that.params.publicKey);
var encrypted = encrypt.encrypt(dataText1);
console.log(encrypted);

// step2:服务器上私钥解密
$.get('testrsa.php', {act: 'data',dataText: encrypted}, function (result) {
result = $.parseJSON(result);
console.log(result['code']);
console.log(result['decrypted']);
$('#dataText2').val(result['decrypted']);
});
});
}
};
window.rsa = rsa;
})(window);


相关链接

Github 地址 :https://github.com/travist/jsencrypt