Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions seranet/Secrets/Encrypt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using Microsoft.Extensions.Options;

namespace Workplace.Eureka.Services.Secrets
{
public class Encrypt
{
readonly string PasswordHash ="SampleHash";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inject the Secrets to the class via .net DI framework. Refer the code written Decrypt.cs

readonly string SaltKey = "SampleSalt";
readonly string VIKey = "SampleVI";



public string Decrypts(string encryptedText)
{
try
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format the document.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the line is bit off indented? Applicable to several places.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed that @kosalanuwan already pointed it out. Ignore my comment.

var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the corresponding types instead of var.

using (SymmetricAlgorithm symmetricKey = CreateAlgorithm())
{
var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));

using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
cipherTextBytes = memoryStream.ToArray();
cryptoStream.Close();
}
memoryStream.Close();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memoryStream closes automatically once the execution has done within the using statement. Not need to specifically close.

}
string encrypted_text = Convert.ToBase64String(cipherTextBytes);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider returning the converted string instead of assigning to a local variable and returning the variable instance.

return encrypted_text;
}
}
catch (Exception)
{
return null;
}
}

static SymmetricAlgorithm CreateAlgorithm()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider changing the accessibility of the method and available as an instance method instead of static. You can change this to private SymmetricAlgorithm CreateAlgorithm() instead.
OR
reuse the same logic written on Decrypt.cs

{
SymmetricAlgorithm algo = Aes.Create();
algo.Mode = CipherMode.CBC;
algo.Padding = PaddingMode.None;

return algo;
}
}
}