Exemplo n.º 1
0
        private void SetPassword(string password)
        {
            byte[] pass   = Encoding.ASCII.GetBytes(password);
            var    result = VipNetImport.CryptSetProvParam(ProviderHandle, 33, pass, 0);

            CheckCapicomResult("SetPassword" + password, result);
            Console.WriteLine("CryptSetProvParam - {0}", result);
        }
Exemplo n.º 2
0
        public int CreateHashHandle()
        {
            IntPtr hHash  = new IntPtr();
            var    result = VipNetImport.CryptCreateHash(ProviderHandle, 32798, 0, 0, ref hHash);

            CheckCapicomResult("CreateHashHandle", result);
            Console.WriteLine("CryptCreateHash - {0}", result);
            return(hHash.ToInt32());
        }
Exemplo n.º 3
0
        private void OpenKey()
        {
            IntPtr hUserKey = new IntPtr();
            var    result   = VipNetImport.CryptGetUserKey(ProviderHandle, 2, ref hUserKey);

            CheckCapicomResult("OpenKey", result);
            Console.WriteLine("CryptGetUserKey - {0}", result);
            KeyPair = new KeyPair(hUserKey.ToInt32());
        }
Exemplo n.º 4
0
        public PublicKey ImportPublicKey(byte[] keyData)
        {
            var pPublicKey = new IntPtr();
            var result     = VipNetImport.CryptImportKey(ProviderHandle, keyData, keyData.Length, 0, 0, ref pPublicKey);

            return(new PublicKey(pPublicKey.ToInt32())
            {
                Bytes = keyData
            });
        }
Exemplo n.º 5
0
        private byte[] ExportKeyData(int keyType)
        {
            var pdwDataLen = new IntPtr();
            var result     = VipNetImport.CryptExportKey(KeyPair.Handle, 0, keyType, 0, null, ref pdwDataLen);

            Console.Write("{0:X}", Marshal.GetLastWin32Error());
            var data = new byte[pdwDataLen.ToInt32()];

            result = VipNetImport.CryptExportKey(KeyPair.Handle, 0, keyType, 0, data, ref pdwDataLen);
            return(data);
        }
Exemplo n.º 6
0
        private void OpenContainer()
        {
            var hProv = new IntPtr();

            var result = VipNetImport.CryptAcquireContextW(
                ref hProv,
                ContainerPath,
                ProviderName, 2, 64);

            Console.WriteLine("CryptAcquireContextW - {0}", result);
            CheckCapicomResult("OpenContainer" + ContainerPath, result);
            ProviderHandle = hProv.ToInt32();
        }
Exemplo n.º 7
0
        private string GetCertificate()
        {
            IntPtr pSize  = new IntPtr();
            IntPtr pData  = IntPtr.Zero;
            var    result = VipNetImport.CryptGetKeyParam(KeyPair.Handle, 26, pData, ref pSize, 0);

            pData  = Marshal.AllocHGlobal(pSize.ToInt32());
            result = VipNetImport.CryptGetKeyParam(KeyPair.Handle, 26, pData, ref pSize, 0);
            var publicKeyValue = new byte[pSize.ToInt32()];

            Marshal.Copy(pData, publicKeyValue, 0, publicKeyValue.Length);
            Console.WriteLine("{0}, {0:X}", Marshal.GetLastWin32Error());
            Console.WriteLine("CryptGetKeyParam - {0}", result);
            var base64PublicKey = Convert.ToBase64String(publicKeyValue);

            Console.WriteLine("Certificate:{0}", base64PublicKey);
            return(base64PublicKey);
        }
