
januszmk
Novice
Apr 24, 2012, 2:05 PM
Post #1 of 8
(3860 views)
|
Hi. I have some code in C# and I am trying convert to perl. The code:
BigInteger exp = new BigInteger("SOME BIG NUMBER", 10); BigInteger n = new BigInteger("OTHER BIG NUMBER", 10); BigInteger integer3 = new BigInteger(str.Substring(1), 0x24); byte[] bytes = integer3.modPow(exp, n).getBytes(); ASCIIEncoding encoding = new ASCIIEncoding(); return new string(encoding.GetChars(bytes)); the second argument of constructor in BigInteger class is a base system. the str is the string , something like:
_f4eoy18ien92q667yex54u6xqyd2u6e2j0kjkvtesj4gcbkij6yq93ms7kx4yzxx0gdmj9vrhk7yns371ormeg5j44jkm727tg3 anyway, the final string that is return is "patyczkowona1". I tried to do this in perl, with using Math::BigInt;. I found some functions in net that convert an byte array to BigInt and BigInt to array:
# Calculate the bit-width of cells on this system. sub count_bits { my $i = 0; my $q = ~0x0; # Set all bits while ( $q ) { # All shifted out? $q = $q << 1; # Shift left $i++; # Accum shifts } return $i; # Return bit-width } my $bits = count_bits; # System bit-width. my $cells = int( $bits / 8 ); # max bytes in array my $mask = ~0x0; # Full mask = 0xF* my $top_bit = ~( $mask >> 1 ); # High mask = 0x80* ########################## # END N-BIT SYSTEM CHECK # ########################## # Convert a BigInt to an array. sub bint_to_array { my $quo = shift; my $msk = Math::BigInt->new($mask); my @cls; until ( $quo->is_zero() ) { my $rem = $quo->copy(); # Lest modify ruin. $rem->band($msk); # REM = LSC $quo->brsft($bits); # QUO = Cells above unshift @cls, $rem->bstr(); # Keep remainder } return @cls; } # Convert an array to a BigInt. sub array_to_bint { my @cls = @_; my $out = Math::BigInt->new(0); my $i = 0; while ( @_ ) { my $lsc = Math::BigInt->new( pop @_ ); # LSC $lsc->blsft($bits * $i); # Shift up $out->bior($lsc); # OR to LSC ++$i; } return $out; } First, I am converting my string to ascii table, next, this ascii table to BigInt, then I am using bmodpow() function and convert result to array:
my $string = "f4eoy18ien92q667yex54u6xqyd2u6e2j0kjkvtesj4gcbkij6yq93ms7kx4yzxx0gdmj9vrhk7yns371ormeg5j44jkm727tg3"; my @ascii = unpack("C*", $string); my $x = array_to_bint(@ascii); my $exp = "1699226779513586444311853417832813053444275415713267420973354241696037822738978485422119265005494628039077923965408489035118957525416966246759228390003017"; my $n = "8496133897567932221559267089164065267221377078566337104866771208480189113695076911576868324903413008512741498700587851315525414979963642608998181269527941"; $x->bmodpow($exp,$n); my @a = bint_to_array ($x); my $z; foreach $z (@a) { print "arg: $z\n"; } But, the results is not like supposed to be:
arg: 8660100782654598831 arg: 15621194053667428015 arg: 13895005360354588697 arg: 7950417416029500606 arg: 12330079340979623579 arg: 145443755943421736 arg: 8563019593306281932 arg: 2707167814207797994 every number supposed to be a ascii number. I tried to convert every argument of @ascii to base36 (encode_base36() in Math::Base36), but then when I do "my $x = array_to_bint(@ascii);" then $x return "NaN" Does anyone have any idea what am I doing wrong? Thanks in advance
|