栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用js实现摩斯密码的加密和解密

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用js实现摩斯密码的加密和解密

网络上看到的,很长

var utils = utils || {};utils.isArray = function(value) {    return Object.prototype.toString.apply(value) === '[object Array]';}utils.trim = function(value) {    return value.trim ? value.trim() : value.replace(/^s+|s+$|/g,'');}// 解决IE不兼容console问题var console = console || {};console.log = console.log || function(){};console.error = console.error || function(){};// 使用字典存储摩斯码对照关系function Dictionary() {    this.datasource = {};    this.rdatasource = {};}Dictionary.prototype.add = function(keys, values) {    if(typeof keys === 'undefined' || typeof values === 'undefined') {        console.error('Illegal arguments');        return ;    }    if(utils.isArray(keys) && utils.isArray(values)) {        if(keys.length != values.length) { console.error('keys length not equals values length'); return ;        }        for(var i = 0; i < keys.length; i++) { this.datasource[keys[i]] = values[i];        }        return ;    }    this.datasource[keys] = values;}Dictionary.prototype.reversal = function(){    var tempData = this.datasource;    for(var i in tempData) {        if(tempData.hasOwnProperty(i)) { this.rdatasource[tempData[i]] = i;        }    }}Dictionary.prototype.showAll = function(values) {    var count = 0;    console.log('-----------morse pre mapping-----------');    for(var i in values) {        if(values.hasOwnProperty(i)) { count++; console.log(i + 't > ' + values[i]);        }    }    console.log('total count: ' + count);}// morse pre libraryvar morse = (function(global){    var mpre = {},        r_special = /<w+>/g,        r_find = /^<(w+)>$/;    // store datas mapping    mpre.mdatas = (function(){        var dictionaryDS = new Dictionary();        // initial mappping        dictionaryDS.add( [     'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',     '1','2','3','4','5','6','7','8','9','0',     'AA','AR','AS','BK','BT','CT','SK','SOS',     '.',':',',',';','?','=',"'",'/','!','-','_','"','(',')','$','&','@','+' ], [     // letter     '.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..',     // number      '.----','..---','...--','....-','.....','-....','--...','---..','----.','-----',     // special charactor     '.-.-','.-.-.','.-...','-...-.-','-...-','-.-.-','...-.-','...---...',     // punctuation     '.-.-.-','---...','--..--','-.-.-.','..--..','-...-','.----.','-..-.','-.-.--','-....-','..--.-','.-..-.','-.--.','-.--.-','...-..-','.-...','.--.-.','.-.-.' ]        );        return dictionaryDS;    }());        // error flag    mpre.error_flag = false;    // 将字符串转换为摩斯码    mpre.parse = function(values) {        // console.log('input: ' + values);        this.error_flag = false;        var _datasource = this.mdatas.datasource, item = '', a_special = [], a_temp = [], a_value = [], count = 0, result = '';        values = values.toUpperCase();        a_special = values.match(r_special);        a_temp = values.split(r_special);        // 将用户输入的字符串转换成数组        for(var i=0; i<a_temp.length; i++) { item = a_temp[i]; if(item !== '') {     // IE无法通过下标来索引字符串     if(!item[0]){         item = item.split('');     }     for(var j=0; j<item.length; j++) {         a_value[count++] = item[j];     } } // 当前字符串为<AS>形式,提取AS字符 if(i !== a_temp.length - 1){     a_value[count++] = a_special[i].match(r_find)[1]; }        }        // 将解析数组形式的用户输入值        for(var i=0; i<a_value.length; i++) { item = a_value[i]; if(item === ' ') {     result += '/ '; } else if(typeof _datasource[item] === 'undefined') {     this.error_flag = true;     // console.error('Invalid characters in input.')     result += '? '; }else {     result += _datasource[item] + ' '; }        }        return utils.trim(result);    }    //将摩斯码转换成字符串    mpre.depre = function(values) {        // console.log('input: ' + values);        this.error_flag = false;        this.mdatas.reversal();        var _rdatasource = this.mdatas.rdatasource, a_input = values.split(' '), result = '', item = '', c_result = '';        for(var i=0; i<a_input.length; i++) { item = a_input[i]; if(item === '/') {     result += ' '; }else {     c_result = _rdatasource[item];     if(typeof c_result === 'undefined') {         this.error_flag = true;         // console.error('Invalid characters in input.')         result += '?';     } else {         if(c_result.length > 1){  result += '<' + c_result + '>';         } else {  result += c_result;         }     } }        }        return result;    }    return mpre;}(this));
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/383084.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号