Exemplo n.º 8
0
        public Signature SigningHash(Hash hash)
        {
            var signature = new Signature()
            {
                SignedHash = hash
            };
            var pbSignature = IntPtr.Zero;
            var pdwLength   = new IntPtr();
            var result      = VipNetImport.CryptSignHash(hash.Handle, 2, null, 0, pbSignature, ref pdwLength);

            signature.Bytes = new byte[pdwLength.ToInt32()];
            if (pdwLength.ToInt32() == 0)
            {
                CheckCapicomResult("SigningHash" + "length == 0", result);
            }
            var size = pdwLength.ToInt32() * Marshal.SizeOf(signature.Bytes[0]);

            pbSignature = Marshal.AllocHGlobal(size);

            Console.WriteLine("SignatureLength = {0}", pdwLength.ToInt32());
            result = VipNetImport.CryptSignHash(hash.Handle, 2, null, 0, pbSignature, ref pdwLength);
            Console.WriteLine("CryptSignHash - {0}", result);
            Console.WriteLine("!!{0:x} = {0}!!", Marshal.GetLastWin32Error());

            Marshal.Copy(pbSignature, signature.Bytes, 0, signature.Bytes.Length);
            Console.WriteLine("Signature: {0}", signature.Base64);

            //y2XdxwhHeZ0nReO2zFSJdBbObXcra+w0tK/dCukZUGF0kjsWq/+AbO4S4/PL7LX9C8fUu9D0WsZStxRjvSmApw==
            signature.Key = ImportPublicKey();

            var privateKey = Convert.ToBase64String(ExportKeyData(VipNetConst.PrivateKeyBlob));

            //Private Key:"BxCKACMuAABJVENTTAAAAK0AAAAwgaoCAQMwbgIBAgICLiOgHjAcBgYqhQMCAhMwEgYHKoUDAgIjAQYHKoUDAgIeAaESBBCg4PLNAAAAAMD8/1W+Xs4BpiQwIoAPMjAxMzA2MDExMTUxMjFagQ8yMDE0MDYwMTExNTEyMVqnCwMJAIYzJMGalnL1MDUCAgQAAgJgLqcjAyEAkvgi9y7r1tivJcf6ZDJ56LEzqJZBmlAJ2yb/qyqJu56qBgIEgAAAAEwAAABvtgPbhcLo2eXmUKoVaZjWUJthcx81bqC1hg5hbJ/tnOQ6tho60A1FC2vU7/wvCqTBeQJwpCA7n0qAfXSiU2ncIG96jDkL4PWmx4a+"

            //Public Key: "BiAAACMuAABNQUcxAAIAADASBgcqhQMCAiMBBgcqhQMCAh4BhjMkwZqWcvXgwPu0J5QiHE6G+2NnDNYmazapb5QCt0YCCLE/UyIysDJMKQZt1bJrdDXQ5AmeTncWkatgBdbJlg=="
            //"06200000232e00004d41473100020000301206072a85030202230106072a850302021e01863324c19a9672f5e0c0fbb42794221c4e86fb63670cd6266b36a96f9402b7460208b13f532232b0324c29066dd5b26b7435d0e4099e4e771691ab6005d6c996"
            //Signature:  "QtiWoUjMcdjlNOD3SL9tEWb6bG+oU8yXygrGMn4Qc7aKELF9BUKfSJNFyOf/5f90A5FLs9b+IKOIylRODMM4zg=="
            if (!VerifySignature(signature))
            {
                throw new Exception("VerifySignature not pass");
            }

            return(signature);
        }
Exemplo n.º 9
0
        public Hash HashData(string message)
        {
            var hash = new Hash(CreateHashHandle());

            var messageBytes = Encoding.UTF8.GetBytes(message);
            var result       = VipNetImport.CryptHashData(hash.Handle, messageBytes, messageBytes.Length, 0);
            //if (message.Length != messageBytes.Length)
            //throw new Exception(messageBytes.Length + "!=" + messageBytes.Length);

            IntPtr pSize = new IntPtr();
            IntPtr pData = IntPtr.Zero;

            result = VipNetImport.CryptGetHashParam(hash.Handle, 0x0002, pData, ref pSize, 0);
            pData  = Marshal.AllocHGlobal(pSize.ToInt32());
            result = VipNetImport.CryptGetHashParam(hash.Handle, 0x0002, pData, ref pSize, 0);
            CheckCapicomResult("HashData" + message, result);

            hash.Bytes = new byte[pSize.ToInt32()];
            Marshal.Copy(pData, hash.Bytes, 0, hash.Bytes.Length);
            Console.WriteLine("{0}, {0:X}", Marshal.GetLastWin32Error());
            Console.WriteLine("CryptHashData - {0}", result);
            Console.WriteLine("Hash:{0}", hash.Base64);
            return(hash);
        }
Exemplo n.º 10
0
 public bool VerifySignature(Signature signature)
 {
     return(VipNetImport.CryptVerifySignature(signature.SignedHash.Handle, signature.Bytes, signature.Bytes.Length, signature.Key.Handle, null, 0));
 }