我必须以加密格式将密码字段存储在SQL Server数据库中,我必须在用户登录系统时对其进行解密。加密部分工作正常。但我在解密部分得到错误" Base-64字符数组的长度无效"在
行byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
解密模块。
private string Encryptdata(string password)
{
string encryptpwd = string.Empty;
byte[] encode = new byte[password.Length];
encode = Encoding.UTF8.GetBytes(password);
encryptpwd = Convert.ToBase64String(encode);
return encryptpwd;
}
private string Decryptdata(string encryptpwd)
{
string decryptpwd = string.Empty;
UTF8Encoding encodepwd = new UTF8Encoding();
Decoder Decode = encodepwd.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(encryptpwd); //here I am getting error as "Invalid length for a Base-64 char array"
int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
decryptpwd = new String(decoded_char);
return decryptpwd;
}
输入数据:prabu
加密数据:cHJhYnU=
答案 0 :(得分:0)
您遇到错误,因为您的代码是这样的:
string password = "prabu";
string encryptdata = Encryptdata(password);
string decryptdata = Decryptdata(password);
答案 1 :(得分:0)
我们使用将密码保存为盐渍哈希,而不是保存加密的用户密码,而是将密码保存为盐渍哈希,每次存储新密码时都会自动生成盐(盐和哈希存储在数据库)。
要对登录尝试进行身份验证,我们会为登录期间提供的密码生成哈希值,但使用最初设置密码时存储的salt。然后验证登录,只需比较两个哈希值。
例如,如果选择SHA1哈希函数:
using System;
using System.Security.Cryptography;
public interface ISaltedHash
{
/// <summary>
/// Gets the hash.
/// </summary>
string Hash
{
get;
}
/// <summary>
/// Gets the salt.
/// </summary>
string Salt
{
get;
}
}
public class SaltedHashProvider
{
#region Fields
private int m_saltLength = 6;
#endregion // Fields
#region Public Methods
/// <summary>
/// Encrypts data with the a salted SHA1 algorith.
/// The salt will be automatically generated.
/// </summary>
/// <param name="value">Value to be encrypted.</param>
/// <returns>The encrypted data.</returns>
public ISaltedHash EncryptWithSalt( string value )
{
string salt = CreateSalt();
string hash = Encrypt( salt + value );
return new SaltedHash
{
Hash = hash,
Salt = salt
};
}
/// <summary>
/// Encrypts data with the a salted SHA1 algorith.
/// </summary>
/// <param name="value">Value to be encrypted.</param>
/// <param name="salt">Salt to be used when encypting the value.</param>
/// <returns>The encrypted data.</returns>
public ISaltedHash EncryptWithSalt( string value, string salt )
{
string hash = Encrypt( salt + value );
return new SaltedHash
{
Hash = hash,
Salt = salt
};
}
#endregion // Public Methods
#region Helper Methods
/// <summary>
/// Creates salt.
/// </summary>
/// <returns>A base64 salt string.</returns>
private string CreateSalt()
{
byte[] saltBlob = CreateRandomBytes(m_saltLength);
return Convert.ToBase64String(saltBlob);
}
/// <summary>
/// Encrypts data with the SHA1 algorithm.
/// </summary>
/// <param name="value">Value to be encrypted.</param>
/// <returns>The encrypted data.</returns>
private string Encrypt( string value )
{
byte[] blob = ToByteArray( value );
byte[] hash = ComputeHash( blob );
return Convert.ToBase64String( hash );
}
/// <summary>
/// Computes the hash value for the specified byte array.
/// </summary>
/// <param name="blob">The input to commute the hash for.</param>
/// <returns>The computed hash code.</returns>
private byte[] ComputeHash( byte[] blob )
{
return new SHA1CryptoServiceProvider().ComputeHash( blob );
}
/// <summary>
/// Gets a UTF8 byte array encoding for the specified character array.
/// </summary>
/// <param name="value">The input containing characters to be encoded.</param>
/// <returns>The UTF8 encoded array.</returns>
private byte[] ToByteArray( string value )
{
return System.Text.Encoding.UTF8.GetBytes( value );
}
/// <summary>
/// Creates a random byte array.
/// </summary>
/// <param name="length">Length of array to be generated.</param>
/// <returns>A random byte array.</returns>
private static byte[] CreateRandomBytes( int length )
{
byte[] blob = new byte[length];
new RNGCryptoServiceProvider().GetBytes( blob );
return blob;
}
#endregion // Helper Methods
}
答案 2 :(得分:0)
private string Decrypt(string cipherText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}