密码解密和加密 [英] password decrypt and encrypt

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

问题描述

我有三个文本框名称为txtenter,txtencrypt和txtdecrypt,两个按钮名称为ENCRYPT,DECRYPT..如果我在txtenter文本框中输入任何值,则单击ENCRYPT按钮我想将txtenter中的数据转换为txtencrypt中的加密格式化数据显示,然后再单击DECRYPT解密txtdecrypt中的数据显示。







  <   asp:TextBox     id   =  txtenter    runat   =  server  /  >  
< asp:TextBox id = txtencrypt runat = server / >
< asp:TextBox id = txtdecrypt runat = 服务器 / >
< asp:按钮 id = ENCRYPT runat = sever / >
< asp:按钮 id = DECRYPT < span class =code-attribute> runat = sever / >







问候

sarath。

解决方案

你的实际问题没有提到密码,但关于机会您正计划使用代码加密和解密密码(如主题所示)然后我会说:不要。



见这里:< a href =http://www.codeproject.com/Tips/186585/Password-Storage-How-to-do-it>密码存储:如何操作。 [ ^ ]


试试这个,,,, :)



 使用系统; 
使用 System.IO;
使用 System.Text;
使用 System.Security.Cryptography;

命名空间 Paddedwall.CryptoLib
{

public class 加密
{
#region enums,constants&字段
// 对称加密类型
public enum CryptoTypes
{
encTypeDES = 0
encTypeRC2,
encTypeRijndael,
encTypeTripleDES
}

private const string CRYPT_DEFAULT_PASSWORD = CB06cfE507a1\" ;
private const CryptoTypes CRYPT_DEFAULT_METHOD = CryptoTypes.encTypeRijndael;

private byte [] mKey = { 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ,< span class =code-digit> 17
18 19 20 21 22 23 24 };
private byte [] mIV = { 65 110 68 26 69 178 200 219 };
private byte [] SaltByteArray = {0x49,0x76,0x61,0x6e,0x20,0x4d ,0x65,0x64,0x76,0x65,0x64,0x65,0x76};
private CryptoTypes mCryptoType = CRYPT_DEFAULT_METHOD;
private string mPassword = CRYPT_DEFAULT_PASSWORD;
#endregion

#region Constructors

public 加密()
{
calculateNewKeyAndIV();
}

public 加密(CryptoTypes CryptoType)
{
this .CryptoType = CryptoType;
}
#endregion

#region道具


public CryptoTypes CryptoType
{
get
{
return mCryptoType;
}
set
{
if (mCryptoType != value
{
mCryptoType = value ;
calculateNewKeyAndIV();
}
}
}

/// < 摘要 >
/// Passsword Key Property。
/// 加密/解密
/// < / summary >
public < span class =code-keyword> string
密码
{
获取
{
return mPassword;
}
set
{
if (mPassword != value
{
mPassword = value ;
calculateNewKeyAndIV();
}
}
}
#endregion

#region加密


public string 加密( string inputText)
{
// 声明新编码器
UTF8Encoding UTF8Encoder = new UTF8Encoding();
// 获取字符串的字节表示
byte [] inputBytes = UTF8Encoder.GetBytes(inputText);

// 转换回字符串
return Convert.ToBase64String(EncryptDecrypt(inputBytes, true ));
}


public string 加密(< span class =code-keyword> string inputText, string password)
{
这个 .Password =密码;
return this .Encrypt(inputText);
}


public string 加密(< span class =code-keyword> string inputText, string password,CryptoTypes cryptoType)
{
mCryptoType = cryptoType;
return this .Encrypt(inputText,password);
}


public string 加密(< span class =code-keyword> string inputText,CryptoTypes cryptoType)
{
this .CryptoType = cryptoType;
return this .Encrypt(inputText);
}

#endregion

#region解密


public string 解密( string inputText)
{
// 声明一个新的编码器
UTF8Encoding UTF8Encoder = new UTF8Encoding();
// 获取字符串的字节表示
byte [] inputBytes = Convert.FromBase64String(inputText);

// 转换回字符串
return UTF8Encoder.GetString(EncryptDecrypt(inputBytes, false ));
}

/// < 摘要 >
/// 使用用户定义的密码密钥解密字符串
/ // < / summary >
/// < span class =code-summarycomment>< param name =inputText > 要解密的字符串< / param >
< span class =code-summarycomment> /// < param name =password > 解密时使用的密码< / param >
/// < 返回 > 解密后的字符串< / returns >
public string 解密( string inputText ,字符串密码)
{
.Password =密码;
return Decrypt(inputText);
}


public string 解密(< span class =code-keyword> string
inputText, string password,CryptoTypes cryptoType)
{
mCryptoType = cryptoType;
return 解密(inputText,密码);
}


public string 解密(< span class =code-keyword> string inputText,CryptoTypes cryptoType)
{
this .CryptoType = cryptoType;
return Decrypt(inputText);
}
#endregion

#region Symmetric Engine


private byte [] EncryptDecrypt( byte [] inputBytes, bool Encrpyt)
{
// 获取正确的转换
ICryptoTransform transform = getCryptoTransform(Encrpyt);

// 输出的内存流
MemoryStream memStream = < span class =code-keyword> new MemoryStream();

尝试
{
// 设置cryption - 写入memstream的输出
CryptoStream cryptStream = new CryptoStream(memStream,transform,CryptoStreamMode。写);

// 将数据写入加密引擎
cryptStream.Write (inputBytes, 0 ,inputBytes.Length);

// 我们已经完成
cryptStream.FlushFinalBlock() ;

// 获取结果
byte [] output = memStream.ToArray();

// 已完成引擎,因此关闭流
cryptStream.Close();

return 输出;
}
catch (例外e)
{
// 抛出错误
throw new 异常( 对称引擎出错。错误: + e.Message,e) ;
}
}


private ICryptoTransform getCryptoTransform( bool encrypt)
{
SymmetricAlgorithm SA = selectAlgorithm();
SA.Key = mKey;
SA.IV = mIV;
if (加密)
{
return SA.CreateEncryptor( );
} else
{
return SA.CreateDecryptor();
}
}

private SymmetricAlgorithm selectAlgorithm()
{
SymmetricAlgorithm SA;
switch (mCryptoType)
{
case CryptoTypes.encTypeDES:
SA = DES.Create();
break ;
case CryptoTypes.encTypeRC2:
SA = RC2.Create();
break ;
case CryptoTypes.encTypeRijndael:
SA = Rijndael.Create();
break ;
case CryptoTypes.encTypeTripleDES:
SA = TripleDES.Create();
break ;
默认
SA = TripleDES.Create();
break ;
}
return SA;
}


私有 void calculateNewKeyAndIV()
{
// 使用salt以便无法通过字典攻击找到密钥
PasswordDeriveBytes pdb = new PasswordDeriveBytes(mPassword,SaltByteArray);
SymmetricAlgorithm algo = selectAlgorithm();
mKey = pdb.GetBytes(algo.KeySize / 8 );
mIV = pdb.GetBytes(algo.BlockSize / 8 );
}

#endregion
}


{
#region enum,constants and fields
// 可用哈希类型
public enum HashingTypes
{
SHA,SHA256,SHA384,SHA512,MD5
}
#endregion

#region static members
public < span class =code-keyword> static string 哈希( String inputText)
{
return ComputeHash(inputText,HashingTypes.MD5);
}

public static 字符串哈希(字符串 inputText,HashingTypes hashingType)
{
return ComputeHash(inputText,hashingType);
}

/// < 摘要 >
/// 如果输入文本等于散列文本,则返回true
/// < / summary >
/// < param name =inputText > 要测试的未散列文本< / param >
< span class =code-summarycomment> /// < param name =hashText > 已散列文本< / param >
/// < 返回 > 布尔值true或false < / returns >
public static bool isHashEqual( string inputText, string hashText)
{
return (Hash(inputText)== hashText);
}

public static bool isHashEqual( string inputText, string hashText,HashingTypes hashingType)
{
return (哈希(inputText,hashingType)== hashText);
}
#endregion

#region Hashing Engine


private static string ComputeHash( string inputText,HashingTypes hashingType)
{
HashAlgorithm HA = getHashAlgorithm(hashingType);

// 声明新编码器
UTF8Encoding UTF8Encoder = < span class =code-keyword> new UTF8Encoding();
// 获取输入文本的字节表示
byte [] inputBytes = UTF8Encoder.GetBytes(inputText);


// 哈希输入字节数组
byte [] output = HA.ComputeHash(inputBytes);

// 将输出字节数组转换为字符串
< span class =code-keyword> return Convert.ToBase64String(output);
}


private static HashAlgorithm getHashAlgorithm( HashingTypes hashingType)
{
switch (hashingType)
{
case HashingTypes.MD5:
return new MD5CryptoServiceProvider();
case HashingTypes.SHA:
return new SHA1CryptoServiceProvider();
case HashingTypes.SHA256:
return new SHA256Managed();
case HashingTypes.SHA384:
return new SHA384Managed();
case HashingTypes.SHA512:
return new SHA512Managed();
默认
返回 new MD5CryptoServiceProvider();
}
}
#endregion

}


}


试试这个... :)





使用C#加密和解密数据 [ ^ ]

I am having three text boxes name as "txtenter","txtencrypt" and "txtdecrypt" and two buttons name as "ENCRYPT","DECRYPT"..If i enter any value in "txtenter" text box then click on "ENCRYPT" button i want to convert data in "txtenter" in to encrypted formated data display in "txtencrypt" and click on "DECRYPT" again decrypt the data display in "txtdecrypt".



<asp:TextBox id="txtenter" runat="server"/>
<asp:TextBox id="txtencrypt" runat="server"/>
<asp:TextBox id="txtdecrypt" runat="server"/>
<asp:Button id="ENCRYPT" runat="sever"/>
<asp:Button id="DECRYPT" runat="sever"/>




Regards
sarath.

解决方案

Your actual question doesn't mention passwords, but on the off chance that you are planning on using the code to encrypt and decrypt passwords (as the subject line implies) then I will say this: Don't.

See here: Password Storage: How to do it.[^]


Try this,,,,:)

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
 
