Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

pem file string reader c#

//------- Parses binary ans.1 RSA private key; returns RSACryptoServiceProvider  ---
public static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey)
{
        byte[] MODULUS, E, D, P, Q, DP, DQ, IQ ;

        // ---------  Set up stream to decode the asn.1 encoded RSA private key  ------
        MemoryStream  mem = new MemoryStream(privkey) ;
        BinaryReader binr = new BinaryReader(mem) ;    //wrap Memory Stream with BinaryReader for easy reading
        byte bt = 0;
        ushort twobytes = 0;
        int elems = 0;
        try {
                twobytes = binr.ReadUInt16();
                if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
                        binr.ReadByte();        //advance 1 byte
                else if (twobytes == 0x8230)
                        binr.ReadInt16();       //advance 2 bytes
                else
                        return null;

                twobytes = binr.ReadUInt16();
                if (twobytes != 0x0102) //version number
                        return null;
                bt = binr.ReadByte();
                if (bt !=0x00)
                        return null;


                //------  all private key components are Integer sequences ----
                elems = GetIntegerSize(binr);
                MODULUS = binr.ReadBytes(elems);

                elems = GetIntegerSize(binr);
                E = binr.ReadBytes(elems) ;

                elems = GetIntegerSize(binr);
                D = binr.ReadBytes(elems) ;

                elems = GetIntegerSize(binr);
                P = binr.ReadBytes(elems) ;

                elems = GetIntegerSize(binr);
                Q = binr.ReadBytes(elems) ;

                elems = GetIntegerSize(binr);
                DP = binr.ReadBytes(elems) ;

                elems = GetIntegerSize(binr);
                DQ = binr.ReadBytes(elems) ;

                elems = GetIntegerSize(binr);
                IQ = binr.ReadBytes(elems) ;

                Console.WriteLine("showing components ..");
                if (verbose) {
                        showBytes("
Modulus", MODULUS) ;
                        showBytes("
Exponent", E);
                        showBytes("
D", D);
                        showBytes("
P", P);
                        showBytes("
Q", Q);
                        showBytes("
DP", DP);
                        showBytes("
DQ", DQ);
                        showBytes("
IQ", IQ);
                }

                // ------- create RSACryptoServiceProvider instance and initialize with public key -----
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                RSAParameters RSAparams = new RSAParameters();
                RSAparams.Modulus =MODULUS;
                RSAparams.Exponent = E;
                RSAparams.D = D;
                RSAparams.P = P;
                RSAparams.Q = Q;
                RSAparams.DP = DP;
                RSAparams.DQ = DQ;
                RSAparams.InverseQ = IQ;
                RSA.ImportParameters(RSAparams);
                return RSA;
        }
        catch (Exception) {
                return null;
        }
        finally {
                binr.Close();
        }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: hdrp lit change emmision values 
Csharp :: c# SQLite execute Command 
Csharp :: how to get c# code of aspx page 
Csharp :: unity save slots 
Csharp :: create circumference with nettopologysuite 
Csharp :: visual studio auto generate and setters 
Csharp :: asp.net unregister client script 
Csharp :: windows forms webbrowser goback 
Csharp :: advance C# tricks and hits 
Csharp :: save a string as file to drive appscript 
Csharp :: c# check if there is 5 mins space between two datestime 
Csharp :: isdaylightsavingtime in c# 
Csharp :: Write N lines with M numbers each that describe the layout of the second layer in the way shown above 
Csharp :: RemoveClaim 
Csharp :: EF will not create columns RULE 
Csharp :: panning script c# on phone 
Csharp :: unity only one component type 
Csharp :: calculate 01 with min max value 
Csharp :: cannot access file being used by another process create file 
Csharp :: Difference between Math.Floor() and Math.Truncate() 
Csharp :: .net framework cheat sheet 
Csharp :: unity inspector sliders 
Csharp :: c# only only 2 digits after decimal number double 
Csharp :: protected override void OnExiting(Object sender, EventArgs args) { base.OnExiting(sender, args); Environment.Exit(Environment.ExitCode); } 
Csharp :: how to get the index of an element in a list in unity 5 
Csharp :: do loop c# 
Csharp :: c# MD5.Create returning nul 
Csharp :: generate random string 
Html :: html disable editing textbox 
Html :: html link to call phone number 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =