Vigenère Cipher Helper - codewars

Vigenère Cipher Helper - codewars

月光魔力鸭

2019-01-11 19:41 阅读 964 喜欢 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


感谢支持!

赞赏支持
提交评论
评论信息 (请文明评论)
暂无评论,快来快来写想法...
推荐
在开发过程中经常会碰到跨frame 去操作的需求,那么如何获得这个frame呢,这里写了一个工具类,用来通过name获得frame的jq对象
突然来了一个调研任务,想要实现一个类似3D虚拟展厅类似的需求,主要就是放一些学生的作品,然后预览啥的,效果还是要全景的效果。 经过一番调查,最终锁定了以前从未接触过的krpano。
Question from codewar,about all of array combinations.
之前的时候都是在各大主机厂商手动进行申请免费的,直到阿里的免费期限变更为3个月.. 我就开始觉的有些麻烦了,还不如使用这个let's encrypt进行部署呢。
先记录下,不定哪天就查了..防止找不到或不全
开发的项目中有使用到微信开发,由于之前偶尔才用一次,也是用的别人的地址和测试号,这里记录下自己的操作。防止后续遗忘。
最近折腾的少了,实在没的写了,大约三四个月没更新了,先水一篇。
做作业的时候,需要在手机上预览下,但是发现如果想在移动端上展示A4样子的作业还是挺麻烦的,最后还是准备通过图片来展示,然后移动端缩放呗。。