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

Commit9929ab7

Browse files
add hillCipher at cryptography section (trekhleb#424)
* add hillCipher.js and its test case first commit* add README.md* update styleCo-authored-by: Oleksii Trekhleb <trehleb@gmail.com>
1 parente105460 commit9929ab7

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#Hill Cipher
2+
3+
* The Hill cipher is a polygraphic substitution cipher based on linear algebra.
4+
Each letter is represented by a number modulo 26.
5+
6+
* Encryption: to encrypt a message, each block of n letters (considered as an n-component vector) is multiplied by an invertible n × n matrix, against modulus 26.
7+
Consider the message 'ACT', and the key below (or GYB/NQK/URP in letters):
8+
| 6 24 1|
9+
| 13 16 10|
10+
| 20 17 15|
11+
The message is the vector:
12+
| 0|
13+
| 2|
14+
| 19|
15+
Thus the enciphered vector is given by
16+
| 6 24 1|| 0|| 67|| 15|
17+
| 13 16 10| | 2 | = | 222 | ≡ | 14 | (mod 26)
18+
| 20 17 15|| 19|| 319|| 7|
19+
which corresponds to a ciphertext of 'POH'.
20+
21+
* Decryption: to decrypt the message, each block is multiplied by the inverse of the matrix used for encryption.
22+
23+
24+
##Reference
25+
-[Wikipedia]https://en.wikipedia.org/wiki/Hill_cipher
26+
-[GeeksforGeeks]https://www.geeksforgeeks.org/hill-cipher/
27+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
importhillCipherEncryptfrom'../hillCipher';
2+
3+
describe('hillCipher',()=>{
4+
it('should throw an error when the length of the keyString does not equal to the power of length of the message ',()=>{
5+
constinvalidLenghOfkeyString=()=>{
6+
hillCipherEncrypt('hello','helloworld');
7+
};
8+
9+
expect(invalidLenghOfkeyString).toThrowError();
10+
});
11+
it('should throw an error when message or keyString contains none letter character',()=>{
12+
constinvalidCharacterInMessage=()=>{
13+
hillCipherEncrypt('hell3','helloworld');
14+
};
15+
constinvalidCharacterInKeyString=()=>{
16+
hillCipherEncrypt('hello','hel12world');
17+
};
18+
expect(invalidCharacterInMessage).toThrowError();
19+
expect(invalidCharacterInKeyString).toThrowError();
20+
});
21+
it('should encrypt passed message using Hill Cipher',()=>{
22+
expect(hillCipherEncrypt('ACT','GYBNQKURP')).toBe('POH');
23+
expect(hillCipherEncrypt('GFG','HILLMAGIC')).toBe('SWK');
24+
});
25+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
/**
3+
* generate key matrix from given keyString
4+
*
5+
*@param {integer} length
6+
*@param {string} keyString
7+
*@return {Array[][]} keyMatrix
8+
*/
9+
constgenerateKeyMatrix=(length,keyString)=>{
10+
constkeyMatrix=[];
11+
letkeyStringIndex=0;
12+
for(leti=0;i<length;i+=1){
13+
constkeyMatrixRow=[];
14+
for(letj=0;j<length;j+=1){
15+
keyMatrixRow.push((keyString.codePointAt(keyStringIndex))%65);
16+
keyStringIndex+=1;
17+
}
18+
keyMatrix.push(keyMatrixRow);
19+
}
20+
returnkeyMatrix;
21+
};
22+
23+
/**
24+
* generate message vector from given message
25+
*
26+
*@param {*} message
27+
*@return {Array} messageVector
28+
*/
29+
constgenerateMessageVector=(message)=>{
30+
constmessageVector=[];
31+
for(leti=0;i<message.length;i+=1){
32+
messageVector.push(message.codePointAt(i)%65);
33+
}
34+
returnmessageVector;
35+
};
36+
37+
/**
38+
* validate data and encrypt message from given message and keyString
39+
*
40+
*@param {string} message plaintext
41+
*@param {string} keyString
42+
*@return {string} cipherString
43+
*
44+
*/
45+
46+
exportdefaultfunctionhillCipherEncrypt(message,keyString){
47+
constlength=keyString.length**(0.5);
48+
// keyString.length must equal to square of message.length
49+
if(!Number.isInteger(length)&&length!==message.length){
50+
thrownewError('invalid key string length');
51+
}
52+
// keyString and messange can only contain letters
53+
if(!(/^[a-zA-Z]+$/.test(message))||!(/^[A-Za-z]+$/.test(keyString))){
54+
thrownewError('messange and key string can only contain letters');
55+
}
56+
57+
constkeyMatrix=generateKeyMatrix(length,keyString);
58+
constmessageVector=generateMessageVector(message);
59+
letciperString='';
60+
for(letrow=0;row<length;row+=1){
61+
letitem=0;
62+
for(letcolumn=0;column<length;column+=1){
63+
item+=keyMatrix[row][column]*messageVector[column];
64+
}
65+
ciperString+=String.fromCharCode((item%26)+65);
66+
}
67+
returnciperString;
68+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp