Vigenère Cipher Helper - codewars

Vigenère Cipher Helper - codewars

月光魔力鸭

2019-01-11 19:41 阅读 1028 喜欢 1 维吉尼亚加解密 codewars

Vigenère Cipher Helper

The Vigenère cipher is a classic cipher originally developed by Italian cryptographer Giovan Battista Bellaso and published in 1553. It is named after a later French cryptographer Blaise de Vigenère, who had developed a stronger autokey cipher (a cipher that incorporates the message of the text into the key).
The cipher is easy to understand and implement, but survived three centuries of attempts to break it, earning it the nickname "le chiffre indéchiffrable" or "the indecipherable cipher."
From Wikipedia:

The Vigenère cipher is a method of encrypting alphabetic text by using a series of different Caesar ciphers based on the letters of a keyword. It is a simple form of polyalphabetic substitution. ... In a Caesar cipher, each letter of the alphabet is shifted along some number of places; for example, in a Caesar cipher of shift 3, A would become D, B would become E, Y would become B and so on. The Vigenère cipher consists of several Caesar ciphers in sequence with different shift values.

Assume the key is repeated for the length of the text, character by character. Note that some implementations repeat the key over characters only if they are part of the alphabet -- this is not the case here.
The shift is derived by applying a Caesar shift to a character with the corresponding index of the key in the alphabet.
Visual representation:
"my secret code i want to secure"  // message
"passwordpasswordpasswordpasswor"  // key
Write a class that, when given a key and an alphabet, can be used to encode and decode from the cipher.

Example

var alphabet = 'abcdefghijklmnopqrstuvwxyz';
var key = 'password';

// creates a cipher helper with each letter substituted
// by the corresponding character in the key
var c = new VigenèreCipher(key, alphabet);

c.encode('codewars'); // returns 'rovwsoiv'
c.decode('laxxhsj');  // returns 'waffles'
Any character not in the alphabet must be left as is. For example (following from above):
c.encode('CODEWARS'); // returns 'CODEWARS'

There's my solution below

function VigenèreCipher(key,abc){
    this.encode = function(str){
        //对str进行分割,确认是否在字母表中,然后按照
        return str.split('').map((item,i)=>{
            //按照key和abc中的位置,进行移动找到当前所在的位置
            return abc.indexOf(item) > -1 ? 
            abc[(abc.indexOf(item) + abc.indexOf(key[i % key.length])) % abc.length]
            : item;
        }).join('');
    }
    this.decode = function(str){
        //与encode相反,获得坐标在移动回来
        return str.split('').map((item,i)=>{
            //按照key和abc中的位置,进行移动找到当前所在的位置
            return abc.indexOf(item) > -1 ? 
            abc[( abc.length + abc.indexOf(item) - abc.indexOf(key[i % key.length]) ) % abc.length]
            : item;
        }).join('');
    }
}

Question From : https://www.codewars.com/kata/52d1bd3694d26f8d6e0000d3/train/javascript

转载请注明出处: https://chrunlee.cn/article/code-war-cipher-helper.html


感谢支持!

赞赏支持
提交评论
评论信息 (请文明评论)
暂无评论,快来快来写想法...
推荐
在我们web开发过程中经常会碰到针对table的一些dom操作,这里整理一下关于这方面的知识点。当然我们可以通过jquery这样的插件来处理,或许会更简单一些,不过现在简单说下原生JS是如何操作的
近期需求:将一棵树导出到excel中,树是ztree,通过插件Table2excel导出table到excel中。
通过canvas可以进行画图实现一些动画效果等,今天练习下通过canvas来实现一个简易的电子画板,可以在白板上进行画画,然后指定不同的颜色、线条粗细,加载不同的背景以及擦除效果。
web开发中,前台有时候会需要一个随机数或序列,通常来说,这个随机数可能只在当前页面中使用,并不需要太过严格,大体上重复率低即可。
最大公因数,也称最大公约数、最大公因子,指两个或多个整数共有约数中最大的一个。a,b的最大公约数记为(a,b),同样的,a,b,c的最大公约数记为(a,b,c),多个整数的最大公约数也有同样的记号。求最大公约数有多种方法,常见的有质因数分解法、短除法、辗转相除法、更相减损法。与最大公约数相对应的概念是最小公倍数,a,b的最小公倍数记为[a,b]。
nvm install 16.15.0 : The process cannot access the file because it is being used by another process
jsQR 是一款纯粹的由javascript实现的二维码识别库,可以在浏览器端使用,也可以在后端node.js环境使用。我之前使用过其他的识别库,例如:qrcode-reader 或其他,在使用上都比较麻烦,而且识别率并不高。jsQR是后来发现的,感觉(没有实际对比验证)jsQR识别率要更高些,使用起来也更简单,不需要安装其他依赖软件。
web网站上总会有在文本域中提交代码的操作,那么如何处理呢?