This is the view. Upon submit, the data is encrypted. No problem.:
<form asp-controller="Permit" asp-action="Encryptori" method="post">
<div class="form-group">
<label for="Name">Name:</label>
<input type="text" id="Name" name="Name" value="Roberto" class="form-control" />
</div>
<div class="form-group">
<label for="Address">Address:</label>
<input type="text" id="Address" name="Address" value="Viale Manfredo Fanti, 4 (50137) Firenze" class="form-control" />
</div>
<br />
<button type="submit" class="btn btn-primary" onclick="encryptAndSubmit()">Encrypt and Submit</button>
<br /><br />
@ViewBag.Display
<input type="hidden" id="Key" name="Key" value="@ViewBag.Key" />
<input type="hidden" id="IV" name="IV" value="@ViewBag.IV" />
This is the script in the View:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/crypto-js.min.js"></script>
<script>
function encryptAndSubmit() {
var name = document.getElementById("Name").value;
var address = document.getElementById("Address").value;
var key = "@ViewBag.Key";
var iv = "@ViewBag.IV";
var encryptedName = encrypt(name, key, iv);
var encryptedAddress = encrypt(address, key, iv);
document.getElementById("Name").value = encryptedName;
document.getElementById("Address").value = encryptedAddress;
}
function encrypt(plainText, key, iv) {
var keyBytes = CryptoJS.enc.Utf8.parse(key);
var ivBytes = CryptoJS.enc.Utf8.parse(iv);
var encrypted = CryptoJS.AES.encrypt(plainText, keyBytes, {
iv: ivBytes,
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
}
And the controller.:
private readonly byte[] _key;
private readonly byte[] _iv;
public PermitController()
{
var rng = new RNGCryptoServiceProvider();
_key = new byte[32];
rng.GetBytes(_key);
_iv = new byte[16];
rng.GetBytes(_iv);
}
public async Task<IActionResult> Encryptori()
{
ViewBag.Display = "";
ViewBag.Key = Convert.ToBase64String(_key);
ViewBag.IV = Convert.ToBase64String(_iv);
ApplicationUser user = await _userManager.GetUserAsync(User);
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Encryptori(IFormCollection collection)
{
string TheKey = collection["Key"];
string TheIv = collection["IV"];
var keyBytes = Convert.FromBase64String(TheKey);
var ivBytes = Convert.FromBase64String(TheIv);
string encryptedName = collection["Name"];
string encryptedAddress = collection["Address"];
var decryptedName = Decrypt(encryptedName, TheKey, TheIv);
var decryptedAddress = Decrypt(encryptedAddress, TheKey, TheIv);
// Store the decrypted data in the ViewBag
ViewBag.Display = encryptedName + " and " + encryptedAddress;
ApplicationUser user = await _userManager.GetUserAsync(User);
return View();
}
private string Decrypt(string encryptedText, string key, string iv)
{
try
{
var keyBytes = Convert.FromBase64String(key);
var ivBytes = Convert.FromBase64String(iv);
using (var aes = Aes.Create())
{
aes.Key = keyBytes;
aes.IV = ivBytes;
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.ECB;
var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
{
var encryptedBytes = Convert.FromBase64String(encryptedText);
cs.Write(encryptedBytes, 0, encryptedBytes.Length);
}
ms.Position = 0;
var decryptedBytes = new byte[ms.Length];
ms.Read(decryptedBytes, 0, decryptedBytes.Length);
return Encoding.UTF8.GetString(decryptedBytes);
}
}
}
catch (CryptographicException ex)
{
Console.WriteLine("Decryption failed: " + ex.Message);
return null;
}
}
I tried changing different padding mode, but still the same. Still encounter the System.Security.Cryptography.CryptographicException: ‘Padding is invalid and cannot be removed. Need help.