using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Dapper;
using System.Dynamic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Text;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Hosting.Server;
using System.IO;
using System.Security.Cryptography;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
namespace yc_api.Controllers
{
[Route("restful/[controller]")]
[ApiController]
public class RpcController : ControllerBase
{
private ILogger<RpcController> _logger;
private readonly IConfiguration _configuration;
public RpcController(ILogger<RpcController> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}
[EnableCors("cors")]
[HttpPost]
public ActionResult Post([FromBody] JObject requestData)
{
dynamic rv = new ExpandoObject();
rv.success = false;
try
{
string encryptedData = requestData["encryptedData"].ToString();
if (string.IsNullOrEmpty(encryptedData))
{
return BadRequest("Encrypted data is missing.");
}
string decryptedJson = AesEncryption.Decrypt(encryptedData);
var data = JsonConvert.DeserializeObject(decryptedJson);
return Ok(data);
}
catch (CryptographicException ex)
{
return BadRequest($"Decryption error: {ex.Message}");
}
catch (Exception ex)
{
return BadRequest($"Error: {ex.Message}");
}
return new JsonResult(rv);
}
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "value";
}
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
[HttpDelete("{id}")]
public void Delete(int id)
{
}
public class AesEncryption
{
private static readonly byte[] Key = Convert.FromBase64String("");
private static readonly byte[] IV = new byte[16];
public static string Encrypt(string plainText)
{
using (var aes = Aes.Create())
{
aes.Key = Key;
aes.IV = IV;
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
using (var ms = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
using (var writer = new StreamWriter(cryptoStream))
{
writer.Write(plainText);
}
}
return Convert.ToBase64String(ms.ToArray());
}
}
}
public static string Decrypt(string cipherText)
{
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (var aes = Aes.Create())
{
aes.Key = Key;
aes.IV = Encoding.UTF8.GetBytes("");
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
using (var ms = new MemoryStream(cipherBytes))
{
using (var cryptoStream = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read))
{
using (var reader = new StreamReader(cryptoStream))
{
return reader.ReadToEnd();
}
}
}
}
}
}
}
}