예제 #1
0
파일: Libpriv.cs 프로젝트: Nick1965/Xgate
        public static unsafe byte[] SignText(string src, IPrivKey key)
        {
            const int max_length = 2048;
            sbyte *   tmp        = stackalloc sbyte[max_length];

            byte[] bsrc   = cp1251.GetBytes(src);
            byte[] result = new byte[max_length];

            int rc;

            fixed(byte *psrc = bsrc)
            rc = Crypt_Sign(psrc, bsrc.Length, tmp, max_length, key.getPKey());

            if (rc < 0)
            {
                throw (new IPrivException(rc));
            }
            // return new string(tmp, 0, rc, cp1251);
            int len = 0;

            for (int i = 0; i < max_length; i++)
            {
                len++;
                if (tmp[i] == 0)
                {
                    break;
                }
            }
            result = new byte[len];
            for (int i = 0; i < len; i++)
            {
                result[i] = (byte)tmp[i];
            }
            return(HttpUtility.UrlEncodeToBytes(result));
        }
예제 #2
0
파일: Libpriv.cs 프로젝트: Nick1965/Xgate
        /// <summary>
        /// Проверка подписи ответа
        /// </summary>
        /// <param name="src"></param>
        /// <param name="pubkey"></param>
        /// <param name="serial"></param>
        public static void VerifyMessage(string src, string pubkey, string serial)
        {
            IPrivKey PublicKey = null;

            IPriv.Initialize();
            PublicKey = IPriv.openPublicKey(pubkey, Convert.ToUInt32(serial));

            // Проверить подпись
            PublicKey.verifyText(src);

            // Закрыть крипто-провайдер
            PublicKey.closeKey();
            IPriv.Done();
        }
예제 #3
0
파일: Libpriv.cs 프로젝트: Nick1965/Xgate
        public static unsafe string verifyText(string src, IPrivKey key)
        {
            byte[] srcb = cp1251.GetBytes(src);
            fixed(byte *psrc = srcb)
            {
                sbyte *pdst  = (sbyte *)IntPtr.Zero;
                int    pndst = 0;
                int    rc    = Crypt_Verify(psrc, srcb.Length, &pdst, ref pndst, key.getPKey());

                if (rc != 0)
                {
                    throw (new IPrivException(rc));
                }
                return(new string(pdst, 0, pndst, cp1251));
            }
        }
예제 #4
0
파일: Libpriv.cs 프로젝트: Nick1965/Xgate
        public static unsafe string signText(string src, IPrivKey key)
        {
            const int max_length = 2048;
            sbyte *   tmp        = stackalloc sbyte[max_length];

            byte[] bsrc = cp1251.GetBytes(src);
            int    rc;

            fixed(byte *psrc = bsrc)
            rc = Crypt_Sign(psrc, bsrc.Length, tmp, max_length, key.getPKey());

            if (rc < 0)
            {
                throw (new IPrivException(rc));
            }
            return(new string(tmp, 0, rc, cp1251));
        }
예제 #5
0
파일: Libpriv.cs 프로젝트: Nick1965/Xgate
        public static unsafe IPrivKey openPublicKey(string path, uint keyserial)
        {
            IPrivKey k = new IPrivKey();

            byte[] bpath = new byte[path.Length + 1];            //zero-terminated string
            cp1251.GetBytes(path, 0, path.Length, bpath, 0);
            fixed(byte *ppath = bpath)
            {
                int rc = Crypt_OpenPublicKeyFromFile(0, ppath, keyserial, k.getPKey(), null);

                if (rc != 0)
                {
                    throw (new IPrivException(rc));
                }
            }

            return(k);
        }
예제 #6
0
파일: Libpriv.cs 프로젝트: Nick1965/Xgate
        public static unsafe IPrivKey openSecretKey(string path, string passwd)
        {
            IPrivKey k = new IPrivKey();

            byte[] bpath   = new byte[path.Length + 1];          //zero-terminated string
            byte[] bpasswd = new byte[passwd.Length + 1];        //zero-terminated string
            cp1251.GetBytes(path, 0, path.Length, bpath, 0);
            cp1251.GetBytes(passwd, 0, passwd.Length, bpasswd, 0);
            fixed(byte *ppath = bpath)
            fixed(byte *ppasswd = bpasswd)
            {
                int rc = Crypt_OpenSecretKeyFromFile(0, ppath, ppasswd, k.getPKey());

                if (rc != 0)
                {
                    throw (new IPrivException(rc));
                }
            }
            return(k);
        }
예제 #7
0
파일: Libpriv.cs 프로젝트: Nick1965/Xgate
        /// <summary>
        /// Подписать сообщение в строке в кодировке 1251
        /// </summary>
        /// <param name="src">Исходное сообщение в кодировке UTF-8</param>
        /// <param name="trg">Сообщение в кодировке 1251 упакованное UrlEncode</param>
        /// <param name="s_text">Сообщение с подписью в кодировке UTF-8 (для лога)</param>
        /// <returns>0 - OK; 1 - ошибка криптопровайдера; -1 - системная ошибка</returns>
        public static void SignMessage(string src, out string trg, out string s_text,
                                       string secret, string passwd)
        {
            trg    = "";
            s_text = "";
            IPrivKey SecretKey = null;

            if (string.IsNullOrEmpty(src))
            {
                throw new IPrivException(-101);
            }

            IPriv.Initialize();
            SecretKey = IPriv.openSecretKey(secret, passwd);

            // Подписать сообщение в кодировке 1251
            s_text = SecretKey.signText(src);

            // Закрыть крипто-провайдер
            SecretKey.closeKey();
            IPriv.Done();

            trg = "inputmessage=" + HttpUtility.UrlEncode(s_text, Encoding.GetEncoding(1251));
        }
예제 #8
0
파일: Libpriv.cs 프로젝트: Nick1965/Xgate
 public static unsafe void closeKey(IPrivKey key)
 {
     Crypt_CloseKey(key.getPKey());
 }