HermaFx.Cryptography.ASN1.GetBytes C# (CSharp) Method

GetBytes() public method

public GetBytes ( ) : byte[]
return byte[]
        public virtual byte[] GetBytes()
        {
            byte[] val = null;

            if (Count > 0)
            {
                int esize = 0;
                ArrayList al = new ArrayList();
                foreach (ASN1 a in elist)
                {
                    byte[] item = a.GetBytes();
                    al.Add(item);
                    esize += item.Length;
                }
                val = new byte[esize];
                int pos = 0;
                for (int i = 0; i < elist.Count; i++)
                {
                    byte[] item = (byte[])al[i];
                    Buffer.BlockCopy(item, 0, val, pos, item.Length);
                    pos += item.Length;
                }
            }
            else if (m_aValue != null)
            {
                val = m_aValue;
            }

            byte[] der;
            int nLengthLen = 0;

            if (val != null)
            {
                int nLength = val.Length;
                // special for length > 127
                if (nLength > 127)
                {
                    if (nLength <= Byte.MaxValue)
                    {
                        der = new byte[3 + nLength];
                        Buffer.BlockCopy(val, 0, der, 3, nLength);
                        nLengthLen = 0x81;
                        der[2] = (byte)(nLength);
                    }
                    else if (nLength <= UInt16.MaxValue)
                    {
                        der = new byte[4 + nLength];
                        Buffer.BlockCopy(val, 0, der, 4, nLength);
                        nLengthLen = 0x82;
                        der[2] = (byte)(nLength >> 8);
                        der[3] = (byte)(nLength);
                    }
                    else if (nLength <= 0xFFFFFF)
                    {
                        // 24 bits
                        der = new byte[5 + nLength];
                        Buffer.BlockCopy(val, 0, der, 5, nLength);
                        nLengthLen = 0x83;
                        der[2] = (byte)(nLength >> 16);
                        der[3] = (byte)(nLength >> 8);
                        der[4] = (byte)(nLength);
                    }
                    else
                    {
                        // max (Length is an integer) 32 bits
                        der = new byte[6 + nLength];
                        Buffer.BlockCopy(val, 0, der, 6, nLength);
                        nLengthLen = 0x84;
                        der[2] = (byte)(nLength >> 24);
                        der[3] = (byte)(nLength >> 16);
                        der[4] = (byte)(nLength >> 8);
                        der[5] = (byte)(nLength);
                    }
                }
                else
                {
                    // basic case (no encoding)
                    der = new byte[2 + nLength];
                    Buffer.BlockCopy(val, 0, der, 2, nLength);
                    nLengthLen = nLength;
                }
                if (m_aValue == null)
                    m_aValue = val;
            }
            else
                der = new byte[2];

            der[0] = m_nTag;
            der[1] = (byte)nLengthLen;

            return der;
        }

Usage Example

Ejemplo n.º 1
0
            /*
             * RSAPrivateKey ::= SEQUENCE {
             *	version           Version,
             *	modulus           INTEGER,  -- n
             *	publicExponent    INTEGER,  -- e
             *	privateExponent   INTEGER,  -- d
             *	prime1            INTEGER,  -- p
             *	prime2            INTEGER,  -- q
             *	exponent1         INTEGER,  -- d mod (p-1)
             *	exponent2         INTEGER,  -- d mod (q-1)
             *	coefficient       INTEGER,  -- (inverse of q) mod p
             *	otherPrimeInfos   OtherPrimeInfos OPTIONAL
             * }
             */
            static public byte[] Encode(RSA rsa)
            {
                RSAParameters param = rsa.ExportParameters(true);

                ASN1 rsaPrivateKey = new ASN1(0x30);

                rsaPrivateKey.Add(new ASN1(0x02, new byte[1] {
                    0x00
                }));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.Modulus));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.Exponent));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.D));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.P));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.Q));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.DP));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.DQ));
                rsaPrivateKey.Add(ASN1Convert.FromUnsignedBigInteger(param.InverseQ));

                return(rsaPrivateKey.GetBytes());
            }
All Usage Examples Of HermaFx.Cryptography.ASN1::GetBytes