コード例 #1
0
        public async Task <string> EncryptAsync(EncryptionModel model)
        {
            byte[] iv = new byte[16];
            byte[] array;

            using (Aes aes = Aes.Create())
            {
                aes.Key = Encoding.UTF8.GetBytes(_encryptionKeyService.GetKey());
                aes.IV  = iv;

                ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter streamWriter = new StreamWriter((Stream)cryptoStream))
                        {
                            await streamWriter.WriteAsync(model.Secret);
                        }

                        array = memoryStream.ToArray();
                    }
                }
            }

            return(Convert.ToBase64String(array));
        }
コード例 #2
0
        public string changePassword([FromBody] TamuModel tamu)
        {
            try
            {
                connection = new SqlConnection(ConnectionModel.connectionString);
                connection.Open();

                string email          = tamu.email;
                string password       = EncryptionModel.encryption(tamu.password);
                string passwordRepeat = EncryptionModel.encryption(tamu.confirm);

                command = new SqlCommand("SELECT * FROM tamu WHERE email = '" + email + "' AND password LIKE '" + password + "'", connection);
                reader  = command.ExecuteReader();
                reader.Read();
                if (reader.HasRows)
                {
                    reader.Close();

                    command = new SqlCommand("UPDATE tamu SET password = '******' WHERE email = '" + email + "' AND password LIKE '" + password + "'", connection);
                    command.ExecuteReader();
                    connection.Close();

                    return("Success");
                }
                else
                {
                    reader.Close();
                    connection.Close();
                    return("Fail");
                }
            } catch (Exception)
            {
                return("Fail");
            }
        }
コード例 #3
0
        public async Task <IActionResult> Encrypt(EncryptionModel model)
        {
            var result = new ExpandoObject();

            result.TryAdd("data", await _encryptionService.EncryptAsync(model));
            return(Ok(result));
        }
コード例 #4
0
        public async Task <IActionResult> decrypt(EncryptionModel model)
        {
            var service = EncryptionService.DecryptString(model.text);

            var response = new GenericResponse <string>()
            {
                IsSuccess    = true,
                Message      = "Bucket created successfully.",
                ResponseCode = 200,
                Result       = service
            };

            return(Ok(response));
        }
コード例 #5
0
        public JsonResult SymmetricEncrypt(WebSecurity.Models.EncryptionModel encryption, string ButtonType)
        {
            var result = new EncryptionModel();

            var symEncryption = new SymmetricEncryption();

            SymmetricAlgorithm symAlgo = new AesManaged();

            symAlgo.Padding = PaddingMode.PKCS7;
            var key = string.Empty;
            var iv  = string.Empty;

            if (!string.IsNullOrEmpty(encryption.PrivateKey) && !string.IsNullOrEmpty(encryption.InitializationVector))
            {
                symAlgo.Key = Convert.FromBase64String(encryption.PrivateKey);
                symAlgo.IV  = Convert.FromBase64String(encryption.InitializationVector);
            }
            else
            {
                key = Convert.ToBase64String(symAlgo.Key);
                iv  = Convert.ToBase64String(symAlgo.IV);
            }

            if (ButtonType == "Encrypt")
            {
                var encryptedText = symEncryption.EncryptText(symAlgo, encryption.PlainText);
                result = new EncryptionModel()
                {
                    EncryptedText        = encryptedText,
                    PlainText            = encryption.PlainText,
                    InitializationVector = iv,
                    PrivateKey           = key
                };
            }
            else if (ButtonType == "Decrypt")
            {
                var plainText = symEncryption.DecryptData(symAlgo, encryption.EncryptedText);

                result = new EncryptionModel()
                {
                    EncryptedText        = encryption.EncryptedText,
                    PlainText            = plainText,
                    PrivateKey           = encryption.PrivateKey,
                    InitializationVector = encryption.InitializationVector
                };
            }

            return(Json(result));
        }
コード例 #6
0
        public async Task <string> EncryptAsync(EncryptionModel model)
        {
            try
            {
                var response = await _apiClient.Client.PostAsJsonAsync("encrypt", model);

                var result = await response.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <string>(result));
            }
            catch (Exception)
            {
                throw new ApiException("Encryption service is unavailable");
            }
        }
コード例 #7
0
        public JsonResult AsymmetricEncrypt(WebSecurity.Models.EncryptionModel encryption, string ButtonType)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            string publicKey  = string.Empty; // false to get the public key
            string privateKey = string.Empty; // true to get the private key

            if (!string.IsNullOrEmpty(encryption.PublicKey) && !string.IsNullOrEmpty(encryption.PrivateKey))
            {
                publicKey  = HttpUtility.HtmlDecode(encryption.PublicKey);
                privateKey = HttpUtility.HtmlDecode(encryption.PrivateKey);
            }
            else
            {
                publicKey  = rsa.ToXmlString(false); // false to get the public key
                privateKey = rsa.ToXmlString(true);  // true to get the private key
            }

            var result = new EncryptionModel();

            var asymEncryption = new AsymmetricEncryption();

            if (ButtonType == "Encrypt")
            {
                var encryptedText = asymEncryption.EncryptText(publicKey, encryption.PlainText);
                result = new EncryptionModel()
                {
                    EncryptedText = encryptedText,
                    PlainText     = encryption.PlainText,
                    PublicKey     = HttpUtility.HtmlEncode(publicKey),
                    PrivateKey    = HttpUtility.HtmlEncode(privateKey)
                };
            }
            else if (ButtonType == "Decrypt")
            {
                var plainText = asymEncryption.DecryptData(HttpUtility.HtmlDecode(encryption.PrivateKey), encryption.EncryptedText);

                result = new EncryptionModel()
                {
                    EncryptedText = encryption.EncryptedText,
                    PlainText     = plainText,
                    PublicKey     = HttpUtility.HtmlEncode(publicKey),
                    PrivateKey    = HttpUtility.HtmlEncode(privateKey)
                };
            }

            return(Json(result));
        }
コード例 #8
0
        public TamuModel login([FromBody] TamuModel tamu)
        {
            try
            {
                connection = new SqlConnection(ConnectionModel.connectionString);
                connection.Open();

                string email    = tamu.email;
                string password = EncryptionModel.encryption(tamu.password);

                command = new SqlCommand("SELECT * FROM tamu WHERE email = '" + email + "' AND password LIKE '" + password + "'", connection);
                reader  = command.ExecuteReader();
                reader.Read();

                if (reader.HasRows)
                {
                    tamu.id       = Convert.ToInt32(reader[0]);
                    tamu.nik      = Convert.ToString(reader[1]);
                    tamu.nama     = Convert.ToString(reader[2]);
                    tamu.email    = Convert.ToString(reader[3]);
                    tamu.nohp     = Convert.ToString(reader[4]);
                    tamu.alamat   = Convert.ToString(reader[5]);
                    tamu.password = Convert.ToString(reader[6]);

                    connection.Close();

                    return(tamu);
                }
                else
                {
                    connection.Close();
                    return(null);
                }
            } catch (Exception)
            {
                return(null);
            }
        }
コード例 #9
0
 public async Task <IActionResult> Encrypt([FromBody] EncryptionModel model)
 {
     return(Ok(await _encryptionService.EncryptAsync(model)));
 }