在解密密文时获取Base-64 char数组的无效长度

时间:2015-09-06 16:07:58

标签: c# encryption encoding

尝试解密使用base64加密的cipherText密码时,我收到“Base-64 char数组的长度无效”。但是在解密时它会在解密函数内引发错误消息。请帮我克服这个问题。提前感谢你。

protected void Save(object sender, EventArgs e)
{
        //Save into Service Table 

        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "insert_user";
        cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = txtUsername.Text.Trim();
        cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value = Encrypt(txtPassword.Text.Trim());
        cmd.Connection = con;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            lblMsg.Text = "Record inserted successfully";
        }

        catch (Exception ex)
        {
            throw ex;
        }

        finally
        {
            con.Close();
            con.Dispose();
        }
}

protected void Login(object sender, EventArgs e)
{
    string password;
    string decryptPassword;

    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "get_username";
    cmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = txtUserName.Text.Trim();
    cmd.Connection = con;

    try
    {
        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        rdr.Read();
        password = rdr["Password"].ToString();

    }

    catch (Exception ex)
    {
        throw ex;
    }

    finally
    {
        con.Close();
        con.Dispose();
    }  
    decryptPassword = Decrypt(password);
  }

加密功能:

public string Encrypt(string clearText)
{
    string EncryptionKey = "MAKV2SPBNI99212";
    byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
    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.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(clearBytes, 0, clearBytes.Length);
                cs.Close();
            }
            clearText = Convert.ToBase64String(ms.ToArray());
        }
    }
    return clearText;
}

解密功能:

public string Decrypt(string cipherText)
{
    string EncryptionKey = "MAKV2SPBNI99212";
    byte[] cipherBytes = System.Convert.FromBase64String(cipherText);     //Error: "Invalid length for a Base-64 char array or string."
    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;
}

0 个答案:

没有答案