-
Notifications
You must be signed in to change notification settings - Fork 6
Added encrytion method #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"; | ||
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Format the document. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the line is bit off indented? Applicable to several places. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the corresponding types instead of |
||
| 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(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| string encrypted_text = Convert.ToBase64String(cipherTextBytes); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| { | ||
| SymmetricAlgorithm algo = Aes.Create(); | ||
| algo.Mode = CipherMode.CBC; | ||
| algo.Padding = PaddingMode.None; | ||
|
|
||
| return algo; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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