@@ -67,42 +67,20 @@ local function decrypt(msg)
6767 return bint .upowmod (msg , d , n )
6868end
6969
70- -- Converts a buffer of bytes to a big number.
71- local function text2bn (msg )
72- local num = bint (0 )
73- local exp = bint (1 )
74- for i = 1 ,# msg do
75- num = num + string.byte (msg , i ) * exp
76- exp = exp * 256
77- end
78- return num
79- end
80-
81- -- Converts a big number into a buffer of bytes.
82- local function bn2text (num )
83- local bytes = {}
84- while num > 0 do
85- local c = string.char ((num % 256 ):tonumber ())
86- table.insert (bytes , c )
87- num = num // 256
88- end
89- return table.concat (bytes )
90- end
91-
9270-- Test encrypt and decrypt
9371print (' Message encryption test:' )
9472local msg = ' Hello world!'
9573print (' Message: ' .. msg )
96- local x = text2bn (msg , 36 )
74+ local x = bint . frombe (msg )
9775assert (x < n )
9876print (' x = 0x' .. bint .tobase (x , 16 ))
9977local c = encrypt (x )
10078print (' c = 0x' .. bint .tobase (c , 16 ))
101- assert (c == bint .frombase (' 33f037cdce6e7da0d7e652ea577b235f8a15ed63060d7e ' , 16 ))
79+ assert (c == bint .frombase (' 81fa941a0bf7a387f0ad060b90a5cd251be4031b4df39a ' , 16 ))
10280local m = decrypt (c )
10381print (' m = 0x' .. bint .tobase (m , 16 ))
10482assert (m == x )
105- local decoded = bn2text ( m )
83+ local decoded = bint . tobe ( m , true )
10684print (' Decoded: ' .. decoded )
10785assert (decoded == msg )
10886print (' success!' )
0 commit comments