Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc4035de

Browse files
added Substitution Cipher and its test
1 parent5f1ed3e commitc4035de

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

‎Ciphers/SubstitutionCipher.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Substitution Cipher
3+
*
4+
* A monoalphabetic substitution cipher replaces each letter of the plaintext
5+
* with another letter based on a fixed permutation (key) of the alphabet.
6+
* https://en.wikipedia.org/wiki/Substitution_cipher
7+
*/
8+
9+
constalphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
10+
constdefaultKey='QWERTYUIOPASDFGHJKLZXCVBNM'
11+
12+
/**
13+
* Encrypts a string using a monoalphabetic substitution cipher
14+
*@param {string} text - The text to encrypt
15+
*@param {string} key - The substitution key (must be 26 uppercase letters)
16+
*@returns {string}
17+
*/
18+
exportfunctionsubstitutionCipherEncryption(text,key=defaultKey){
19+
if(key.length!==26||!/^[A-Z]+$/.test(key)){
20+
thrownewRangeError('Key must be 26 uppercase English letters.')
21+
}
22+
23+
letresult=''
24+
consttextUpper=text.toUpperCase()
25+
for(leti=0;i<textUpper.length;i++){
26+
constchar=textUpper[i]
27+
constindex=alphabet.indexOf(char)
28+
if(index!==-1){
29+
result+=key[index]
30+
}else{
31+
result+=char
32+
}
33+
}
34+
returnresult
35+
}
36+
/**
37+
* Decrypts a string encrypted with the substitution cipher
38+
*@param {string} text - The encrypted text
39+
*@param {string} key - The substitution key used during encryption
40+
*@returns {string}
41+
*/
42+
exportfunctionsubstitutionCipherDecryption(text,key=defaultKey){
43+
if(key.length!==26||!/^[A-Z]+$/.test(key)){
44+
thrownewRangeError('Key must be 26 uppercase English letters.')
45+
}
46+
47+
letresult=''
48+
consttextUpper=text.toUpperCase()
49+
for(leti=0;i<textUpper.length;i++){
50+
constchar=textUpper[i]
51+
constindex=key.indexOf(char)
52+
if(index!==-1){
53+
result+=alphabet[index]
54+
}else{
55+
result+=char
56+
}
57+
}
58+
returnresult
59+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import{describe,it,expect}from'vitest'
2+
import{
3+
substitutionCipherEncryption,
4+
substitutionCipherDecryption
5+
}from'../SubstitutionCipher.js'
6+
7+
8+
describe('Substitution Cipher',()=>{
9+
constkey='QWERTYUIOPASDFGHJKLZXCVBNM'
10+
11+
it('correctly encrypts a message',()=>{
12+
constencrypted=substitutionCipherEncryption('HELLO WORLD',key)
13+
expect(encrypted).toBe('ITSSG VGKSR')
14+
})
15+
16+
it('correctly decrypts a message',()=>{
17+
constdecrypted=substitutionCipherDecryption('ITSSG VGKSR',key)
18+
expect(decrypted).toBe('HELLO WORLD')
19+
})
20+
21+
it('handles non-alphabetic characters',()=>{
22+
constencrypted=substitutionCipherEncryption('Test! 123',key)
23+
expect(encrypted).toBe('ZTLZ! 123')
24+
})
25+
26+
it('throws error for invalid key',()=>{
27+
expect(()=>substitutionCipherEncryption('HELLO','BADKEY')).toThrow(
28+
RangeError
29+
)
30+
})
31+
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp