-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_encrypt.ts
72 lines (61 loc) · 2.3 KB
/
sample_encrypt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// This is a simple encryption algorithm that uses a
// key and a mathematical operation to encrypt and
// decrypt data.
// Choose a mathematical operation to use as the core
// of the encryption algorithm. In this case, we will
// use addition.
const operation = (x, y) => x + y;
// Generate a random key to use for encrypting and
// decrypting data. The key should be long and
// random to make it difficult for an attacker to
// guess or reverse engineer.
const key = Math.random().toString(36).substring(2);
// Define a function for encrypting data. This
// function takes the data to be encrypted and
// the key as inputs, and returns the encrypted
// data as output.
const encrypt = (data, key) => {
// Convert the data to a string to make it
// easier to encrypt.
const dataString = data.toString();
// Initialize an empty array to store the
// encrypted data.
const encryptedData = [];
// Loop through the data string and encrypt
// each character using the key and the
// chosen mathematical operation.
for (let i = 0; i < dataString.length; i++) {
const charCode = dataString.charCodeAt(i);
const encryptedCharCode = operation(charCode, key);
encryptedData.push(encryptedCharCode);
}
// Return the encrypted data as output.
return encryptedData;
};
// Define a function for decrypting data. This
// function takes the encrypted data and the
// key as inputs, and returns the decrypted
// data as output.
const decrypt = (encryptedData, key) => {
// Initialize an empty string to store the
// decrypted data.
let decryptedData = "";
// Loop through the encrypted data and
// decrypt each character using the key
// and the chosen mathematical operation.
for (let i = 0; i < encryptedData.length; i++) {
const encryptedCharCode = encryptedData[i];
const decryptedCharCode = operation(encryptedCharCode, -key);
decryptedData += String.fromCharCode(decryptedCharCode);
}
// Return the decrypted data as output.
return decryptedData;
};
// Test the encryption and decryption functions
// using some sample data.
const data = "Hello, world!";
const encryptedData = encrypt(data, key);
const decryptedData = decrypt(encryptedData, key);
console.log("Original data:", data);
console.log("Encrypted data:", encryptedData);
console.log("Decrypted data:", decryptedData);