Ejemplo n.º 1
0
        /// <summary>
        /// 对指定字符串,根据加密模式和编码方式进行解密
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="mode"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string DecryptString(string strSource, EncryptionMode mode, Encoding encoding)
        {
            int mod = (int)mode % 10;

            byte[] byteSource;
            switch (mod)
            {
            case 1:
                byteSource = ClientEncryptionUtils.Hex2Byte(strSource);
                break;

            case 2:
                byteSource = Convert.FromBase64String(strSource);
                break;

            default:
                throw new ArgumentException(string.Format("EncryptionMode invalid"), "mode");
            }

            byte[] byteResult = DecryptBytes(byteSource, mode);
            string strTemp    = encoding.GetString(byteResult);

            strTemp = strTemp.TrimEnd('\0');
            return(strTemp);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 对指定的字符串,根据加密模式和编码方式进行加密
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="mode"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string EncryptString(string strSource, EncryptionMode mode, Encoding encoding)
        {
            byte[] byteSource = encoding.GetBytes(strSource);
            byte[] byteResult = EncryptBytes(byteSource, mode);
            int    mod        = (int)mode % 10;
            string strReturn;

            switch (mod)
            {
            case 1:
                strReturn = ClientEncryptionUtils.Byte2Hex(byteResult);
                break;

            case 2:
                strReturn = Convert.ToBase64String(byteResult);
                break;

            default:
                throw new ArgumentException(string.Format("EncryptionMode invalid"), "mode");
            }
            return(strReturn);
        }