jQuery--base64前台加密解密
varpath=document.getElementById("path").value;
function app(info){
$("#txt").val($("#txt").val()+'\n'+info);
}
function subfunc(){
varput1=$.trim($("#putcardno01").val());
// varestxt=$.base64.encode(put1);
//varestxt=$.base64.btoa(put1);
varestxt=encodeBase64(put1);
$("#putcardno02").val(estxt);
app("加密后["+estxt+"]");
}
function subfunc02(){
varput1=$.trim($("#putcardno02").val());
//varestxt=$.base64.decode(put1);
//varestxt=$.base64.atob(put1);
varestxt=decodeBase64(put1);
app("解密后["+estxt+"]");
}
//////////////////////////////////////////
varnumTimes=5;
function subfunc03(){
varput1=$.trim($("#putcardno01").val());
// varestxt=$.base64.encode(put1);
//varestxt=$.base64.btoa(put1);
//estxt=$.base64.btoa(estxt);
estxt=encodeBase64(put1,numTimes);
$("#putcardno03").val(estxt);
app(numTimes+"次加密后["+estxt+"]");
}
function subfunc04(){
varput1=$.trim($("#putcardno03").val());
//varestxt=$.base64.decode(put1);
//varestxt=$.base64.atob(put1);
//estxt=$.base64.atob(estxt);
estxt=decodeBase64(put1,numTimes);
app(numTimes+"次解密后["+estxt+"]");
}
function clearrr(){
$("#putcardno02").val("");
$("#putcardno03").val("");
$("#putcardno04").val("");
$("#txt").val("");
}
//加密方法。没有过滤首尾空格,即没有trim.
//加密可以加密N次,对应解密N次就可以获取明文
function encodeBase64(mingwen,times){
varcode="";
varnum=1;
if(typeoftimes=='undefined'||times==null||times==""){
num=1;
}else{
varvt=times+"";
num=parseInt(vt);
}
if(typeofmingwen=='undefined'||mingwen==null||mingwen==""){
}else{
$.base64.utf8encode=true;
code=mingwen;
for(vari=0;i
code=$.base64.btoa(code);
}
}
return code;
}
//解密方法。没有过滤首尾空格,即没有trim
//加密可以加密N次,对应解密N次就可以获取明文
function decodeBase64(mi,times){
varmingwen="";
varnum=1;
if(typeoftimes=='undefined'||times==null||times==""){
num=1;
}else{
varvt=times+"";
num=parseInt(vt);
}
if(typeofmi=='undefined'||mi==null||mi==""){
}else{
$.base64.utf8encode=true;
mingwen=mi;
for(vari=0;i
mingwen=$.base64.atob(mingwen);
}
}
return mingwen;
}
/*
测试
输入 suolong2014version
加密后[c3VvbG9uZzIwMTR2ZXJzaW9u]
解密后[suolong2014version]
5次加密后[VjFod1QxWXlVblJUYTJoUVYwWmFhRnBYZEhOTk1WSlhWV3hPVG1KSVFscFZNalYzWVVaYU5tSkVSVDA9]
5次解密后[suolong2014version]
*/
package com.code;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
*
* Base64加密--解密
*
* @author lushuaiyin
*
*/
public class Base64Util {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Stringstr="suolong2014version";
System.out.println("测试明文["+str+"]");
Stringbasecode=Base64Util.encodeBase64(str);
System.out.println("加密后["+basecode+"]");
if(basecode!=null){
Stringres=Base64Util.decodeBase64(basecode);
System.out.println("解密后["+res+"]");
}
/////////////////////////////////////////
System.out.println("");
System.out.println("N次加密测试--------");
StringbasecodeN=Base64Util.encodeBase64(str, 2);
StringresN=Base64Util.decodeBase64(basecodeN, 2);
StringbasecodeN3=Base64Util.encodeBase64(str, 5);
StringresN3=Base64Util.decodeBase64(basecodeN3, 5);
}
//提供加密N次
public static String encodeBase64(String mingwen,int times){
intnum=(times<=0)?1:times;
Stringcode="";
if(mingwen==null||mingwen.equals("")){
}else{
code=mingwen;
for(inti=0;i
code=encodeBase64(code);
}
System.out.println("加密"+num+"次后["+code+"]");
}
return code;
}
//对应提供解密N次
public static String decodeBase64(String mi,int times){
intnum=(times<=0)?1:times;
Stringmingwen="";
if(mi==null||mi.equals("")){
}else{
mingwen=mi;
for(inti=0;i
mingwen=decodeBase64(mingwen);
}
System.out.println("解密"+num+"次后["+mingwen+"]");
}
return mingwen;
}
///////////////////////////////////////////////////////////////////
public static String encodeBase64(String mingwen){
Stringcode="";
if(mingwen==null||mingwen.equals("")){
}else{
BASE64Encoderencoder=newBASE64Encoder();
try {
code=encoder.encode(mingwen.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
// System.out.println("加密后["+code+"]");
}
return code;
}
public static String decodeBase64(String mi){
Stringmingwen="";
if(mi==null||mi.equals("")){
}else{
BASE64Decoderdecoder=newBASE64Decoder();
try {
byte[]by=decoder.decodeBuffer(mi);
mingwen=newString(by);
} catch (Exception e) {
e.printStackTrace();
}
// System.out.println("解密后["+mingwen+"]");
}
return mingwen;
}
}
/*
打印:
测试明文[suolong2014version]
加密后[c3VvbG9uZzIwMTR2ZXJzaW9u]
解密后[suolong2014version]
N次加密测试--------
加密2次后[YzNWdmJHOXVaekl3TVRSMlpYSnphVzl1]
解密2次后[suolong2014version]
加密5次后[VjFod1QxWXlVblJUYTJoUVYwWmFhRnBYZEhOTk1WSlhWV3hPVG1KSVFscFZNalYzWVVaYU5tSkVS
VDA9]
解密5次后[suolong2014version]
*/