Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.

Commit a20ed02

Browse files
Update index.html
Signed-off-by: qwerty-uiop-qaz <[email protected]>
1 parent 6a274f2 commit a20ed02

File tree

1 file changed

+73
-71
lines changed

1 file changed

+73
-71
lines changed

index.html

Lines changed: 73 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
}
2424

2525
.tab-content.active {
26-
display: block; !important
26+
display: block !important;
2727
}
2828
</style>
2929
</head>
@@ -44,7 +44,7 @@
4444
<div id="tab2" class="tab-content">
4545
Ciphertext:
4646
<textarea id="ciphertext"></textarea>
47-
<button type="button" onclick="decrypt">Decrypt</button>
47+
<button type="button" onclick="decrypt()">Decrypt</button>
4848
</div>
4949
</body>
5050
<script>
@@ -59,60 +59,66 @@
5959
selectedTab.classList.add('active');
6060
}
6161

62-
// Convert a string to BigInt
63-
function stringToBigInt(str) {
64-
// Use the UTF-16 character codes of the string, join them, and turn into BigInt
65-
let charCodes = Array.from(str).map(char => char.charCodeAt(0)).join('');
66-
return BigInt(charCodes);
67-
}
62+
// Convert a string to BigInt
63+
function stringToBigInt(str) {
64+
// Use the UTF-16 character codes of the string, join them, and turn into BigInt
65+
let charCodes = Array.from(str).map(char => char.charCodeAt(0)).join('');
66+
return BigInt(charCodes);
67+
}
6868

