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();
public IQueryable<user> GetUsers()
{
return db.Users;
}
SymmetricAlgorithm desobj = Rijndael.Create();
string key;
string ciphereData;
byte[] chipherbytes;
byte[] plainbytes;
byte[] plainbytes2;
byte[] plainKey;
[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);
}
[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);
}
[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);
}
[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;
}
}