我想加密和解密,但是出现一个错误(“要解密的数据的长度是>无效”。) [英] I want to encrypt and decrypt but one error comes ("Length of the data to decrypt is > invalid".)

查看:126
本文介绍了我想加密和解密,但是出现一个错误(“要解密的数据的长度是>无效”。)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我下面的代码。请帮我或任何其他算法。



This is my code below.please help me out or any other algorithm.

using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using WebApplication5.Models;
using System.Security.Cryptography;
using System.Text;

namespace WebApplication5.Controllers
{
    public class UsersController : ApiController
    {
        private ChatDatabaseEntities1 db = new ChatDatabaseEntities1();

        // GET: api/Users
        public IQueryable<user> GetUsers()
        {
            return db.Users;
        }
        SymmetricAlgorithm desobj = Rijndael.Create();
        string key;
        string ciphereData;
        byte[] chipherbytes;
        byte[] plainbytes;
        byte[] plainbytes2;
        byte[] plainKey;

        // GET: api/Users/5
        [ResponseType(typeof(User))]
        public IHttpActionResult GetUser(string Email,string password)
        {
            User user = db.Users.Find(Email);
            if (user == null)
            {
                return NotFound();
            }
            string temp = decrypter(user.Password, user.PasswordSalt);
            if (password == temp)
            {
              //  return Ok(user.Email);
            }


            return Ok( user.Email);
        }


            
        

        // PUT: api/Users/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutUser(string id, User user)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != user.Email)
            {
                return BadRequest();
            }

            db.Entry(user).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Users
        [ResponseType(typeof(User))]
        public IHttpActionResult PostUser(User user)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            string temp_password = Encryptdata("Text");
            user.Password = temp_password;
            user.PasswordSalt = Key();
            user.UserType = "user";

            db.Users.Add(user);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (UserExists(user.Email))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = user.Email }, user);
        }

        // DELETE: api/Users/5
        [ResponseType(typeof(User))]
        public IHttpActionResult DeleteUser(string id)
        {
            User user = db.Users.Find(id);
            if (user == null)
            {
                return NotFound();
            }

            db.Users.Remove(user);
            db.SaveChanges();

            return Ok(user);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool UserExists(string id)
        {
            return db.Users.Count(e => e.Email == id) > 0;
        }
        private string Encryptdata(string plaintext)
        {
            ciphereData = plaintext;
            plainbytes = Encoding.ASCII.GetBytes(ciphereData);
            string GN = Key();
            plainKey = Encoding.ASCII.GetBytes(GN);
            desobj.Key = plainKey;
            desobj.Mode = CipherMode.CBC;
            desobj.Padding = PaddingMode.PKCS7;
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, desobj.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(plainbytes, 0, plainbytes.Length);
            cs.Close();
            chipherbytes = ms.ToArray();
            ms.Close();
            string encryptedpassword = Encoding.ASCII.GetString(chipherbytes);
            return encryptedpassword;
        }

        private string Key()
        {
            Random random = new Random();
            key = "" + random.Next(0, 9) + random.Next(0, 9) + random.Next(0, 9) + random.Next(0, 9) + random.Next(0, 9) + random.Next(0, 9) + random.Next(0, 9) + random.Next(0, 9) + random.Next(0, 9) + random.Next(0, 9) + random.Next(0, 9) + random.Next(0, 9) + random.Next(0, 9) + random.Next(0, 9) + random.Next(0, 9) + random.Next(0, 9);

            return key;
        }

        private string decrypter(string password, string temp1_password)
        {
            byte[] chipherbytes = Convert.FromBase64String(temp1_password);
           
            System.IO.MemoryStream ms1 = new System.IO.MemoryStream(chipherbytes);
            CryptoStream cs1 = new CryptoStream(ms1, desobj.CreateDecryptor(), CryptoStreamMode.Read);
            cs1.Read(chipherbytes, 0, chipherbytes.Length);

            plainbytes2 = ms1.ToArray();
            cs1.Close();
            ms1.Close();
            string decrypt = Encoding.ASCII.GetString(plainbytes2);

            string temp_decrypt = decrypt.Substring(0,chipherbytes.Length);

            return temp_decrypt;
        }
    }

推荐答案

不加密密码!

哈希他们相反 - 这里有一些关于如何做的信息:密码存储:怎么做。 [ ^ ]


这篇关于我想加密和解密,但是出现一个错误(“要解密的数据的长度是&gt;无效”。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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