Vigenère Cipher Helper - codewars

简介: 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

相关文章
eggjs 项目报错 Cookie need secret key to sign and encrypt. Please set config.keys first
eggjs 项目报错 Cookie need secret key to sign and encrypt. Please set config.keys first
289 0
eggjs 项目报错 Cookie need secret key to sign and encrypt. Please set config.keys first
|
5月前
|
安全 网络安全
jsch 报错 no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha> 如何处理
【5月更文挑战第24天】jsch 报错 no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha> 如何处理
262 1
|
5月前
|
算法 网络安全
Unable to negotiate with 127.0.0.1 port 29215: no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha> 解决
【5月更文挑战第5天】Unable to negotiate with 127.0.0.1 port 29215: no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha> 解决
220 7
|
5月前
|
安全 网络安全
jsch 报错 no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha>问题处理方法
【5月更文挑战第10天】jsch 报错 no matching host key type found. Their offer: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha>问题处理方法
247 0
|
5月前
|
Web App开发 安全 小程序
Edge ERR_SSL_VERSION_OR_CIPHER_MISMATCH问题解决
以往应用的一些系统,可能因为年代久远,只能支持SSL的低版本协议,在Win 10和Win 11强制使用edge取代Internet explore以后: - 因为edge本身默认不支持低版本的SSL协议; - 老旧系统可能本身只能支持Internet explore。
1221 1
|
算法 大数据 数据安全/隐私保护
RSA加密:javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes
RSA加密:javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes
474 0
|
JavaScript 数据安全/隐私保护
Node.js使用jsrsasign实现SHA256withRSA加密算法
Node.js使用jsrsasign实现SHA256withRSA加密算法
949 0
|
JavaScript 算法 安全
JS常见加密 AES、DES、RSA、MD5、SHAI、HMAC、Base64 - Python/JS实现
本文仅仅介绍了常见的一些JS加密,并记录了JS和Python的实现方式 常见的加密算法基本分为这几类: (1)base64编码伪加密 (2)线性散列算法(签名算法)MD5 (3)安全哈希算法 SHAI (4)散列消息鉴别码 HMAC (5)对称性加密算法 AES,DES (6)非对称性加密算法 RSA
650 0
JS常见加密 AES、DES、RSA、MD5、SHAI、HMAC、Base64 - Python/JS实现
/usr/include/openssl/des.h:91:9: error: unknown type name ‘DES_LONG’ DES_LONG deslong[2];
/usr/include/openssl/des.h:91:9: error: unknown type name ‘DES_LONG’ DES_LONG deslong[2];
122 0
crypto_policy_set_aes_cm_128_hmac_sha1_32/crypto_policy_set_aes_cm_128_hmac_sha1_80找不到
crypto_policy_set_aes_cm_128_hmac_sha1_32/crypto_policy_set_aes_cm_128_hmac_sha1_80找不到
86 0