|
23 | 23 | } |
24 | 24 |
|
25 | 25 | .tab-content.active { |
26 | | - display: block; !important |
| 26 | + display: block !important; |
27 | 27 | } |
28 | 28 | </style> |
29 | 29 | </head> |
|
44 | 44 | <div id="tab2" class="tab-content"> |
45 | 45 | Ciphertext: |
46 | 46 | <textarea id="ciphertext"></textarea> |
47 | | - <button type="button" onclick="decrypt">Decrypt</button> |
| 47 | + <button type="button" onclick="decrypt()">Decrypt</button> |
48 | 48 | </div> |
49 | 49 | </body> |
50 | 50 | <script> |
|
59 | 59 | selectedTab.classList.add('active'); |
60 | 60 | } |
61 | 61 |
|
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 | + } |
68 | 68 |
|
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(); |
73 | 73 |
|
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) { |
76 | 76 | // Extract the last 2-3 digits (UTF-16 character code ranges from 0 to 65535) |
77 | 77 | let charCode = parseInt(strRepresentation.slice(-3), 10); |
78 | 78 |
|
79 | 79 | // If the charCode is invalid, try 2 digits |
80 | 80 | 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); |
83 | 83 | } else { |
84 | | - strRepresentation = strRepresentation.slice(0, -3); |
| 84 | + strRepresentation = strRepresentation.slice(0, -3); |
85 | 85 | } |
86 | 86 |
|
87 | 87 | charCodes.push(charCode); |
| 88 | + } |
| 89 | + |
| 90 | + // Reverse the charCodes and convert back to characters |
| 91 | + return charCodes.reverse().map(code => String.fromCharCode(code)).join(''); |
88 | 92 | } |
89 | 93 |
|
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 |
95 | 104 | 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 |
97 | 106 | let num = document.getElementById('num').value; // Get the message number |
98 | 107 | const plaintext = document.getElementById('plaintext').value; // Get the plaintext |
99 | 108 |
|
| 109 | + // Validate inputs |
| 110 | + if (!key || !plaintext) { |
| 111 | + alert("Please provide both an encryption key and plaintext."); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
100 | 115 | // Set the message number to a random number if it is 0 or not set |
101 | 116 | if (!num || num === "0") { |
102 | 117 | num = Math.floor(Math.random() * 1000000); // Generate a random number (e.g., between 0 and 999999) |
103 | 118 | document.getElementById('num').value = num; // Update the input field with the random number |
104 | 119 | } |
105 | 120 |
|
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); |
116 | 122 |
|
117 | 123 | // Compute hashes |
118 | 124 | const keyHash = await sha256(key); |
|
130 | 136 |
|
131 | 137 | console.log('Final Hash:', finalHash); |
132 | 138 |
|
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); |
136 | 141 |
|
137 | | - const ciphertext = bigIntToString(bigintciphertext) |
| 142 | + const ciphertext = bigIntToString(bigintciphertext); |
138 | 143 |
|
139 | 144 | document.getElementById('ciphertext').value = ciphertext; |
140 | 145 |
|
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 | + } |
149 | 148 |
|
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 |
158 | 154 |
|
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 | + } |
162 | 160 |
|
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); |
166 | 164 |
|
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); |
169 | 168 |
|
170 | | - // Take the hash of the result |
171 | | - const finalHash = await sha256(multipliedHashBigInt.toString()); |
| 169 | + // Multiply the two hashes |
| 170 | + const multipliedHashBigInt = keyBigInt * numBigInt; |
172 | 171 |
|
173 | | - console.log('Final Hash for Decryption:', finalHash); |
| 172 | + // Take the hash of the result |
| 173 | + const finalHash = await sha256(multipliedHashBigInt.toString()); |
174 | 174 |
|
175 | | - //use `finalHash` for decryption logic |
| 175 | + console.log('Final Hash for Decryption:', finalHash); |
176 | 176 |
|
177 | | - const bigintciphertext = strToBigInt(ciphertext) |
| 177 | + // `finalHash` is for decryption logic |
| 178 | + const bigintciphertext = stringToBigInt(ciphertext); |
178 | 179 |
|
179 | | - const bigintplaintext = bigintciphertext / finalHash |
| 180 | + const bigintplaintext = bigintciphertext / BigInt('0x' + finalHash); |
180 | 181 |
|
181 | | - const plaintext = bigIntToString(bigintplaintext) |
| 182 | + const plaintext = bigIntToString(bigintplaintext); |
182 | 183 |
|
183 | | - document.getElementById('plaintext').value = plaintext |
| 184 | + document.getElementById('plaintext').value = plaintext; |
184 | 185 |
|
185 | | - showTabContent('tab1') |
186 | | -} |
| 186 | + showTabContent('tab1'); |
| 187 | + } |
187 | 188 | </script> |
188 | 189 | </html> |
| 190 | + |
0 commit comments