@irfanshadikrishad/cipher 1.0.5 → 1.0.7

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 CHANGED
@@ -43,15 +43,17 @@ console.log(caesar.encrypt("hello world")) // Output: "nkrru cuxrj"
43
43
 
44
44
  This library provides implementations of various classical and modern ciphers:
45
45
 
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 |
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 |
55
+ | [AES](/docs/en/ciphers/AES.md) | Symmetric Block Cipher | Yes | High | Also known as, Rijndael |
56
+ | [DES](/docs/en/ciphers/DES.md) | Symmetric Block Cipher | Yes | Medium | 56-bit key, Used in legacy systems, replaced by AES |
55
57
 
56
58
  More ciphers coming soon...
57
59
 
package/dist/Cipher.d.ts CHANGED
@@ -5,6 +5,8 @@ import { Vigenere } from "./ciphers/Vigenere.js";
5
5
  import { Alphabet } from "./ciphers/Alphabet.js";
6
6
  import { Salsa20 } from "./ciphers/Salsa20.js";
7
7
  import { ADFGVX } from "./ciphers/ADFGVX.js";
8
+ import { AES } from "./ciphers/AES.js";
9
+ import { DES } from "./ciphers/DES.js";
8
10
  export declare abstract class Cipher {
9
11
  /**
10
12
  * Caesar cipher is a substitution cipher where each letter in the plaintext is shifted a certain number of places down the alphabet.
@@ -44,6 +46,18 @@ export declare abstract class Cipher {
44
46
  * @param codeword - 36 characters long
45
47
  */
46
48
  static ADFGVX: typeof ADFGVX;
47
- abstract encrypt(text: string): string | Uint8Array;
48
- abstract decrypt(text: string | Uint8Array): string;
49
+ /**
50
+ * Advanced Encryption Standard (AES) is a symmetric encryption algorithm.
51
+ * @param key 256-bit (32 byte) key
52
+ * @param iv 128-bit (16 byte) initialization vector
53
+ */
54
+ static AES: typeof AES;
55
+ /**
56
+ * Data Encryption Standard (DES) is a symmetric encryption algorithm.
57
+ * @param key 8 characters long
58
+ * @param iv 8 characters long
59
+ */
60
+ static DES: typeof DES;
61
+ abstract encrypt(text: string): string;
62
+ abstract decrypt(text: string): string;
49
63
  }
