如何加密和解密? [英] how to encrypt and decrypt ?

查看:70
本文介绍了如何加密和解密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用 asp.net 创建一个 create_user 页面和一个 user_login 页面.我正在尝试在创建用户的过程中对密码进行加密(将密码插入表中),并在登录过程中对密码进行解密(从表中检索密码).
您能否帮助我加密和解密密码?

Hi,
I am creating a create_user page and an user_login page using asp.net. I am trying to encrypt the password during creating the user(insert password into table) and decrypt the password during login(retrive password from table).
Will you pls help me in encrypting and decrypting the password ?

推荐答案

using System.Security.Cryptography;

public static string Encrypt(string strEncrypt)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(strEncrypt);

            if (true)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes("123"));
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes("123");

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

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

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        public static string Decrypt(string strDecrypt)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(strDecrypt);

            if (true)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes("123"));
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes("123");

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

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

            return UTF8Encoding.UTF8.GetString(resultArray);
        }


请参阅此链接 [ ^ ]


虽然给出的两个解决方案是您所要求的,但是请不要使用这种方法

与任何对称加密的替代方法相比,在服务器端存储密码的哈希值要安全得多

创建密码后,服务器将存储哈希值
输入密码进行身份验证时,仅将哈希发送到服务器进行比较

安全密码的密钥 [
While the two solutions you have been given are what you asked for, please don''t use this approach

It''s much more secure to store a hash of the password at the server end, than any symmetrically encrypted alternative

When the password is created, the server stores a hash
When the password is entered for authentication, only the hash is sent to the server for comparison

The Key to a Secure Password[^]


这篇关于如何加密和解密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