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

相关文章
|
移动开发 开发框架 JavaScript
uniapp如何与原生应用进行混合开发?
uniapp如何与原生应用进行混合开发?
1313 0
|
XML Java 数据库
【Spring】SpringBoot 配置文件
【Spring】SpringBoot 配置文件
|
弹性计算
阿里云ECS配置内网穿透服务器
阿里云ECS配置内网穿透服务器 包含FRP zerotier
|
6月前
|
传感器 人工智能 自然语言处理
火热邀测!DataWorks数据集成支持大模型AI处理
阿里云DataWorks数据集成新增大模型AI处理功能,支持在数据同步中无缝调用通义千问等AI模型,实现文本翻译、情感分析、摘要生成等功能。适用于电商客服、智能汽车、供应链、医疗、金融、法律及教育等多个场景,大幅提升数据处理效率与洞察深度。用户可通过自然语言配置,快速完成高级数据分析与处理,无需额外部署调试。立即申请测试资格,体验智能化数据处理!
1308 4
火热邀测!DataWorks数据集成支持大模型AI处理
|
6月前
|
人工智能 自然语言处理 前端开发
【CodeBuddy】三分钟开发一个实用小功能之:记忆翻牌配对游戏
CodeBuddy 是一款强大的 AI 编程助手,能够将自然语言描述快速转化为可运行的代码。通过记忆翻牌游戏的开发案例,展示了其从需求理解到技术实现的全流程支持:利用 CSS 的 `transform` 和 `grid` 布局实现动态卡片与响应式设计,借助 JavaScript 管理游戏状态和交互逻辑。AI 不仅能自动生成代码框架,还能优化动画效果、处理防抖机制等细节,大幅降低开发门槛。这一工具让开发者专注于创意本身,推动编程从“手动编写”向“思维传递”转变,开启人机协作新篇章。
251 7
【CodeBuddy】三分钟开发一个实用小功能之:记忆翻牌配对游戏
|
XML 物联网 API
服务端和客户端 RESTful 接口上传 Excel 的 Python 代码
本文作者木头左是物联网工程师,分享如何使用 Python 和 Flask-RESTful 构建一个简单的 RESTful API,实现文件上传功能,特别支持Excel文件。通过安装Flask和Flask-RESTful库,创建Flask应用,实现文件上传接口,并将其添加到API。该方法具有简单易用、灵活、可扩展及社区支持等优点。
服务端和客户端 RESTful 接口上传 Excel 的 Python 代码
|
监控
关键绩效指标和关键风险指标的区别
【8月更文挑战第31天】
690 0
|
XML 缓存 前端开发
Electron-builder 是如何打包 Windows 应用的?
本文首发于微信公众号“前端徐徐”,作者徐徐深入解析了 electron-builder 在 Windows 平台上的打包流程。文章详细介绍了 `winPackager.ts`、`AppxTarget.ts`、`MsiTarget.ts` 和 `NsisTarget.ts` 等核心文件,涵盖了目标创建、图标处理、代码签名、资源编辑、应用签名、性能优化等内容,并分别讲解了 AppX/MSIX、MSI 和 NSIS 安装程序的生成过程。通过这些内容,读者可以更好地理解和使用 electron-builder 进行 Windows 应用的打包和发布。
783 0
|
移动开发 前端开发 JavaScript
DSBridge框架使用说明
DSBridge框架使用说明
1163 0
|
机器学习/深度学习 人工智能 自然语言处理
混淆矩阵(Confusion Matrix)
随着机器学习和人工智能的迅速发展,分类模型成为了解决各种问题的重要工具。然而,仅仅知道模型预测对了多少样本是不够的。我们需要一种更详细、更系统的方法来理解模型的分类能力,以及它在不同类别上的表现。 混淆矩阵是在机器学习和统计学中用于评估分类模型性能的一种表格。它对模型的分类结果进行了详细的总结,特别是针对二元分类问题,另外混淆矩阵是用于评估分类模型性能的一种表格,特别适用于监督学习中的分类问题。它以矩阵形式展示了模型对样本进行分类的情况,将模型的预测结果与实际标签进行对比。
1417 1