카이사르 암호와 자바스크립트
카이사르 암호란?
In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.
암호
const caesarCipher = (str, shift) => {
const letters = 'abcdefghijklmnopqrstuvwxyz'.split('');
let res = '';
for (let i = 0; i < str.length; i++) {
const char = str[i];
const ind = letters.indexOf(char);
if (ind === -1) {
res += char;
continue;
}
const encodedIndex = (ind + shift) % 26;
res += letters[encodedIndex];
}
return res;
};
모카 테스트
mocha.setup('bdd');
const { assert } = chai;
describe('caesarCipher()', () => {
it('Shifting Letters Successfully', () => {
assert.equal(caesarCipher('c', -2), 'a');
assert.equal(caesarCipher('abcd', 1), 'bcde');
assert.equal(caesarCipher('yz', 1), 'za');
assert.equal(caesarCipher('abcd', 100), 'wxyz');
});
it("Doesn't shift non-alphabetic Characters", () => {
assert.equal(caesarCipher('gurer ner 9 qbtf!', 13), 'there are 9 dogs!');
});
});
mocha.run();
Reference
이 문제에 관하여(카이사르 암호와 자바스크립트), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/anasnmu/caesar-cipher-and-javascript-1kl1텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)