Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code below.please help me out or any other algorithm.

C#
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;
        }
    }
Posted
Updated 25-Oct-15 22:33pm
v2

1 solution

Don't encrypt passwords!
Hash them instead - there is some information on how to do it here: Password Storage: How to do it.[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900