当前位置: 代码迷 >> C# >> 求个des加密解密步骤(没有向量的)
  详细解决方案

求个des加密解密步骤(没有向量的)

热度:374   发布时间:2016-04-28 08:42:19.0
求个des加密解密方法(没有向量的)
如题...百度了半天 都是有向量的,也尝试修改,但是实在是无能为力,

用于app之间通信的时候敏感数据加密解密,但是找了半天都没有找到大家都一样的加密方法...
------解决思路----------------------
RSA 加密看有用沒?
        #region RSA加密
        /// <summary>
        /// RSA加密
        /// </summary>
        /// <param name="s">明文</param>
        /// <param name="key">密钥</param>
        /// <returns>密文</returns>
        public string EncryptRSA(string encryptstr, string key)
        {
            if (string.IsNullOrEmpty(encryptstr))
            {
                return "";
            }
            if (string.IsNullOrEmpty(key))
            {
                return encryptstr;
            }
            try
            {
                byte[] enByteArray = UTF8Encoding.UTF8.GetBytes(encryptstr);
                CspParameters cspp = new CspParameters();
                cspp.KeyContainerName = key;
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspp);
                rsa.PersistKeyInCsp = true;
                byte[] bytes = rsa.Encrypt(enByteArray, true);
                return BitConverter.ToString(bytes);
            }
            catch (Exception ex)
            {
                return encryptstr;
            }
        }
       #endregion


        #region RSA解密
        /// <summary>
        /// RSA解密
        /// </summary>
        /// <param name="s">密文</param>
        /// <param name="key">密钥</param>
        /// <returns>明文</returns>
        public string DecryptRSA(string decryptstr, string key)
        {
            if (string.IsNullOrEmpty(decryptstr))
            {
                return "";
            }
            if (string.IsNullOrEmpty(key))
            {
                return decryptstr;
            }
            try
            {
                string[] deArray = decryptstr.Split(new string[] { "-" }, StringSplitOptions.None);
                byte[] deByteArray = Array.ConvertAll<string, byte>(deArray,
                       (a => Convert.ToByte(byte.Parse(a, NumberStyles.HexNumber))));
                CspParameters cspp = new CspParameters();
                cspp.KeyContainerName = key;
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspp);
                rsa.PersistKeyInCsp = true;
                byte[] bytes = rsa.Decrypt(deByteArray, true);
                return UTF8Encoding.UTF8.GetString(bytes);
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        #endregion
------解决思路----------------------
在项目里找到一个这个玩意,没用过




#region AES加密/解密(无向量)

        public static string AesEncrypt(string str, string key)
        {
            if (string.IsNullOrEmpty(str)) return null;
            Byte[] toEncryptArray = Encoding.Default.GetBytes(str);

            RijndaelManaged rm = new RijndaelManaged
            {
                Key = Encoding.Default.GetBytes(key),
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };

            ICryptoTransform cTransform = rm.CreateEncryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            string a = BitConverter.ToString(resultArray).Replace("-", "");
            return a;
            //return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        public static string AesDecrypt(string str, string key)
        {
            if (string.IsNullOrEmpty(str)) return null;
            Byte[] toEncryptArray = Convert.FromBase64String(str);

            RijndaelManaged rm = new RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes(key),
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };

            ICryptoTransform cTransform = rm.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Encoding.UTF8.GetString(resultArray);
        }

        #endregion


------解决思路----------------------
把Mode设置为ECB就不需要IV了
  相关解决方案