@@ -11,24 +11,11 @@ export function encrypt(key: Uint8Array, plaintext: Uint8Array): { ciphertext: U
1111 throw new RangeError ( "Plaintext size must be 128 bits or smaller." )
1212 }
1313
14- // Ensure key size is 128 bits (16 bytes)
15- if ( key . length != BLOCK_SIZE ) {
16- throw new RangeError ( "Key size must be 128 bits." )
17- }
1814 // Generate a random value 'r' of the same length as the block size
1915 const r = forge . random . getBytesSync ( BLOCK_SIZE )
2016
21- // Create a new AES cipher using the provided key
22- const cipher = forge . cipher . createCipher ( 'AES-ECB' , forge . util . createBuffer ( key ) )
23-
24- // Encrypt the random value 'r' using AES in ECB mode
25- cipher . start ( )
26- cipher . update ( forge . util . createBuffer ( r ) )
27- cipher . finish ( )
28-
29- // Get the encrypted random value 'r' as a Buffer and ensure it's exactly 16 bytes
30- // the node-forge package produces strange results when passing strings
31- const encryptedR = encodeString ( cipher . output . data ) . slice ( 0 , BLOCK_SIZE )
17+ // Get the encrypted random value 'r'
18+ const encryptedR = encryptRandomNumber ( r , key )
3219
3320 // Pad the plaintext with zeros if it's smaller than the block size
3421 const plaintextPadded = new Uint8Array ( [ ...new Uint8Array ( BLOCK_SIZE - plaintext . length ) , ...plaintext ] )
@@ -51,27 +38,13 @@ export function decrypt(key: Uint8Array, r: Uint8Array, ciphertext: Uint8Array):
5138 throw new RangeError ( "Ciphertext size must be 128 bits." )
5239 }
5340
54- // Ensure key size is 128 bits (16 bytes)
55- if ( key . length != BLOCK_SIZE ) {
56- throw new RangeError ( "Key size must be 128 bits." )
57- }
58-
5941 // Ensure random size is 128 bits (16 bytes)
6042 if ( r . length != BLOCK_SIZE ) {
6143 throw new RangeError ( "Random size must be 128 bits." )
6244 }
6345
64- // Create a new AES decipher using the provided key
65- const cipher = forge . cipher . createCipher ( 'AES-ECB' , forge . util . createBuffer ( key ) )
66-
67- // Encrypt the random value 'r' using AES in ECB mode
68- cipher . start ( )
69- cipher . update ( forge . util . createBuffer ( r ) )
70- cipher . finish ( )
71-
72- // Get the encrypted random value 'r' as a Buffer and ensure it's exactly 16 bytes
73- // the node-forge package produces strange results when passing strings
74- const encryptedR = encodeString ( cipher . output . data )
46+ // Get the encrypted random value 'r'
47+ const encryptedR = encryptRandomNumber ( r , key )
7548
7649 // XOR the encrypted random value 'r' with the ciphertext to obtain the plaintext
7750 const plaintext = new Uint8Array ( BLOCK_SIZE )
@@ -195,10 +168,8 @@ export function decryptUint(ciphertext: bigint, userKey: string): bigint {
195168 // Convert ciphertext to Uint8Array
196169 let ctArray = new Uint8Array ( )
197170
198- let temp = new Uint8Array ( )
199-
200171 while ( ciphertext > 0 ) {
201- temp = new Uint8Array ( [ Number ( ciphertext & BigInt ( 255 ) ) ] )
172+ const temp = new Uint8Array ( [ Number ( ciphertext & BigInt ( 255 ) ) ] )
202173 ctArray = new Uint8Array ( [ ...temp , ...ctArray ] )
203174 ciphertext >>= BigInt ( 8 )
204175 }
@@ -262,9 +233,9 @@ export function encodeKey(userKey: string): Uint8Array {
262233 }
263234
264235 return keyBytes
265- }
236+ }
266237
267- export function encodeUint ( plaintext : bigint ) : Uint8Array {
238+ export function encodeUint ( plaintext : bigint ) : Uint8Array {
268239 // Convert the plaintext to bytes in little-endian format
269240
270241 const plaintextBytes = new Uint8Array ( BLOCK_SIZE ) // Allocate a buffer of size 16 bytes
@@ -275,9 +246,9 @@ export function encodeKey(userKey: string): Uint8Array {
275246 }
276247
277248 return plaintextBytes
278- }
249+ }
279250
280- export function decodeUint ( plaintextBytes : Uint8Array ) : bigint {
251+ export function decodeUint ( plaintextBytes : Uint8Array ) : bigint {
281252 const plaintext : Array < string > = [ ]
282253
283254 let byte = ''
@@ -289,4 +260,24 @@ export function encodeKey(userKey: string): Uint8Array {
289260 }
290261
291262 return BigInt ( "0x" + plaintext . join ( "" ) )
292- }
263+ }
264+
265+ function encryptRandomNumber ( r : string | Uint8Array , key : Uint8Array ) {
266+ // Ensure key size is 128 bits (16 bytes)
267+ if ( key . length != BLOCK_SIZE ) {
268+ throw new RangeError ( "Key size must be 128 bits." )
269+ }
270+
271+ // Create a new AES cipher using the provided key
272+ const cipher = forge . cipher . createCipher ( 'AES-ECB' , forge . util . createBuffer ( key ) )
273+
274+ // Encrypt the random value 'r' using AES in ECB mode
275+ cipher . start ( )
276+ cipher . update ( forge . util . createBuffer ( r ) )
277+ cipher . finish ( )
278+
279+ // Get the encrypted random value 'r' as a Buffer and ensure it's exactly 16 bytes
280+ const encryptedR = encodeString ( cipher . output . data ) . slice ( 0 , BLOCK_SIZE )
281+
282+ return encryptedR
283+ }
0 commit comments