@@ -0,0 +1,8 @@
1
+ import { Cipher } from "../Cipher.js";
2
+ export declare class AES extends Cipher {
3
+ private key;
4
+ private iv;
5
+ constructor(key: string, iv: string);
6
+ encrypt(plaintext: string): string;
7
+ decrypt(ciphertext: string): string;
8
+ }
@@ -0,0 +1,28 @@
1
+ import crypto from "crypto";
2
+ import { Cipher } from "../Cipher.js";
3
+ import { Buffer } from "buffer";
4
+ export class AES extends Cipher {
5
+ constructor(key, iv) {
6
+ super();
7
+ this.key = Buffer.from(key, "hex");
8
+ this.iv = Buffer.from(iv, "hex");
9
+ if (this.key.length !== 32) {
10
+ throw new Error("Invalid key length: AES-256 requires a 32-byte key");
11
+ }
12
+ if (this.iv.length !== 16) {
13
+ throw new Error("Invalid IV length: AES requires a 16-byte IV");
14
+ }
15
+ }
16
+ encrypt(plaintext) {
17
+ const cipher = crypto.createCipheriv("aes-256-cbc", this.key, this.iv);
18
+ let encrypted = cipher.update(plaintext, "utf8", "hex");
19
+ encrypted += cipher.final("hex");
20
+ return encrypted;
21
+ }
22
+ decrypt(ciphertext) {
23
+ const decipher = crypto.createDecipheriv("aes-256-cbc", this.key, this.iv);
24
+ let decrypted = decipher.update(ciphertext, "hex", "utf8");
25
+ decrypted += decipher.final("utf8");
26
+ return decrypted;
27
+ }
28
+ }
@@ -0,0 +1,15 @@
1
+ import { Cipher } from "../Cipher.js";
2
+ export declare class DES extends Cipher {
3
+ private key;
4
+ constructor(key: string);
5
+ encrypt(text: string): string;
6
+ decrypt(text: string): string;
7
+ private pad;
8
+ private unpad;
9
+ private feistel;
10
+ private getRoundKey;
11
+ private f;
12
+ private permute;
13
+ private stringToBigInt;
14
+ private bigIntToString;
15
+ }
@@ -0,0 +1,87 @@
1
+ import { Cipher } from "../Cipher.js";
2
+ import { Buffer } from "buffer";
3
+ const IP = [
4
+ // Initial Permutation
5
+ 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38,
6
+ 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1,
7
+ 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39,
8
+ 31, 23, 15, 7,
9
+ ];
10
+ const FP = [
11
+ // Final Permutation
12
+ 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14,
13
+ 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28,
14
+ 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9,
15
+ 49, 17, 57, 25,
16
+ ];
17
+ export class DES extends Cipher {
18
+ constructor(key) {
19
+ super();
20
+ if (key.length !== 8)
21
+ throw new Error("DES key must be exactly 8 characters.");
22
+ this.key = this.stringToBigInt(key);
23
+ }
24
+ encrypt(text) {
25
+ const paddedText = this.pad(text);
26
+ const encryptedBlocks = [];
27
+ for (let i = 0; i < paddedText.length; i += 8) {
28
+ const block = paddedText.substring(i, i + 8);
29
+ const input = this.stringToBigInt(block);
30
+ const permuted = this.permute(input, IP);
31
+ const encrypted = this.feistel(permuted);
32
+ const finalPerm = this.permute(encrypted, FP);
33
+ encryptedBlocks.push(finalPerm.toString(16).padStart(16, "0"));
34
+ }
35
+ return encryptedBlocks.join(""); // Concatenate hex strings
36
+ }
37
+ decrypt(text) {
38
+ let decryptedText = "";
39
+ for (let i = 0; i < text.length; i += 16) {
40
+ // 16 hex chars = 8 bytes
41
+ const block = BigInt("0x" + text.substring(i, i + 16));
42
+ const permuted = this.permute(block, IP);
43
+ const decrypted = this.feistel(permuted);
44
+ const finalPerm = this.permute(decrypted, FP);
45
+ decryptedText += this.bigIntToString(finalPerm);
46
+ }
47
+ return this.unpad(decryptedText);
48
+ }
49
+ pad(text) {
50
+ const padLength = 8 - (text.length % 8);
51
+ return text + String.fromCharCode(padLength).repeat(padLength);
52
+ }
53
+ unpad(text) {
54
+ const padLength = text.charCodeAt(text.length - 1);
55
+ return text.slice(0, -padLength);
56
+ }
57
+ feistel(input) {
58
+ let L = input >> BigInt(32);
59
+ let R = input & BigInt(0xffffffff);
60
+ for (let i = 0; i < 16; i++) {
61
+ const roundKey = this.getRoundKey();
62
+ const newR = L ^ this.f(R, roundKey);
63
+ L = R;
64
+ R = newR;
65
+ }
66
+ return (R << BigInt(32)) | L;
67
+ }
68
+ getRoundKey() {
69
+ return this.key; // Placeholder (Key Schedule logic needed)
70
+ }
71
+ f(block, roundKey) {
72
+ return block ^ roundKey; // Placeholder (Expand, S-Box, Permutation)
73
+ }
74
+ permute(input, table) {
75
+ let output = BigInt(0);
76
+ for (let i = 0; i < table.length; i++) {
77
+ output |= ((input >> BigInt(64 - table[i])) & BigInt(1)) << BigInt(63 - i);
78
+ }
79
+ return output;
80
+ }
81
+ stringToBigInt(str) {
82
+ return BigInt("0x" + Buffer.from(str, "utf8").toString("hex"));
83
+ }
84
+ bigIntToString(num) {
85
+ return Buffer.from(num.toString(16).padStart(16, "0"), "hex").toString("utf8");
86
+ }
87
+ }
package/dist/index.js CHANGED
@@ -6,6 +6,8 @@ import { Vigenere } from "./ciphers/Vigenere.js";
6
6
  import { Alphabet } from "./ciphers/Alphabet.js";
7
7
  import { Salsa20 } from "./ciphers/Salsa20.js";
8
8
  import { ADFGVX } from "./ciphers/ADFGVX.js";
9
+ import { AES } from "./ciphers/AES.js";
10
+ import { DES } from "./ciphers/DES.js";
9
11
  Cipher.Caesar = Caesar;
10
12
  Cipher.Atbash = Atbash;
11
13
  Cipher.Playfair = Playfair;
@@ -13,4 +15,6 @@ Cipher.Vigenere = Vigenere;
13
15
  Cipher.Alphabet = Alphabet;
14
16
  Cipher.Salsa20 = Salsa20;
15
17
  Cipher.ADFGVX = ADFGVX;
18
+ Cipher.AES = AES;
19
+ Cipher.DES = DES;
16
20
  export { Cipher };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@irfanshadikrishad/cipher",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
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": {