输入数据不是一个完整的块。 AES解密

时间:2014-12-13 15:30:16

标签: c# database encryption aes

我正在尝试使用AES加密/解密数据库。但我有一些问题。 "输入数据不是一个完整的块。" 这是我的Contact.cs类:

解密

    private string Decrypt(string cipherText)
    {         
        string EncryptionKey = "MAKV2SPBNI99212";
        cipherText= cipherText.Replace(" ", "+");
        int mod4 = cipherText.Length % 4;
        if ( mod4 > 0 )
        {
           cipherText += new string( '=', 4 - mod4 );
        }

        //cipherText = cipherText.Replace(" ", "+");
      //  byte[] cipherBytes = Convert.FromBase64String(cipherText/*.Replace(" ", "+")*/);
        byte[] cipherBytes = System.Convert.FromBase64String(cipherText);
        string Convert = System.Text.Encoding.UTF8.GetString(cipherBytes);
        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(); //The input data is not a complete block.
                }
                cipherText = Encoding.Unicode.GetString(ms.ToArray());
            }
        }
        return cipherText;
    }

加密

private 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 _name;
    [NotMapped]
    public string Name
    {

        get
        {

            return Decrypt(_name);
        }
        set
        {
            _name = Encrypt(value);

        }
    }



    public class ContactConfig : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Contact>
    {
        public ContactConfig()
        {
            Property(b => b.ValueForDB);
        }
    }



    [Column("Name")]

    private string ValueForDB
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }

    public int ContactId { get; set; }

    public string Address { get; set; }

    public string City { get; set; }

    public string State { get; set; }

它是我的IdentityModel.cs类:

公共类ApplicationUser:IdentityUser

{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>  
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public System.Data.Entity.DbSet<ContactManager.Models.Contact> Contacts { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // model definition is additive so other declaration will be applied on top rather than replacing this one
        modelBuilder.Configurations.Add(new Contact.ContactConfig());

        // no harm leaving this in as EF binding is additive
        base.OnModelCreating(modelBuilder);
    }
}

我在Azure上尝试加密数据库。我的项目是:http://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-deploy-aspnet-mvc-app-membership-oauth-sql-database/

感谢您的帮助!

0 个答案:

没有答案