namespace Paddedwall.CryptoLib
{
   
    public class Crypto
    {
        #region enums, constants & fields
        //types of symmetric encyption
        public enum CryptoTypes
        {
            encTypeDES = 0,
            encTypeRC2,
            encTypeRijndael,
            encTypeTripleDES
        }
 
        private const string CRYPT_DEFAULT_PASSWORD = "CB06cfE507a1";
        private const CryptoTypes CRYPT_DEFAULT_METHOD = CryptoTypes.encTypeRijndael;
 
        private byte[] mKey = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
        private byte[] mIV = {65, 110, 68, 26, 69, 178, 200, 219};
        private byte[] SaltByteArray  = {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76};
        private CryptoTypes mCryptoType = CRYPT_DEFAULT_METHOD;
        private string mPassword = CRYPT_DEFAULT_PASSWORD;
        #endregion
 
        #region Constructors
 
        public Crypto()
        {
            calculateNewKeyAndIV();
        }
 
        public Crypto(CryptoTypes CryptoType)
        {
            this.CryptoType = CryptoType;
        }
        #endregion
 
        #region Props
 
      
        public CryptoTypes CryptoType
        {
            get
            {
                return mCryptoType;
            }
            set
            {
                if (mCryptoType != value)
                {
                    mCryptoType = value;
                    calculateNewKeyAndIV();
                }
            }
        }
 
