BigInteger.Parse () a problem of reading in large quantities

I'm currently trying to accomplish this task ( http://cryptopals.com/sets/1/challenges/1 ), and I am having problems completing a task in C #. I can not make out the number in a large integer.

So, the code is as follows:

        string output = "";
        BigInteger hexValue = BigInteger.Parse("49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6");

        output = Convert.ToBase64String(hexValue.ToByteArray());
        Console.WriteLine(hexValue);
        Console.WriteLine(output);
        Console.ReadKey();
        return "";

And currently the problem that I get is when I run the program with an error

System.FormatException: "Value cannot be parsed." and I'm not quite sure why.

So what is the appropriate way to get a large integer from a string in BigInt?

+4
source share
3 answers

Initial task

BigInteger.Parse , , . "" , NumberStyles.HexNumber.

BigInteger

, BigInteger. -, , , . . ( : "0001" - , , hex.)

- , byte[], BigInteger.ToByteArray(), . , , byte[] hex BitConverter:

BigInteger bigInt = BigInteger.Parse("1234567890ABCDEF", NumberStyles.HexNumber);
byte[] bytes = bigInt.ToByteArray();
Console.WriteLine(BitConverter.ToString(bytes));

"EF-CD-AB-90-78-56-34-12" - BigInteger.ToByteArray little-endian:

, , . .

, - , ..

BigInteger

, , , . , , , .

, , - . , base64, .

, :

  • String (hex) BigInteger: lossy ( 0s , )
  • BigInteger to byte[]: not lossy
  • byte[] to String (base64): not lossy

:

  • String (hex) byte[]: ( nybbles , )
  • byte[] to String (base64): not lossy
+6

NumberStyles.HexNumber:

BigInteger.Parse("49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6", 
                 NumberStyles.HexNumber,
                 CultureInfo.InvariantCulture);

, .

+4

The problem is that the input is not decimal, but hexadecimal, so you need to pass an additional parameter for parsing:

BigInteger number = BigInteger.Parse(
            hexString,
            NumberStyles.AllowHexSpecifier);
+3
source

Source: https://habr.com/ru/post/1695939/


All Articles