0

This is my first post on Stackoverflow so i will appreciate any suggestions on how to improve.

I tried the following from here So im trying to encode a string message, let it be "test", with the Microsoft RSACryptoServiceProvider with the help of BouncyCastle.

I've got a given public key where the server provider already described me how the public key is structured.

public key: 48015250010000010100970348B03E911DCCE5ED8F555C2116DBC4D7E96D4C1CDC4BBBAAD26BAA54B5C834F604F9DFB391459459772FB51D00AFD0FE3A9B2DA724E62113A9E8C95BEF377CB5FCF7FEBE42E5282A0DA50F01D5D2635DD958F9836CFB4F8B616777C0CF67DB9A5530AD679E321972E4D4F4F33DED057CB690417A3B42FBFCE2AD9FDD80C815AF1EC858C796D4EA2F17954E4BFAD08E3E0397FA34122AC5951D889B06359A401E5506E50FA176B5A77FAB84E25CFCDBF2330AA173DA1156C8B79D6DB6BFAE828B00811183E63F137648E1FC1786B52D815C248BCADDDF6A17C941414F67A23ADFE82FE76196B64B96E36F8604FA00E8E357F5AE6C83B992D622D5E9CD9C1D00000003010001

The public key is a hexstring and i extracted the modulus and exponent in seperate variables and saved them as Base64 Strings. The modulus and exponent are saved in another variable as a xml string for use as the RSAKeyValue.

string strModulusAndExponentAsXml = "<RSAKeyValue><Modulus>00970348B03E911DCCE5ED8F555C2116DBC4D7E96D4C1CDC4BBBAAD26BAA54B5C834F604F9DFB391459459772FB51D00AFD0FE3A9B2DA724E62113A9E8C95BEF377CB5FCF7FEBE42E5282A0DA50F01D5D2635DD958F9836CFB4F8B616777C0CF67DB9A5530AD679E321972E4D4F4F33DED057CB690417A3B42FBFCE2AD9FDD80C815AF1EC858C796D4EA2F17954E4BFAD08E3E0397FA34122AC5951D889B06359A401E5506E50FA176B5A77FAB84E25CFCDBF2330AA173DA1156C8B79D6DB6BFAE828B00811183E63F137648E1FC1786B52D815C248BCADDDF6A17C941414F67A23ADFE82FE76196B64B96E36F8604FA00E8E357F5AE6C83B992D622D5E9CD9C1D</Modulus><Exponent>010001</Exponent></RSAKeyValue>";
  string strModulusAndExponentAsBase64 = Base64Encode(strModulusAndExponentAsXml);

Now i want to create a Asn1Object and provide the byte parameter

Asn1Object obj = Asn1Object.FromByteArray(Convert.FromBase64String(strModulusAndExponentAsBase64));

and here it fails, i get the error:

System.IO.IOException: 'unknown tag 28 encountered'

Does anyone know what im doing wrong, so i can create the Asn1Ojbect with the given exponent and modulus? Let me know if anything is unclear.

drecunion
  • 121
  • 8
  • One red flag to me is that you aren't using the conversion functions from the same namespace. `Convert.FromBase64String` is a .NET function, but which namespace does `Base64Encode` come from? – Neil Jan 21 '19 at 16:02
  • It's very simple really. Your data bears absolutely no relation to any ASN1 object. It is XML, and there is no point at all in base64 encoding it. Instead, use [`RSA.FromXMLString()`](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsa.fromxmlstring?view=netframework-4.7.1#System_Security_Cryptography_RSA_FromXmlString_System_String_) – President James K. Polk Jan 21 '19 at 18:26
  • okay thank you both, i got the idea. i used way to many different types and even created a new class for encoding purposes. James was right that i only need the RSA.FromXmlString(). In my first post i had made mistakes with the conversion fix below: After all i needed to decode my hex string as a byte array and then convert it to a Base64 Type and then i put the converted modulus and exponent in my variable with the xml public key and it works now. For anyone with the same problem you can find the methods to convert the hexstring here: https://stackoverflow.com/a/46327314/10945532 – drecunion Jan 28 '19 at 13:28

1 Answers1

-2

Don't put it on public if you want to check your crypto. You should try a different method:

if (algValue != null)
    {
        algValue.Clear();
    }
    else
    {
        throw new Exception("No TripleDES key was found to clear.");
    }
}

public void Encrypt(string Element)
{
    // Find the element by name and create a new
    // XmlElement object.
    XmlElement inputElement = docValue.GetElementsByTagName(Element)[0] as XmlElement;

    // If the element was not found, throw an exception.
    if (inputElement == null)
    {
        throw new Exception("The element was not found.");
    }

    // Create a new EncryptedXml object.
    EncryptedXml exml = new EncryptedXml(docValue);

    // Encrypt the element using the symmetric key.
    byte[] rgbOutput = exml.EncryptData(inputElement, algValue, false);

this is the method you should try.