        /// <summary>
        ///     Passsword Key Property.
        ///     The password key used when encrypting / decrypting
        /// </summary>
        public string Password
        {
            get
            {
                return mPassword;
            }
            set
            {
                if (mPassword != value)
                {
                    mPassword = value;
                    calculateNewKeyAndIV();
                }
            }
        }
        #endregion
 
        #region Encryption
 
      
        public string Encrypt(string inputText)
        {
            //declare a new encoder
            UTF8Encoding UTF8Encoder = new UTF8Encoding();
            //get byte representation of string
            byte[] inputBytes = UTF8Encoder.GetBytes(inputText);
 
            //convert back to a string
            return Convert.ToBase64String(EncryptDecrypt(inputBytes,true));
        }
 
      
        public string Encrypt(string inputText, string password)
        {
            this.Password = password;
            return this.Encrypt(inputText);
        }
 
       
        public string Encrypt(string inputText, string password, CryptoTypes cryptoType)
        {
            mCryptoType = cryptoType;
            return this.Encrypt(inputText,password);
        }
 
      
        public string Encrypt(string inputText, CryptoTypes cryptoType)
        {
            this.CryptoType = cryptoType;
            return this.Encrypt(inputText);
        }
 
        #endregion
 
        #region Decryption
 
      
        public string Decrypt(string inputText)
        {
            //declare a new encoder
            UTF8Encoding UTF8Encoder = new UTF8Encoding();
            //get byte representation of string
            byte[] inputBytes = Convert.FromBase64String(inputText);
 
            //convert back to a string
            return UTF8Encoder.GetString(EncryptDecrypt(inputBytes,false));
        }
 
        /// <summary>
        ///     decrypts a string using a user defined password key
        /// </summary>
        /// <param name="inputText">string to decrypt</param>
        /// <param name="password">password to use when decrypting</param>
        /// <returns>a decrypted string</returns>
        public string Decrypt(string inputText, string password)
        {
            this.Password = password;
            return Decrypt(inputText);
        }
 
     
        public string Decrypt(string inputText, string password, CryptoTypes cryptoType)
        {
            mCryptoType = cryptoType;
            return Decrypt(inputText,password);
        }
 
     
        public string Decrypt(string inputText, CryptoTypes cryptoType)
        {
            this.CryptoType = cryptoType;
            return Decrypt(inputText);
        }
        #endregion
 
