异或加密和解密

2014-10-24 16:31:58  访问(2235) 赞(0) 踩(0)

 #region 加密字符串成为byte[]数组

        /// <summary>
        /// 加密字符串成为byte[]数组
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <param name="_encoding"></param>
        /// <returns></returns>
        protected byte[] XorEncryptToByteArray(byte[] byteText, string key, System.Text.Encoding _encoding)
        {
            if (byteText == null || byteText.Length == 0)
                return null;

            if (_encoding == null)
            {
                MethodBase methodBaseValue = MethodBase.GetCurrentMethod();
                throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> Encoding _encoding 为空。");
            }

            if (key == null || key.Length == 0)
            {
                MethodBase methodBaseValue = MethodBase.GetCurrentMethod();
                throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> string key 为空。");
            }

            byte[] byteKey = _encoding.GetBytes(key);

            int iByteTextLen = byteText.Length;
            int iByteKeyLen = byteKey.Length;

            if (iByteKeyLen == 0)
            {
                MethodBase methodBaseValue = MethodBase.GetCurrentMethod();
                throw new Exception("Method :==> " + methodBaseValue.ReflectedType.FullName + " ~ " + methodBaseValue.ToString() + " Exception :==> " + "加密密钥key长度为0。");
            }

            for (int i = 0; i < iByteTextLen; ++i)
            {
                byteText[i] = (byte)(byteText[i] ^ byteKey[i % iByteKeyLen]);
            }

            return byteText;
        }

        #endregion 加密字符串成为byte[]数组


标签:异或加密和解密 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)