69-
// Convert a BigInt back to a string
70-
function bigIntToString(bigInt) {
71-
const charCodes = [];
72-
let strRepresentation = bigInt.toString();
69+
// Convert a BigInt back to a string
70+
function bigIntToString(bigInt) {
71+
const charCodes = [];
72+
let strRepresentation = bigInt.toString();
7373

74-
// Process the string representation of the BigInt as character codes in reverse
75-
while (strRepresentation.length > 0) {
74+
// Process the string representation of the BigInt as character codes in reverse
75+
while (strRepresentation.length > 0) {
7676
// Extract the last 2-3 digits (UTF-16 character code ranges from 0 to 65535)
7777
let charCode = parseInt(strRepresentation.slice(-3), 10);
7878

7979
// If the charCode is invalid, try 2 digits
8080
if (charCode > 65535) {
81-
charCode = parseInt(strRepresentation.slice(-2), 10);
82-
strRepresentation = strRepresentation.slice(0, -2);
81+
charCode = parseInt(strRepresentation.slice(-2), 10);
82+
strRepresentation = strRepresentation.slice(0, -2);
8383
} else {
84-
strRepresentation = strRepresentation.slice(0, -3);
84+
strRepresentation = strRepresentation.slice(0, -3);
8585
}
8686

8787
charCodes.push(charCode);
88+
}
89+
90+
// Reverse the charCodes and convert back to characters
91+
return charCodes.reverse().map(code => String.fromCharCode(code)).join('');
8892
}
8993

90-
// Reverse the charCodes and convert back to characters
91-
return charCodes.reverse().map(code => String.fromCharCode(code)).join('');
92-
}
93-
94-
// Function to handle Encrypt button click
94+
// Helper function to compute SHA-256 hash
95+
async function sha256(message) {
96+
const encoder = new TextEncoder();
97+
const data = encoder.encode(message);
98+
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
99+
const hashArray = Array.from(new Uint8Array(hashBuffer));
100+
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
101+
}
102+
103+
// Function to handle Encrypt button click
95104
async function encrypt() {
96-
const key = document.getElementById('key').value; // Get the encryption key
105+
const key = document.getElementById('key').value; // Get the encryption key
97106
let num = document.getElementById('num').value; // Get the message number
98107
const plaintext = document.getElementById('plaintext').value; // Get the plaintext
99108

109+
// Validate inputs
110+
if (!key || !plaintext) {
111+
alert("Please provide both an encryption key and plaintext.");
112+
return;
113+
}
114+
100115
// Set the message number to a random number if it is 0 or not set
101116
if (!num || num === "0") {
102117
num = Math.floor(Math.random() * 1000000); // Generate a random number (e.g., between 0 and 999999)
103118
document.getElementById('num').value = num; // Update the input field with the random number
104119
}
105120

106-
const bigintplaintext = stringToBigInt(plaintext)
107-
108-
// Helper function to compute SHA-256 hash
109-
async function sha256(message) {
110-
const encoder = new TextEncoder();
111-
const data = encoder.encode(message);
112-
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
113-
const hashArray = Array.from(new Uint8Array(hashBuffer));
114-
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
115-
}
121+
const bigintplaintext = stringToBigInt(plaintext);
116122

117123
// Compute hashes
118124
const keyHash = await sha256(key);
@@ -130,59 +136,55 @@
130136

131137
console.log('Final Hash:', finalHash);
132138

133-
//`finalHash` is for encryption logic
134-
135-
const bigintciphertext = bigintplaintext * finalHash;
139+
// `finalHash` is for encryption logic
140+
const bigintciphertext = bigintplaintext * BigInt('0x' + finalHash);
136141

137-
const ciphertext = bigIntToString(bigintciphertext)
142+
const ciphertext = bigIntToString(bigintciphertext);
138143

139144
document.getElementById('ciphertext').value = ciphertext;
140145

141-
showTabContent('tab2')
142-
}
143-
144-
// Function to handle Decrypt button click
145-
async function decrypt() {
146-
const key = document.getElementById('key').value; // Get the encryption key
147-
const num = document.getElementById('num').value; // Get the message number
148-
const ciphertext = document.getElementById('ciphertext').value; // Get the ciphertext
146+
showTabContent('tab2');
147+
}
149148

150-
// Helper function to compute SHA-256 hash
151-
async function sha256(message) {
152-
const encoder = new TextEncoder();
153-
const data = encoder.encode(message);
154-
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
155-
const hashArray = Array.from(new Uint8Array(hashBuffer));
156-
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
157-
}
149+
// Function to handle Decrypt button click
150+
async function decrypt() {
151+
const key = document.getElementById('key').value; // Get the encryption key
152+
const num = document.getElementById('num').value; // Get the message number
153+
const ciphertext = document.getElementById('ciphertext').value; // Get the ciphertext
158154

159-
// Compute hashes
160-
const keyHash = await sha256(key);
161-
const numHash = await sha256(num);
155+
// Validate inputs
156+
if (!key || !ciphertext) {
157+
alert("Please provide both an encryption key and ciphertext.");
158+
return;
159+
}
162160

163-
// Convert hexadecimal hash values to BigInt for multiplication
164-
const keyBigInt = BigInt('0x' + keyHash);
165-
const numBigInt = BigInt('0x' + numHash);
161+
// Compute hashes
162+
const keyHash = await sha256(key);
163+
const numHash = await sha256(num);
166164

167-
// Multiply the two hashes
168-
const multipliedHashBigInt = keyBigInt * numBigInt;
165+
// Convert hexadecimal hash values to BigInt for multiplication
166+
const keyBigInt = BigInt('0x' + keyHash);
167+
const numBigInt = BigInt('0x' + numHash);
169168

170-
// Take the hash of the result
171-
const finalHash = await sha256(multipliedHashBigInt.toString());
169+
// Multiply the two hashes
170+
const multipliedHashBigInt = keyBigInt * numBigInt;
172171

173-
console.log('Final Hash for Decryption:', finalHash);
172+
// Take the hash of the result
173+
const finalHash = await sha256(multipliedHashBigInt.toString());
174174

175-
//use `finalHash` for decryption logic
175+
console.log('Final Hash for Decryption:', finalHash);
176176

177-
const bigintciphertext = strToBigInt(ciphertext)
177+
// `finalHash` is for decryption logic
178+
const bigintciphertext = stringToBigInt(ciphertext);
178179

179-
const bigintplaintext = bigintciphertext / finalHash
180+
const bigintplaintext = bigintciphertext / BigInt('0x' + finalHash);
180181

181-
const plaintext = bigIntToString(bigintplaintext)
182+
const plaintext = bigIntToString(bigintplaintext);
182183

183-
document.getElementById('plaintext').value = plaintext
184+
document.getElementById('plaintext').value = plaintext;
184185

185-
showTabContent('tab1')
186-
}
186+
showTabContent('tab1');
187+
}
187188
</script>
188189
</html>
190+

0 commit comments

Comments
 (0)