Example #1
0
 public ActionResult ResetPassword(string param = null, string IV = null)
 {
     if (param != null)
     {
         ResetPasswordViewModel result = new ResetPasswordViewModel();
         CryptLib cryptLib             = new CryptLib();
         string   key = CryptLib.getHashSha256("hrkey", 31);
         string   emailtimeout = ""; string email = ""; string timeout = "";
         try
         {
             emailtimeout = cryptLib.decrypt(param, key, IV);
             string[] paramlist = emailtimeout.Split(',');
             email   = paramlist[0];
             timeout = paramlist[1];
             DateTime expiredate = Convert.ToDateTime(timeout);
             if (expiredate > DateTime.Now)
             {
                 result.Email = email;
             }
             else
             {
                 return(View("Error"));
             }
         }
         catch (Exception ex)
         {
             return(View("Error"));
         }
         return(View(result));
     }
     else
     {
         return(View("Error"));
     }
 }
Example #2
0
        public void UpdateProfile(AccountProfileModel model)
        {
            Guid userId = this.GetCurrentUserId();
            var  data   = this._repoUser.Find(userId);

            if (!string.IsNullOrEmpty(model.password))
            {
                if (string.IsNullOrEmpty(model.newPassword))
                {
                    throw new Exception("Please provide new password.");
                }

                CryptLib _crypt = new CryptLib();

                string decryptedPassword = _crypt.decrypt(data.password, data.hashKey, data.vector);
                if (decryptedPassword != model.password)
                {
                    throw new Exception("Invalid Password.");
                }

                string hashShaKey = GetSettingValue("HRIS_HASHSHA_KEY");
                string key        = CryptLib.getHashSha256(hashShaKey, 31); //32 bytes = 256 bits
                string iv         = CryptLib.GenerateRandomIV(16);
                string encrypted  = _crypt.encrypt(model.newPassword, key, iv);
                data.password = encrypted;
                data.hashKey  = key;
                data.vector   = iv;
            }

            data.email       = model.email;
            data.updatedBy   = userId;
            data.updatedDate = DateTime.Now;
            this._repoUser.Update(data);
            this._unitOfWork.Save();
        }
Example #3
0
    public static string Decrypt(string data, string key, string iv)
    {
        CryptLib _crypt = new CryptLib();

        //16 bytes = 128 bits
        key = CryptLib.getHashSha256(key, 32); //32 bytes = 256 bits
        return(_crypt.decrypt(data, key, iv));
    }
        public async Task <ActionResult <LoginResponse> > Login(Login data)
        {
            string cfg_key = _config.GetValue <string>("api_key:key");
            string json    = string.Empty;

            json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
            _log.LogInformation("Request login <==" + json);
            fData        _res     = new fData();
            ActionResult response = Unauthorized();
            CryptLib     scry     = new CryptLib();

            try
            {
                if (key == cfg_key)
                {
                    string desc   = scry.decrypt(data.password);
                    var    result = await _db.getlogin(data.username, data.password);

                    if (result != null)
                    {
                        result.token = GenerateToken(data);
                        _res         = new fData();
                        _res.data    = result;
                        _res.rc      = "00";
                        _res.desc    = "Login berhasil dilakukan";
                        response     = Ok(_res);
                    }
                    else
                    {
                        _res      = new fData();
                        _res.data = result;
                        _res.rc   = "05";
                        _res.desc = "Login gagal dilakukan";
                        response  = StatusCode(400, _res);
                    }
                }
                else
                {
                    _res      = new fData();
                    _res.rc   = "05";
                    _res.desc = "Apikey tidak sesuai";
                    response  = StatusCode(401, _res);
                }
            }
            catch (MyException ex)
            {
                _log.LogError(ex.Message.ToString());
                _res      = new fData();
                _res.rc   = "05";
                _res.desc = "Internal system Error";
                response  = StatusCode(501, _res);
            }
            string json2 = Newtonsoft.Json.JsonConvert.SerializeObject(response);

            _log.LogInformation("Response Login <== " + json2.ToString());
            return(response);
        }
		public static void Main (String []args)
		{
			CryptLib _crypt = new CryptLib ();
			string plainText = "This is the text to be encrypted";
			String iv = CryptLib.GenerateRandomIV (16); //16 bytes = 128 bits
			string key = CryptLib.getHashSha256("my secret key", 31); //32 bytes = 256 bits
			String cypherText = _crypt.encrypt (plainText, key, iv);
			Console.WriteLine ("iv="+iv);
			Console.WriteLine ("key=" + key);
			Console.WriteLine("Cypher text=" + cypherText);
			Console.WriteLine ("Plain text =" + _crypt.decrypt (cypherText, key, iv));
		}
    public static void Main(String [] args)
    {
        CryptLib _crypt     = new CryptLib();
        string   plainText  = "This is the text to be encrypted";
        String   iv         = CryptLib.GenerateRandomIV(16);               //16 bytes = 128 bits
        string   key        = CryptLib.getHashSha256("my secret key", 31); //32 bytes = 256 bits
        String   cypherText = _crypt.encrypt(plainText, key, iv);

        Console.WriteLine("iv=" + iv);
        Console.WriteLine("key=" + key);
        Console.WriteLine("Cypher text=" + cypherText);
        Console.WriteLine("Plain text =" + _crypt.decrypt(cypherText, key, iv));
    }
