-2
我試圖解密存儲在Asp.Net身份「密碼」字段中的密碼。我這樣做,但出現錯誤「要解密的數據的長度無效」。密碼存儲爲「1000:salt:password」。試圖解密時出現「解密數據的長度無效」錯誤
public string DecryptPassword(string hashedPassword)
{
char[] delimiter = { ':' };
string[] split = hashedPassword.Split(delimiter);
//get byte representation of string
byte[] hash = Convert.FromBase64String(split[2]);
byte[] salt = new byte[SIZE_OF_SECRETKEY];
Rfc2898DeriveBytes pwdGen = new Rfc2898DeriveBytes(hash, salt, 1000);
RijndaelManaged _RijndaelManaged = new RijndaelManaged();
_RijndaelManaged.BlockSize = 256;
byte[] key = pwdGen.GetBytes(_RijndaelManaged.KeySize/8); //This will generate a 256 bits key
byte[] iv = pwdGen.GetBytes(_RijndaelManaged.BlockSize/8); //This will generate a 256 bits IV
_RijndaelManaged.Key = key;
_RijndaelManaged.IV = iv;
byte[] plainText2 = null;
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, _RijndaelManaged.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(hash, 0, hash.Length);
}
plainText2 = ms.ToArray();
}
//Decrypted text
return System.Text.Encoding.Unicode.GetString(plainText2);
}
散列做出與此代碼:
HashPassword(string password)
{
RNGCryptoServiceProvider csprng = new RNGCryptoServiceProvider();
byte[] salt = new byte[SIZE_OF_SECRETKEY];
csprng.GetBytes(salt);
// Hash the password and encode the parameters
byte[] hash = PBKDF2(password, salt, ITERATIONS, HASH_SIZE);
return ITERATIONS + ":" + Convert.ToBase64String(salt) + ":" + Convert.ToBase64String(hash);
}
我看到鹽和一些迭代。是什麼讓你認爲密碼是加密的而不是散列?如果它確實被散列,那麼你不能解密它。 –
如果您使用asp.net身份提供程序存儲它,爲什麼不使用身份提供程序來解密它?你確定它是一個***加密的密碼,你正在存儲,而不是***散列密碼?你不能「解密」散列密碼。 – Kritner
@CodesInChaos:是啊:)但是,這給了我答案:)編輯:不,但在這裏得到嚴肅的dejavu。 – leppie