美文网首页
keycloak的私钥解开

keycloak的私钥解开

作者: 价值投机168 | 来源:发表于2022-10-26 09:50 被阅读0次
     public sealed class CryptoHelper
    {
    private static int GetIntegerSize(BinaryReader binr)
    {
        byte bt = 0;
        byte lowbyte = 0x00;
        byte highbyte = 0x00;
        int count = 0;
        bt = binr.ReadByte();
        if (bt != 0x02)        //expect integer
            return 0;
        bt = binr.ReadByte();
    
    
        if (bt == 0x81)
            count = binr.ReadByte();    // data size in next byte
        else
            if (bt == 0x82)
        {
            highbyte = binr.ReadByte();    // data size in next 2 bytes
            lowbyte = binr.ReadByte();
            byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
            count = BitConverter.ToInt32(modint, 0);
        }
        else
        {
            count = bt;        // we already have the data size
        }
    
    
        while (binr.ReadByte() == 0x00)
        {    //remove high order zeros in data
            count -= 1;
        }
        binr.BaseStream.Seek(-1, SeekOrigin.Current);        //last ReadByte wasn't a removed zero, so back up a byte
        return count;
    }
    
    
    public static RSACryptoServiceProvider DecodeRSAPrivateKey(string priKey)
    {
        var privkey = Convert.FromBase64String(priKey);
        byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;
    
        MemoryStream m = new MemoryStream(privkey);
        BinaryReader binr = new BinaryReader(m);    
        byte bt = 0;
        ushort twobytes = 0;
        int elems = 0;
        try
        {
            twobytes = binr.ReadUInt16();
            if (twobytes == 0x8130)
                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;
    
            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);
    
    
            // ------- 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 e)
        {
            Console.WriteLine(e.Message + e.StackTrace);
            return null;
        }
        finally
        {
            binr.Close();
        }
      }
    
    }

    相关文章

      网友评论

          本文标题:keycloak的私钥解开

          本文链接:https://www.haomeiwen.com/subject/jzdazrtx.html