@irfanshadikrishad/cipher 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -10
- package/dist/Cipher.d.ts +7 -0
- package/dist/ciphers/ADFGVX.d.ts +13 -0
- package/dist/ciphers/ADFGVX.js +89 -0
- package/dist/index.js +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
A versatile and secure cryptographic library for implementing various cipher algorithms in Node.js applications.
|
|
4
4
|
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
|
|
5
9
|
#### 🚀 Installation
|
|
6
10
|
|
|
7
11
|
Install the package via npm:
|
|
8
12
|
|
|
9
|
-
npm:
|
|
10
|
-
|
|
11
13
|
```bash
|
|
12
14
|
npm install @irfanshadikrishad/cipher
|
|
13
15
|
```
|
|
@@ -41,14 +43,15 @@ console.log(caesar.encrypt("hello world")) // Output: "nkrru cuxrj"
|
|
|
41
43
|
|
|
42
44
|
This library provides implementations of various classical and modern ciphers:
|
|
43
45
|
|
|
44
|
-
| Cipher | Type
|
|
45
|
-
| --------------------------------------------------- |
|
|
46
|
-
| [Caesar Cipher](/docs/en/ciphers/CAESAR.md) | Substitution
|
|
47
|
-
| [Atbash Cipher](/docs/en/ciphers/ATBASH.md) | Substitution
|
|
48
|
-
| [Playfair Cipher](/docs/en/ciphers/PLAYFAIR.md) | Diagraph-based
|
|
49
|
-
| [Vigenère Cipher](/docs/en/ciphers/VIGENERE.md) | Polyalphabetic
|
|
50
|
-
| [The Alphabet Cipher](/docs/en/ciphers/ALPHABET.md) | Polyalphabetic
|
|
51
|
-
| [Salsa20](/docs/en/ciphers/SALSA20.md) | Stream Cipher
|
|
46
|
+
| Cipher | Type | Key required? | Strength | Used In/Notes |
|
|
47
|
+
| --------------------------------------------------- | ---------------------------------------- | ------------- | -------- | ------------------------------------------ |
|
|
48
|
+
| [Caesar Cipher](/docs/en/ciphers/CAESAR.md) | Substitution | No | Low | Ancient Rome, Simple Obsfuscation |
|
|
49
|
+
| [Atbash Cipher](/docs/en/ciphers/ATBASH.md) | Substitution | No | Low | Hebrew Cipher, Basic Encryption |
|
|
50
|
+
| [Playfair Cipher](/docs/en/ciphers/PLAYFAIR.md) | Diagraph-based | Yes | Medium | Used in WWI & WWII |
|
|
51
|
+
| [Vigenère Cipher](/docs/en/ciphers/VIGENERE.md) | Polyalphabetic | Yes | Medium | Used in Historical Documents |
|
|
52
|
+
| [The Alphabet Cipher](/docs/en/ciphers/ALPHABET.md) | Polyalphabetic | Yes | Medium | Inspired by Vigenere, Cryptography Puzzles |
|
|
53
|
+
| [Salsa20](/docs/en/ciphers/SALSA20.md) | Stream Cipher | Yes | High | Modern Cryptography, Secure Communications |
|
|
54
|
+
| [ADFGVX](/docs/en/ciphers/ADFGVX.md) | Polybius Square + Columnar Transposition | Yes | Medium | Used in WWI, Known for 6x6 polybius square |
|
|
52
55
|
|
|
53
56
|
More ciphers coming soon...
|
|
54
57
|
|
package/dist/Cipher.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { Playfair } from "./ciphers/Playfair.js";
|
|
|
4
4
|
import { Vigenere } from "./ciphers/Vigenere.js";
|
|
5
5
|
import { Alphabet } from "./ciphers/Alphabet.js";
|
|
6
6
|
import { Salsa20 } from "./ciphers/Salsa20.js";
|
|
7
|
+
import { ADFGVX } from "./ciphers/ADFGVX.js";
|
|
7
8
|
export declare abstract class Cipher {
|
|
8
9
|
/**
|
|
9
10
|
* Caesar cipher is a substitution cipher where each letter in the plaintext is shifted a certain number of places down the alphabet.
|
|
@@ -37,6 +38,12 @@ export declare abstract class Cipher {
|
|
|
37
38
|
* @param counter 64-bit counter
|
|
38
39
|
*/
|
|
39
40
|
static Salsa20: typeof Salsa20;
|
|
41
|
+
/**
|
|
42
|
+
* The ADFGVX cipher was a manually applied field cipher used by the Imperial German Army during World War I.
|
|
43
|
+
* @param key - more than 1 characters long (more length, more secure)
|
|
44
|
+
* @param codeword - 36 characters long
|
|
45
|
+
*/
|
|
46
|
+
static ADFGVX: typeof ADFGVX;
|
|
40
47
|
abstract encrypt(text: string): string | Uint8Array;
|
|
41
48
|
abstract decrypt(text: string | Uint8Array): string;
|
|
42
49
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Cipher } from "../Cipher.js";
|
|
2
|
+
export declare class ADFGVX extends Cipher {
|
|
3
|
+
private key;
|
|
4
|
+
private codeword;
|
|
5
|
+
private polybiusSquare;
|
|
6
|
+
private reverseSquare;
|
|
7
|
+
private adfgvx;
|
|
8
|
+
constructor(key?: string | null, codeword?: string | null);
|
|
9
|
+
private createPolybiusSquare;
|
|
10
|
+
private columnarTranspose;
|
|
11
|
+
encrypt(text: string): string;
|
|
12
|
+
decrypt(text: string): string;
|
|
13
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Cipher } from "../Cipher.js";
|
|
2
|
+
export class ADFGVX extends Cipher {
|
|
3
|
+
constructor(key, codeword) {
|
|
4
|
+
super();
|
|
5
|
+
this.key = "CIPHER";
|
|
6
|
+
this.codeword = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
7
|
+
this.polybiusSquare = new Map();
|
|
8
|
+
this.reverseSquare = new Map();
|
|
9
|
+
this.adfgvx = ["A", "D", "F", "G", "V", "X"];
|
|
10
|
+
if (key)
|
|
11
|
+
this.key = key.toUpperCase();
|
|
12
|
+
if (codeword)
|
|
13
|
+
this.codeword = codeword.toUpperCase();
|
|
14
|
+
this.createPolybiusSquare(this.codeword);
|
|
15
|
+
}
|
|
16
|
+
createPolybiusSquare(codeword) {
|
|
17
|
+
const uniqueLetters = [];
|
|
18
|
+
for (const char of codeword.toUpperCase()) {
|
|
19
|
+
if (!uniqueLetters.includes(char)) {
|
|
20
|
+
uniqueLetters.push(char);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const remainingChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
24
|
+
.split("")
|
|
25
|
+
.filter((char) => !uniqueLetters.includes(char));
|
|
26
|
+
const alphabet = [...uniqueLetters, ...remainingChars];
|
|
27
|
+
let index = 0;
|
|
28
|
+
for (const row of this.adfgvx) {
|
|
29
|
+
for (const col of this.adfgvx) {
|
|
30
|
+
const letter = alphabet[index];
|
|
31
|
+
const code = row + col;
|
|
32
|
+
this.polybiusSquare.set(letter, code);
|
|
33
|
+
this.reverseSquare.set(code, letter);
|
|
34
|
+
index++;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
columnarTranspose(text, decrypt = false) {
|
|
39
|
+
const keyOrder = [...this.key]
|
|
40
|
+
.map((char, index) => ({ char, index }))
|
|
41
|
+
.sort((a, b) => a.char.localeCompare(b.char))
|
|
42
|
+
.map((item) => item.index);
|
|
43
|
+
const numCols = this.key.length;
|
|
44
|
+
const numRows = Math.ceil(text.length / numCols);
|
|
45
|
+
const grid = Array.from({ length: numRows }, () => new Array(numCols).fill(""));
|
|
46
|
+
if (!decrypt) {
|
|
47
|
+
let charIndex = 0;
|
|
48
|
+
for (let row = 0; row < numRows; row++) {
|
|
49
|
+
for (let col = 0; col < numCols; col++) {
|
|
50
|
+
if (charIndex < text.length) {
|
|
51
|
+
grid[row][col] = text[charIndex++];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return keyOrder
|
|
56
|
+
.map((col) => grid.map((row) => row[col]).join(""))
|
|
57
|
+
.join("");
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
const sortedKeyOrder = [...keyOrder].sort((a, b) => this.key[a].localeCompare(this.key[b]));
|
|
61
|
+
let charIndex = 0;
|
|
62
|
+
for (const sortedCol of sortedKeyOrder) {
|
|
63
|
+
for (let row = 0; row < numRows; row++) {
|
|
64
|
+
if (charIndex < text.length) {
|
|
65
|
+
grid[row][sortedCol] = text[charIndex++];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return grid.flat().join("");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
encrypt(text) {
|
|
73
|
+
const plaintext = text.replace(/[^A-Za-z0-9]/g, "").toUpperCase();
|
|
74
|
+
const polybiusText = plaintext
|
|
75
|
+
.split("")
|
|
76
|
+
.map((char) => this.polybiusSquare.get(char) || "")
|
|
77
|
+
.join("");
|
|
78
|
+
return this.columnarTranspose(polybiusText);
|
|
79
|
+
}
|
|
80
|
+
decrypt(text) {
|
|
81
|
+
var _a;
|
|
82
|
+
const encrypted = text.replace(/[^A-Za-z0-9]/g, "").toUpperCase();
|
|
83
|
+
const intermediate = this.columnarTranspose(encrypted, true);
|
|
84
|
+
const polybiusText = ((_a = intermediate.match(/.{1,2}/g)) === null || _a === void 0 ? void 0 : _a.map((char) => {
|
|
85
|
+
return this.reverseSquare.get(char) || "";
|
|
86
|
+
})) || [];
|
|
87
|
+
return polybiusText.join("");
|
|
88
|
+
}
|
|
89
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -5,10 +5,12 @@ import { Playfair } from "./ciphers/Playfair.js";
|
|
|
5
5
|
import { Vigenere } from "./ciphers/Vigenere.js";
|
|
6
6
|
import { Alphabet } from "./ciphers/Alphabet.js";
|
|
7
7
|
import { Salsa20 } from "./ciphers/Salsa20.js";
|
|
8
|
+
import { ADFGVX } from "./ciphers/ADFGVX.js";
|
|
8
9
|
Cipher.Caesar = Caesar;
|
|
9
10
|
Cipher.Atbash = Atbash;
|
|
10
11
|
Cipher.Playfair = Playfair;
|
|
11
12
|
Cipher.Vigenere = Vigenere;
|
|
12
13
|
Cipher.Alphabet = Alphabet;
|
|
13
14
|
Cipher.Salsa20 = Salsa20;
|
|
15
|
+
Cipher.ADFGVX = ADFGVX;
|
|
14
16
|
export { Cipher };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@irfanshadikrishad/cipher",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "A versatile and secure cryptographic library for implementing various cipher algorithms in Node.js applications.",
|
|
5
5
|
"homepage": "https://github.com/irfanshadikrishad/cipher#readme",
|
|
6
6
|
"bugs": {
|