Пример #1
0
        /// <summary>
        /// 文件加密长字符串专用
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] SM4EncryptBinary(byte[] value)
        {
            if (value.Length < 1)
            {
                return(value);
            }
            string basestri   = Encoding.UTF8.GetString(value);
            var    ciphertext = SM4EncryptHelper.is_sm4_ciphertext(basestri, basestri.Length);

            if (ciphertext == (int)SM4_ERROR_CODE.SM4_SUCCESS)
            {
                return(value);
            }
            string base64str = Convert.ToBase64String(value);
            int    outlen    = 0;
            IntPtr ip        = IntPtr.Zero;
            var    iflg      = SM4EncryptHelper.sm4_encrypt_binary(base64str, base64str.Length, out ip, out outlen);

            if (iflg == (int)SM4_ERROR_CODE.SM4_SUCCESS)
            {
                byte[] array = new byte[outlen];
                Marshal.Copy(ip, array, 0, outlen);
                SM4EncryptHelper.sm4_freebuf(out ip);
                return(array);
            }
            SM4EncryptHelper.sm4_freebuf(out ip);
            return(new byte[0]);
        }
Пример #2
0
        /// <summary>
        /// sm4加密
        /// </summary>
        /// <param name="value">原始字段</param>
        /// <returns>加密数据</returns>
        public static string SM4Encrypt(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return("");
            }
            var ciphertext = SM4EncryptHelper.is_sm4_ciphertext(value, value.Length);

            if (ciphertext == (int)SM4_ERROR_CODE.SM4_SUCCESS)
            {
                return(value);
            }
            int    outlen = 0;
            IntPtr ip     = IntPtr.Zero;
            var    iflg   = SM4EncryptHelper.sm4_encrypt(value, value.Length, out ip, out outlen);

            string result = "";

            if (iflg == (int)SM4_ERROR_CODE.SM4_SUCCESS)
            {
                byte[] array = new byte[outlen];
                Marshal.Copy(ip, array, 0, outlen);
                result = Encoding.UTF8.GetString(array);
            }
            SM4EncryptHelper.sm4_freebuf(out ip);
            return(result);
        }
Пример #3
0
        /// <summary>
        /// sm4解密
        /// </summary>
        /// <param name="text">加密字段</param>
        /// <returns>解密字符串</returns>
        public static string SM4Decrypt(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return("");
            }
            int    deoutlen = 0;
            IntPtr deip     = IntPtr.Zero;
            var    deiflg   = SM4EncryptHelper.sm4_decrypt(text, text.Length, out deip, out deoutlen);

            byte[] dearray = new byte[deoutlen];
            Marshal.Copy(deip, dearray, 0, deoutlen);

            var deresult = Encoding.UTF8.GetString(dearray);

            _ = SM4EncryptHelper.sm4_freebuf(out deip);
            return(deresult);
        }
Пример #4
0
        /// <summary>
        /// sm4加密
        /// </summary>
        /// <param name="value">原始字段</param>
        /// <returns>加密数据</returns>
        public static string SM4Encrypt(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return("");
            }
            int    outlen = 0;
            IntPtr ip     = IntPtr.Zero;
            var    iflg   = SM4EncryptHelper.sm4_encrypt(value, value.Length, out ip, out outlen);

            byte[] array = new byte[outlen];
            Marshal.Copy(ip, array, 0, outlen);

            var result = Encoding.UTF8.GetString(array);

            SM4EncryptHelper.sm4_freebuf(out ip);
            return(result);
        }
Пример #5
0
        /// <summary>
        /// 文件加密长字符串专用
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] SM4EncryptBinary(byte[] value)
        {
            if (value.Length < 1)
            {
                return(value);
            }
            string base64str = Convert.ToBase64String(value);
            int    outlen    = 0;
            IntPtr ip        = IntPtr.Zero;
            var    iflg      = SM4EncryptHelper.sm4_encrypt_binary(base64str, base64str.Length, out ip, out outlen);

            if (iflg == 0)
            {
                byte[] array = new byte[outlen];
                Marshal.Copy(ip, array, 0, outlen);
                SM4EncryptHelper.sm4_freebuf(out ip);
                return(array);
            }
            SM4EncryptHelper.sm4_freebuf(out ip);
            return(new byte[0]);
        }
Пример #6
0
        public static byte[] SM4DecryptBinary(byte[] value)
        {
            if (value.Length < 1)
            {
                return(value);
            }
            int    deoutlen  = 0;
            IntPtr deip      = IntPtr.Zero;
            string base64str = Encoding.UTF8.GetString(value);
            var    deiflg    = SM4EncryptHelper.sm4_decrypt_binary(base64str, base64str.Length, out deip, out deoutlen);

            if (deiflg == (int)SM4_ERROR_CODE.SM4_SUCCESS)
            {
                byte[] dearray = new byte[deoutlen];
                Marshal.Copy(deip, dearray, 0, deoutlen);
                var baseDecry = Encoding.UTF8.GetString(dearray);
                var result    = Convert.FromBase64String(baseDecry);
                SM4EncryptHelper.sm4_freebuf(out deip);
                return(result);
            }
            SM4EncryptHelper.sm4_freebuf(out deip);
            return(value);
        }