RSA.SimpleParseASN1 C# (CSharp) Method

SimpleParseASN1() public static method

public static SimpleParseASN1 ( string publicKey, byte &modulus, byte &exponent ) : void
publicKey string
modulus byte
exponent byte
return void
    public static void SimpleParseASN1(string publicKey, ref byte[] modulus, ref byte[] exponent)
    {
        byte[] publicKey64 = System.Convert.FromBase64String(publicKey);

        // The ASN1 structure for the public key looks like this:
        //
        //     SubjectPublicKeyInfo ::= SEQUENCE {
        //        algorithm AlgorithmIdentifier,
        //        publicKey BIT STRING }
        //
        // Where the BIT STRING is a SEQUENCE of 2 INTEGERs (the modulus and the exponent)

        System.Type ASN1 = System.Type.GetType("Mono.Security.ASN1");
        System.Reflection.ConstructorInfo Ctor = ASN1.GetConstructor(new System.Type[] { typeof(byte[]) });
        System.Reflection.PropertyInfo Value = ASN1.GetProperty("Value");
        System.Reflection.PropertyInfo Item = ASN1.GetProperty("Item");

        object asn = Ctor.Invoke(new object[] { publicKey64 } );
        object bits = Item.GetValue(asn, new object[] { 1 });

        byte[] value = (byte[])Value.GetValue(bits, null);

        byte[] seq = new byte[value.Length-1];
        System.Array.Copy(value, 1, seq, 0, value.Length-1);

        asn = Ctor.Invoke(new object[] { seq } );

        object asn0 = Item.GetValue(asn, new object[]{ 0 });
        object asn1 = Item.GetValue(asn, new object[]{ 1 });

        modulus = (byte[])Value.GetValue(asn0, null);
        exponent = (byte[])Value.GetValue(asn1, null);

        // non-reflected version
        //	Mono.Security.ASN1 asn = new Mono.Security.ASN1(publicKey64);
        //	Mono.Security.ASN1 bits = asn[1];
        //	byte[] seq = new byte[bits.Length-1];
        //	System.Array.Copy(bits.Value, 1, seq, 0, bits.Length-1);
        //	asn = new Mono.Security.ASN1(seq);
        //	modulus = asn[0].Value;
        //	exponent = asn[1].Value;
    }

Usage Example

Esempio n. 1
0
    void Start()
    {
        // Either parse the ASN1-formatted public LVL key at runtime (only available when stripping is disabled)..
        RSA.SimpleParseASN1(m_PublicKey_Base64, ref m_PublicKey.Modulus, ref m_PublicKey.Exponent);
        m_PublicKey_Modulus_Base64  = System.Convert.ToBase64String(m_PublicKey.Modulus);
        m_PublicKey_Exponent_Base64 = System.Convert.ToBase64String(m_PublicKey.Exponent);
        // .. and check the logcat for these values ...
        Debug.Log("private string m_PublicKey_Modulus_Base64 = \"" + m_PublicKey_Modulus_Base64 + "\";");
        Debug.Log("private string m_PublicKey_Exponent_Base64 = \"" + m_PublicKey_Exponent_Base64 + "\";");

        // .. or use pre-parsed keys (and remove the code above).
        m_PublicKey.Modulus  = System.Convert.FromBase64String(m_PublicKey_Modulus_Base64);
        m_PublicKey.Exponent = System.Convert.FromBase64String(m_PublicKey_Exponent_Base64);

        m_RunningOnAndroid = new AndroidJavaClass("android.os.Build").GetRawClass() != System.IntPtr.Zero;
        if (!m_RunningOnAndroid)
        {
            return;
        }

        LoadServiceBinder();

        new SHA1CryptoServiceProvider();                // keep a dummy reference to prevent too aggressive stripping

        m_ButtonMessage = "Check LVL";
    }
All Usage Examples Of RSA::SimpleParseASN1