如何使用c#和sql server加密和编码密码 [英] how to encrypt and code the password using c# and sql server

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

问题描述

我是新的asp.net开发人员,我想了解一些关于如何在存储到数据库之前加密以及检索后解密数据的信息。




http://www.aspdotnet-suresh.com/2010/12/introduction-here-i-will-explain-how-to_28 .html [ ^ ]


您可以参考:

PWDENCRYPT(Transact-SQL) [ ^ ]

PWDCOMPARE(Transact-SQL) [ ^ ]

关于使用MS SQL Server存储和使用密码。


Hi


加密/解密可以通过多种方式实现

以下是其中一种方式。



虽然将密码从c#保存到SQL将其转换为加密数据

并从sql用户解密密码时使用Decrypt方法获取实际处理值..









  public   string 加密(字符串数据)
{ return data.ToEncryptedData(); }
public string 解密( string data)
{ return data.ToDecryptedData(); }




使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Security.Cryptography;


名称空间助手
{
public static class CryptographyAlgorithm
{


static string key = CMTOOLDEMO 00000\" !;

public static string ToEncryptedData( 字符串输入)
{

byte [] inputArray = UTF8Encoding.UTF8.GetBytes(input);
TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
// tripleDES.GenerateKey();
tripleDES.Key = UTF8Encoding。 UTF8.GetBytes(键);
tripleDES.Mode = CipherMode.ECB;
tripleDES.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tripleDES.CreateEncryptor();
byte [] resultArray = cTransform.TransformFinalBlock(inputArray, 0 ,inputArray.Length);
tripleDES.Clear();
return Convert.ToBase64String(resultArray, 0 ,resultArray.Length);
}

public static string ToDecryptedData( this string input)
{
byte [] inputArray = Convert.FromBase64String(input);
TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
tripleDES.Mode = CipherMode.ECB;
tripleDES.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tripleDES.CreateDecryptor();
byte [] resultArray = cTransform.TransformFinalBlock(inputArray, 0 ,inputArray.Length);
tripleDES.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}
}
}


I am New asp.net Developer, I want some Information regarding how we can encrypt before storing into database and Decrypt data after retrieving.

解决方案

you also refer to:

http://www.aspdotnet-suresh.com/2010/12/introduction-here-i-will-explain-how-to_28.html[^]


You may refer to:
PWDENCRYPT (Transact-SQL)[^]
PWDCOMPARE (Transact-SQL)[^]
regarding storage and usage of passwords with MS SQL Server.


Hi
Encryption/Decryption can be achieved in many ways
below is one of the way.

While saving a password from c# to SQL convert it to encrypted data
and while retrieving password from sql user Decrypt method to get the actual value for processing..




 public string Encrypt(string data)
        { return data.ToEncryptedData(); }
        public string Decrypt(string data)
        { return data.ToDecryptedData(); }




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;


namespace Helper
{
    public static class CryptographyAlgorithm
    {


        static string key = "CMTOOLDEMO!00000";

        public static string ToEncryptedData(this string input)
        {

            byte[] inputArray = UTF8Encoding.UTF8.GetBytes(input);
            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
            // tripleDES.GenerateKey();
            tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
            tripleDES.Mode = CipherMode.ECB;
            tripleDES.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tripleDES.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
            tripleDES.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        public static string ToDecryptedData(this string input)
        {
            byte[] inputArray = Convert.FromBase64String(input);
            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
            tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
            tripleDES.Mode = CipherMode.ECB;
            tripleDES.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tripleDES.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
            tripleDES.Clear();
            return UTF8Encoding.UTF8.GetString(resultArray);
        }
    }
}


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

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