cryptography - How does this message splitting work? -
i have been trying reverse engineer various encryption algorithms in compiled code recently, , came upon code. part of rsa algorithm. i've noted key size small encrypt/decrypt data it's supposed (in case int
), code splits message 2 pieces, , encrypt/decrypt each, sum them together. i've pulled segments of code splits , joins message, , experimented it. appears numerical values uses dependent on n
modulus. so, scheme, , how work?
uint n = 32437; uint origval = 12345; uint newval = 0; (int = 0; < 2; ++i) { ulong num = (ulong)origval * 43827549; //uint num2 = ((origval - (uint)(num >> 32)) / 2 + (uint)(num >> 32)) >> 14; uint num2 = (origval + (uint)(num >> 32)) / 32768; origval -= num2 * n; // rsa encrypt/decrypt here newval *= n; newval += origval; origval = num2; } // put newval origval, reverse origval = newval; newval = 0; (int = 0; < 2; ++i) { ulong num = (ulong)origval * 43827549; //uint num2 = ((origval - (uint)(num >> 32)) / 2 + (uint)(num >> 32)) >> 14; uint num2 = (origval + (uint)(num >> 32)) / 32768; origval -= num2 * n; // rsa encrypt/decrypt here newval *= n; newval += origval; origval = num2; }
note: seems operations applied symmetric.
after using various values origval
, i've found out first 3 lines after for
loop division, line after modulo operation. lines
ulong num = (ulong)origval * 43827549; //uint num2 = ((origval - (uint)(num >> 32)) / 2 + (uint)(num >> 32)) >> 14; uint num2 = (origval + (uint)(num >> 32)) / 32768;
translates into
uint valdivn = origval / n;
and
origval -= num2 * n;
into
origval = origval % n;
final code inside for
loop looks this:
uint valdivn = origval / n; origval = origval % n; // rsa encrypt/decrypt here newval*= n; newval+= origval; origval = valdivn;
analysis
this code splits values taking modulo of original value, transforming it, multiplying n
, , tacking transformation of previous quotient onto result. lines uint valdivn = origval / n;
, newval*= n;
form inverse operations. can think of input message having 2 "boxes". after loop has run through, transformed value put in opposite "boxes". when message decrypted, 2 values in "boxes" reverse transformed, , put in original spots in "boxes". reason divisor n
keep value being encrypted/decrypted under n
, maximum value can encrypt rsa no larger n
. there no possibility of wrong value being decrypted, code processes packed message , extracts part should decrypted prior decrypting. loop runs twice because there no chance quotient exceed size of int
(since input int
).
Comments
Post a Comment