Ejemplo n.º 1
0
        public static void V_Test_CMS_MakeSigData()
        {
            Console.WriteLine("Testing CMS_MakeSigData ...");
            string        strPriKeyFile = null;
            StringBuilder sbPrivateKey  = null;
            //'Dim nIntKeyLen As Integer
            int    nRet          = 0;
            string strInputFile  = null;
            string strOutputFile = null;
            string strCertFile   = null;

            strPriKeyFile = "myuser.epk";
            strCertFile   = "myuser.cer";
            strInputFile  = "excontent.txt";
            strOutputFile = "BasicSignBy_myuser.bin";

            // First we need to read in the private key string
            // NB: This version is not encrypted
            sbPrivateKey = Rsa.ReadEncPrivateKey(strPriKeyFile, "password");
            Console.WriteLine("nIntKeyLen = " + sbPrivateKey.Length);
            if (sbPrivateKey.Length == 0)
            {
                Console.WriteLine(General.LastError());
                Console.WriteLine("Unable to retrieve private key");
                return;
            }
            Console.WriteLine("Key size=" + Rsa.KeyBits(sbPrivateKey.ToString()) + " bits");

            // Now we can sign our message
            nRet = Cms.MakeSigData(strOutputFile, strInputFile, strCertFile, sbPrivateKey.ToString(), 0);
            Console.WriteLine("CMS_MakeSigData returns " + nRet);
        }
Ejemplo n.º 2
0
        public static void V_Test_RSA_PublicKeyFromPrivate()
        {
            Console.WriteLine("Testing RSA_PublicKeyFromPrivate ...");
            string        strPriKeyFile = null;
            StringBuilder sbPrivateKey  = null;
            string        strPublicKey  = null;
            int           nCode         = 0;
            int           nRet          = 0;

            // Read private key from encrypted private key file into internal string form
            strPriKeyFile = "myuser.epk";
            sbPrivateKey  = Rsa.ReadEncPrivateKey(strPriKeyFile, "password");
            if (sbPrivateKey.Length == 0)
            {
                return;
            }

            //Catch error here
            // Display some info about it
            Console.WriteLine("Private key length = {0} bits", Rsa.KeyBits(sbPrivateKey.ToString()));
            nCode = Rsa.KeyHashCode(sbPrivateKey.ToString());
            Console.WriteLine("KeyHashCode={0,8:X}", nCode);
            nRet = Rsa.CheckKey(sbPrivateKey);
            Console.WriteLine("Rsa.CheckKey returns " + nRet + ": (PKI_VALID_PRIVATEKEY=" + 0 + ")");

            // Convert to public key string
            strPublicKey = Rsa.PublicKeyFromPrivate(sbPrivateKey).ToString();
            if (strPublicKey.Length == 0)
            {
                return;
            }

            // Catch error here
            // Display some info about it
            Console.WriteLine("Public key length = " + Rsa.KeyBits(strPublicKey) + " bits");
            nCode = Rsa.KeyHashCode(strPublicKey);
            Console.WriteLine("KeyHashCode={0,8:X}", nCode);
            nRet = Rsa.CheckKey(strPublicKey);
            Console.WriteLine("Rsa.CheckKey returns " + nRet + ": (PKI_VALID_PUBLICKEY=" + 1 + ")");

            // Clean up
            Wipe.String(sbPrivateKey);
        }