Example #7
0
        protected void Page_Load(object sender, EventArgs e)
        {
            string iv      = "4QesEr03HwE5H1C+ICw7SA==";                     // origional iv 128 bits
            string key     = "ovA6siPkyM5Lb9oNcnxLz676K6JK6FrJKk4efEUWBzg="; // origional key 256 bits
            string message = "Meet me at the secret location at 8pm";

            CryptLib cryptLib      = new CryptLib();
            string   encryptedText = cryptLib.encrypt(message, key, iv);

            string originalMessage = cryptLib.decrypt(encryptedText, key, iv);

            Debug.WriteLine(originalMessage);
        }
Example #8
0
        public void ValidateLogin(string companyCode, string username, string password, out Guid sessionId)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                Guid companyId = this._repoCompany.Query().Filter(x => x.code == companyCode).Get().Select(x => x.id).FirstOrDefault();

                var checkUser = this._repoUser.Query().Filter(x => x.username == username && x.companyId == companyId).Get();

                if (!checkUser.Any())
                {
                    throw new Exception("Invalid Username");
                }

                var user = checkUser.Single();

                var status = (UserStatus)user.status;

                switch (status)
                {
                case UserStatus.Disabled:
                    throw new Exception("User has been disabled");

                case UserStatus.Locked:
                    throw new Exception("User has been Locked");

                default:
                    break;
                }

                CryptLib _crypt     = new CryptLib();
                string   hashShaKey = GetSettingValue(companyId, "HRIS_HASHSHA_KEY");
                string   key        = CryptLib.getHashSha256(hashShaKey, 31); //32 bytes = 256 bits
                                                                              //string encrypted = _crypt.encrypt(password, key, iv);
                string decrypt = _crypt.decrypt(user.password, user.hashKey, user.vector);

                if (decrypt != password)
                {
                    throw new Exception("Invalid Password");
                }

                sessionId = this.CreateUserSession(companyId, user.id);

                if (status == UserStatus.ResetPassword)
                {
                    this.UpdateStatus(user.id, UserStatus.Active);
                }

                ts.Complete();
            }
        }
Example #9
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("Hello World again 12!");

            CryptLib _crypt     = new CryptLib();
            string   plainText  = "This is the text to be encrypted.";
            String   iv         = "4NAfcTL5nWERGSLl";                          //CryptLib.GenerateRandomIV(16); //16 bytes = 128 bits
            string   key        = CryptLib.getHashSha256("my secret key", 32); //32 bytes = 256 bits
            String   cypherText = _crypt.encrypt(plainText, key, iv);

            Console.WriteLine("isv=" + iv);
            Console.WriteLine("key=" + key);
            Console.WriteLine("Cypher text=" + cypherText);
            Console.WriteLine("Plain text =" + _crypt.decrypt(cypherText, key, iv));
        }
Example #10
0
        public string Decrypt(string encryptedStr)
        {
            string decryptedStr = _CryptLib.decrypt(encryptedStr, keySha256, iv);

            return(decryptedStr);
        }