当前位置: 首页 > 面试题库 >

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

易京
2023-03-14
本文向大家介绍使用js实现摩斯密码的加密和解密相关面试题,主要包含被问及使用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 code mapping-----------');
    for(var i in values) {
        if(values.hasOwnProperty(i)) {
            count++;
            console.log(i + '\t > ' + values[i]);
        }
    }
    console.log('total count: ' + count);
}

// morse code library
var morse = (function(global){
    var mcode = {},
        r_special = /\<\w+\>/g,
        r_find = /^\<(\w+)\>$/;

    // store datas mapping
    mcode.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
    mcode.error_flag = false;

    // 将字符串转换为摩斯码
    mcode.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);
    }

    //将摩斯码转换成字符串
    mcode.decode = 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 mcode;
}(this));
 类似资料:
  • 本文向大家介绍JS实现的base64加密、md5加密及sha1加密详解,包括了JS实现的base64加密、md5加密及sha1加密详解的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了JS实现的base64加密、md5加密及sha1加密。分享给大家供大家参考,具体如下: 1、base64加密 在页面中引入base64.js文件,调用方法为: 2、md5加密 在页面中引用md5.js文件,调用

  • 问题内容: 我想用Java加密和解密密码,然后以加密形式存储到数据库中。如果它是开源的,那就太好了。有什么建议/建议吗? 问题答案: 编辑 :这个答案是旧的。现在 不建议 使用MD5,因为它很容易被破坏。 我想象中的MD5必须足够好?您可以使用MessageDigest实现它。 这里还列出了其他算法。 如果确实需要,这是它的第三方版本: Fast MD5

  • 我正在尝试使用CryptoJS在JavaScript中进行加密,在C#中进行解密。花了很多时间试图让两种技术返回相同的输出。但是,输出是不同的--CryptoJS产生的加密字符串不同于C#产生的加密字符串。我做错了什么?谢谢你的帮助。

  • 我正在做一个简单的程序来加密/解密使用RSA算法在Java。我创建一个密码对象如下: 我通过调用加密函数进行加密: 解密过程如下: 但是,当我将创建密码对象的代码编辑为://Create a Cipher object Cipher rsapier=Cipher时。getInstance(“RSA”); 问题出在哪里。在第一种情况下(当空格出现时),我指定了NoPadd?为什么空格出现在解密的消息

  • 本文向大家介绍vue中使用cookies和crypto-js实现记住密码和加密的方法,包括了vue中使用cookies和crypto-js实现记住密码和加密的方法的使用技巧和注意事项,需要的朋友参考一下 使用crypto-js加解密 第一步,安装 第二步,在你需要的vue组件内import 第三步,使用 注意这个mymessage是字符串,如果你要加密的用户id(number类型)得先转成字符串

  • 本文向大家介绍java使用RSA加密方式实现数据加密解密的代码,包括了java使用RSA加密方式实现数据加密解密的代码的使用技巧和注意事项,需要的朋友参考一下 RSA的应用 RSA是一种非对称加密算法。现在,很多登陆表单的密码的都采用RSA加密,例如京东中的登陆使用公钥对密码进行加密 java使用RSA加密方式实现数据加密解密,需要首先产生私钥和公钥 测试代码 RSA工具类的实现 总结 以上所述是