Javascript实现摩斯码加密解密
摩尔斯电码是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母、数字和标点符号,是由美国人萨缪尔·摩尔斯在1836年发明。
每一个字符(字母或数字)对应不同的序列(由点和划组成)。
一般来说,任何一种能把书面字符用可变长度的信号表示的编码方式都可以称为摩尔斯电码。
但现在这一术语只用来特指两种表示英语字母和符号的摩尔斯电码:美式摩尔斯电码和国际摩尔斯电码。下面内容仅针对国际摩尔斯电码。
字母、数字、标点、特殊字符与摩斯码对照表


除了上面的字符,也可以通过声音或光线亮度等媒介来传递摩斯密码信息,以声音为例,嘀代表(短声),哒代表(长声)。
摩斯码相关事件
- 前段时间被ISIS处决的日本人质后藤,在行刑过程中他的眨眼行为被摩斯电码专家解读为通过眨眼传递信息,翻译成文字为“不要救我,放弃我!”
- 在1912年的泰坦尼克号游轮沉没前,也曾发送SOS的求救信号。
Javascript实现摩斯码加密解析
- 使用字典来实现摩斯码与字符的对应关系,该字典也是基于对象来实现
- 低版本的IE无法通过下标来索引字符串进行遍历
- IE在str.split(regExp) 中存在一个bug
var str = 'abb' ;
var r = /b/g ;
alert(str.split('b')); // chrome下提示 a,, IE8下提示 a,,
alert(str.split(r)); // chrome下提示 a,, IE8下提示 a
完整代码:
/*
* @author liaoyu
* @created 2015-03-14
*/
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));
参考资料链接
Morse Code Wiki
摩尔斯电码中文WIKI
International Morse Code对照表
摩斯码转换实现参考
Javascript实现摩斯码加密解密的更多相关文章
- uva 508 - Morse Mismatches(摩斯码)
来自https://blog.csdn.net/su_cicada/article/details/80084529 习题4-6 莫尔斯电码(Morse Mismatches, ACM/ICPC Wo ...
- 兼容javascript和C#的RSA加密解密算法,对web提交的数据进行加密传输
Web应用中往往涉及到敏感的数据,由于HTTP协议以明文的形式与服务器进行交互,因此可以通过截获请求的数据包进行分析来盗取有用的信息.虽然https可以对传输的数据进行加密,但是必须要申请证书(一般都 ...
- [LeetCode] Unique Morse Code Words 独特的摩斯码单词
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- [LeetCode] 804. Unique Morse Code Words 独特的摩斯码单词
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- 生成二维码 加密解密类 TABLE转换成实体、TABLE转换成实体集合(可转换成对象和值类型) COOKIE帮助类 数据类型转换 截取字符串 根据IP获取地点 生成随机字符 UNIX时间转换为DATETIME\DATETIME转换为UNIXTIME 是否包含中文 生成秘钥方式之一 计算某一年 某一周 的起始时间和结束时间
生成二维码 /// <summary>/// 生成二维码/// </summary>public static class QRcodeUtils{private static ...
- keyring源码加密解密函数分析
Encrypt the page data contents. Page type can't be FIL_PAGE_ENCRYPTED, FIL_PAGE_COMPRESSED_AND_ENCRY ...
- python-摩斯码转换
意义:简单实现摩斯码的破译和生成 代码: #-*- coding: UTF-8 -*- ' __date__ = '2016/2/2' import pprint import re chars = ...
- JavaScript加密解密7种方法总结分析
原文地址:http://wenku.baidu.com/view/9048edee9e31433239689357.html 本文一共介绍了七种javascript加密方法: 在做网页时(其实是网页木 ...
- Java & PHP & Javascript 通用 RSA 加密 解密 (长字符串)
系统与系统的数据交互中,有些敏感数据是不能直接明文传输的,所以在发送数据之前要进行加密,在接收到数据时进行解密处理:然而由于系统与系统之间的开发语言不同. 本次需求是生成二维码是通过java生成,由p ...
随机推荐
- asp.net 中使用不同的数据源绑定gridview
第一种,使用SqlDataReader绑定gridview.代码如下: public SqlDataReader bind() { SqlConnection con = new SqlConnect ...
- 使用c#生成Identicon图片
Identicon是什么 我们在站点注册的时候通常系统会在我们没有提供自定义头像时为我们指定一个默认的头像,不过,样子千篇一律很是难看.聪明的程序员想了很多办法来解决这个问题,比如你能在这里看到很漂亮 ...
- ###《Machine Learning in Action》 - KNN
初学Python:理解机器学习. 算法是需要实现的,纸上得来终觉浅. // @author: gr // @date: 2015-01-16 // @email: forgerui@gmail.com ...
- asp:时间的显示
DateTime dt = DateTime.Now;// Label1.Text = dt.ToString();//2005-11-5 13:21:25// Label2.Text = ...
- 【html】【11】函数名称约束规范
一.匈牙利命名法: [不推荐]基本原则是:变量名=属性+类型+对象描述,其中每一对象的名称都要求有明确含义,可以取对象名字全称或名字的一部分.要基于容易记忆容易理解的原则.保证名字的连贯性是非常重要的 ...
- FreeMarker语法2
FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主要由如下4个部分组成: 1,文本:直接输出的部分 2,注释:<#-- ... -->格式部分,不会输 ...
- Trie,HDU1875world puzzle
附上代码 #include<iostream> #include<cstdio> #include<string> #include<cstring> ...
- HTML5之图像处理
--- 内嵌图像 - drawImage可以绘制图像context.drawImage(image,dx,dy)context.drawImage(image,dx,dy,dw,dh)context. ...
- CENTOS7 使用网络管理器配置静态IP地址
CENTOS7 的网络配置和CENTOS6有些不同. 如果你想要使用网络管理器来管理该接口,你可以使用nmtui(网络管理器文本用户界面),它提供了在终端环境中配置配置网络管理器的方式. 在使用nmt ...
- javascripct流程语句
1.条件选择 if 语句:只有当指定条件为true时,使用该语句来执行代码 if...else语句:当条件为true时执行代码,当条件为 false 时执行其他代码 if...else i ...