        #region Symmetric Engine
 
     
        private byte[] EncryptDecrypt(byte[] inputBytes, bool Encrpyt)
        {
            //get the correct transform
            ICryptoTransform transform = getCryptoTransform(Encrpyt);
 
            //memory stream for output
            MemoryStream memStream = new MemoryStream();
 
            try
            {
                //setup the cryption - output written to memstream
                CryptoStream cryptStream = new CryptoStream(memStream,transform,CryptoStreamMode.Write);
 
                //write data to cryption engine
                cryptStream.Write(inputBytes,0,inputBytes.Length);
 
                //we are finished
                cryptStream.FlushFinalBlock();
 
                //get result
                byte[] output = memStream.ToArray();
 
                //finished with engine, so close the stream
                cryptStream.Close();
 
                return output;
            }
            catch (Exception e)
            {
                //throw an error
                throw new Exception("Error in symmetric engine. Error : " + e.Message,e);
            }
        }
 
      
        private ICryptoTransform getCryptoTransform(bool encrypt)
        {
            SymmetricAlgorithm SA = selectAlgorithm();
            SA.Key = mKey;
            SA.IV = mIV;
            if (encrypt)
            {
                return SA.CreateEncryptor();
            }else
            {
                return SA.CreateDecryptor();
            }
        }
      
        private SymmetricAlgorithm selectAlgorithm()
        {
            SymmetricAlgorithm SA;
            switch (mCryptoType)
            {
                case CryptoTypes.encTypeDES:
                    SA = DES.Create();
                    break;
                case CryptoTypes.encTypeRC2:
                    SA = RC2.Create();
                    break;
                case CryptoTypes.encTypeRijndael:
                    SA = Rijndael.Create();
                    break;
                case CryptoTypes.encTypeTripleDES:
                    SA = TripleDES.Create();
                    break;
                default:
                    SA = TripleDES.Create();
                    break;
            }
            return SA;
        }
 
      
        private void calculateNewKeyAndIV()
        {
            //use salt so that key cannot be found with dictionary attack
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(mPassword,SaltByteArray);
            SymmetricAlgorithm algo = selectAlgorithm();
            mKey = pdb.GetBytes(algo.KeySize / 8);
            mIV = pdb.GetBytes(algo.BlockSize / 8);
        }
 
        #endregion
    }
 
  
    {
        #region enum, constants and fields
        //types of hashing available
        public enum HashingTypes
        {
            SHA, SHA256, SHA384, SHA512, MD5
        }
        #endregion
 
        #region static members
        public static string Hash(String inputText)
        {
            return ComputeHash(inputText,HashingTypes.MD5);
        }
 
        public static string Hash(String inputText, HashingTypes hashingType)
        {
            return ComputeHash(inputText,hashingType);
        }
 
        /// <summary>
        ///     returns true if the input text is equal to hashed text
        /// </summary>
        /// <param name="inputText">unhashed text to test</param>
        /// <param name="hashText">already hashed text</param>
        /// <returns>boolean true or false</returns>
        public static bool isHashEqual(string inputText, string hashText)
        {
            return (Hash(inputText) == hashText);
        }
 
        public static bool isHashEqual(string inputText, string hashText, HashingTypes hashingType)
        {
            return (Hash(inputText,hashingType) == hashText);
        }
        #endregion
 
        #region Hashing Engine
 
        
        private static string ComputeHash(string inputText, HashingTypes hashingType)
        {
            HashAlgorithm HA = getHashAlgorithm(hashingType);
 
            //declare a new encoder
            UTF8Encoding UTF8Encoder = new UTF8Encoding();
            //get byte representation of input text
            byte[] inputBytes = UTF8Encoder.GetBytes(inputText);
 

            //hash the input byte array
            byte[] output = HA.ComputeHash(inputBytes);
 
            //convert output byte array to a string
            return Convert.ToBase64String(output);
        }
 
    
        private static HashAlgorithm getHashAlgorithm(HashingTypes hashingType)
        {
            switch (hashingType)
            {
                case HashingTypes.MD5 :
                    return new MD5CryptoServiceProvider();
                case HashingTypes.SHA :
                    return new SHA1CryptoServiceProvider();
                case HashingTypes.SHA256 :
                    return new SHA256Managed();
                case HashingTypes.SHA384 :
                    return new SHA384Managed();
                case HashingTypes.SHA512 :
                    return new SHA512Managed();
                default :
                    return new MD5CryptoServiceProvider();
            }
        }
        #endregion
 
    }
 
   
}


Try this...:)


Encrypt and Decrypt Data with C#[^]


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

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