-
-
Notifications
You must be signed in to change notification settings - Fork 3
Add Simple AES Class #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
3a288ae
Add AES Class Simple
2904514
add .gitignore
ad5eb57
add intellisense comments
4d59382
add .vs in gitignore
609c94c
adjust assemblyInfo
aabe952
add header for aes class
5ed76a4
add header for assemblyinfo
757096f
add packge.lock
9ce5ece
Merge branch 'main' into main
AlirezaP b4353b4
restruct solution
7c19296
remove wrong solution
c5af000
Remove .vs folder
bd888fd
Remove packge folder
8809532
rename EncryptionModes to CipherMode
9ed4355
chanfe class intellisesne comments
6753b31
change Block to block (word)
87a7788
Update README.md
AlirezaP d228673
add intellisesne comments for ECB
39f1618
Merge branch 'main' of https://github.com/AlirezaP/System.Security.Cr…
ecc437a
Update README.md
AlirezaP 17526b9
Add a Simple TestCase For AES
3d5a953
Merge branch 'main' of https://github.com/AlirezaP/System.Security.Cr…
dbb3239
Revert line ending removal
josesimoes 14a23b6
Rename file for case consistency
josesimoes a7fb65a
Remove empty lines
josesimoes cbf7161
Merge branch 'main' into main
josesimoes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| // | ||
| // Copyright (c) .NET Foundation and Contributors | ||
| // See LICENSE file in the project root for full license information. | ||
| // | ||
|
|
||
| using System; | ||
|
|
||
| namespace System.Security.Cryptography | ||
| { | ||
| /// <summary> | ||
| /// Specifies the block cipher mode to use for encryption. | ||
| /// </summary> | ||
| public enum CipherMode | ||
| { | ||
| /// <summary> | ||
| /// The Electronic Codebook (ECB) mode encrypts each block individually. Any blocks | ||
| /// of plain text that are identical and in the same message, or that are in a different | ||
| /// message encrypted with the same key, will be transformed into identical cipher | ||
| /// text blocks. Important: This mode is not recommended because it opens the door | ||
| /// for multiple security exploits. If the plain text to be encrypted contains substantial | ||
| /// repetition, it is feasible for the cipher text to be broken one block at a time. | ||
| /// It is also possible to use block analysis to determine the encryption key. Also, | ||
| /// an active adversary can substitute and exchange individual blocks without detection, | ||
| /// which allows blocks to be saved and inserted into the stream at other points | ||
| /// without detection. | ||
| /// </summary> | ||
| ECB = 2 | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Encryption Standard (AES) | ||
| /// </summary> | ||
| public class AES | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the mode for operation of the symmetric algorithm. | ||
| /// </summary> | ||
| /// <returns>The mode for operation of the symmetric algorithm. The default is System.Security.Cryptography.CipherMode.ECB.</returns> | ||
| public virtual CipherMode Mode { get; set; } = CipherMode.ECB; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the System.Security.Cryptography.Aes class. | ||
| /// </summary> | ||
| public AES() | ||
| { | ||
|
|
||
| } | ||
| /// <summary> | ||
| /// Encrypt the array of bytes. | ||
| /// </summary> | ||
| /// <param name="key">The secret key to use for the symmetric algorithm.</param> | ||
| /// <param name="data">The array of byte for encryption</param> | ||
| /// <returns>The encrypted array of bytes</returns> | ||
| public byte[] Encrypt(byte[] key, byte[] data) | ||
| { | ||
| byte[] buf = null; | ||
|
|
||
| if (Mode == CipherMode.ECB) | ||
| { | ||
| buf = EncryptAesEcb(key, data); | ||
| } | ||
|
|
||
| return buf; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Decrypt the array of bytes. | ||
| /// </summary> | ||
| /// <param name="key">The secret key to use for the symmetric algorithm.</param> | ||
| /// <param name="data">The encrypted array of byte for decryption</param> | ||
| /// <returns>The decrypted array of bytes</returns> | ||
| public byte[] Decrypt(byte[] key, byte[] data) | ||
| { | ||
| byte[] buf = null; | ||
|
|
||
| if (Mode == CipherMode.ECB) | ||
| { | ||
| buf = DecryptAesEcb(key, data); | ||
| } | ||
|
|
||
| return buf; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Encrypt the array of bytes In ECB Mode | ||
| /// </summary> | ||
| /// <param name="key">The secret key to use for the symmetric algorithm.</param> | ||
| /// <param name="data">Array of byte for encryption</param> | ||
| /// <returns>The encrypted array of bytes</returns> | ||
| private byte[] EncryptAesEcb(byte[] key, byte[] data) | ||
| { | ||
| int blockSize = 16; // AES block size is 128 bits (16 bytes) | ||
| int blockCount = data.Length / blockSize; | ||
| int remainder = data.Length % blockSize; | ||
|
|
||
| byte[] encryptedData = new byte[data.Length]; | ||
|
|
||
| for (int i = 0; i < blockCount; i++) | ||
| { | ||
| byte[] block = new byte[blockSize]; | ||
| Array.Copy(data, i * blockSize, block, 0, blockSize); | ||
| EncryptBlock(key, block); | ||
| Array.Copy(block, 0, encryptedData, i * blockSize, blockSize); | ||
| } | ||
|
|
||
| // If there is a remainder, pad the last block and encrypt | ||
| if (remainder > 0) | ||
| { | ||
| byte[] lastBlock = new byte[blockSize]; | ||
| Array.Copy(data, blockCount * blockSize, lastBlock, 0, remainder); | ||
| EncryptBlock(key, lastBlock); | ||
| Array.Copy(lastBlock, 0, encryptedData, blockCount * blockSize, remainder); | ||
| } | ||
|
|
||
| return encryptedData; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// XOR the block of data with key | ||
| /// </summary> | ||
| /// <param name="key">The secret key to use for the symmetric algorithm.</param> | ||
| /// <param name="block">The block of data for XOR opration with secret key</param> | ||
| /// <exception cref="ArgumentException">Key and block must have the same length.</exception> | ||
| private void EncryptBlock(byte[] key, byte[] block) | ||
| { | ||
| // Ensure that the key and block have the same length | ||
| if (key.Length != block.Length) | ||
| { | ||
| throw new ArgumentException(); | ||
| } | ||
|
|
||
| for (int i = 0; i < block.Length; i++) | ||
| { | ||
| block[i] = (byte)(block[i] ^ key[i]); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Decrypt the array of bytes In ECB Mode | ||
| /// </summary> | ||
| /// <param name="key">The secret key to use for the symmetric algorithm.</param> | ||
| /// <param name="data">The encrypted array of byte for decryption</param> | ||
| /// <returns>The decrypted array of bytes</returns> | ||
| private byte[] DecryptAesEcb(byte[] key, byte[] data) | ||
| { | ||
| int blockSize = 16; // AES block size is 128 bits (16 bytes) | ||
| int blockCount = data.Length / blockSize; | ||
| int remainder = data.Length % blockSize; | ||
|
|
||
| byte[] decryptedData = new byte[data.Length]; | ||
|
|
||
| for (int i = 0; i < blockCount; i++) | ||
| { | ||
| byte[] block = new byte[blockSize]; | ||
| Array.Copy(data, i * blockSize, block, 0, blockSize); | ||
| DecryptBlock(key, block); | ||
| Array.Copy(block, 0, decryptedData, i * blockSize, blockSize); | ||
| } | ||
|
|
||
| // If there is a remainder, pad the last block and decrypt | ||
| if (remainder > 0) | ||
| { | ||
| byte[] lastBlock = new byte[blockSize]; | ||
| Array.Copy(data, blockCount * blockSize, lastBlock, 0, remainder); | ||
| DecryptBlock(key, lastBlock); | ||
| Array.Copy(lastBlock, 0, decryptedData, blockCount * blockSize, remainder); | ||
| } | ||
|
|
||
| return decryptedData; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// XOR the block of data with key | ||
| /// </summary> | ||
| /// <param name="key">The secret key to use for the symmetric algorithm.</param> | ||
| /// <param name="block">The block of data for XOR opration with secret key</param> | ||
| /// <exception cref="ArgumentException">Key and block must have the same length.</exception> | ||
| private void DecryptBlock(byte[] key, byte[] block) | ||
| { | ||
| // Ensure that the key and block have the same length | ||
| if (key.Length != block.Length) | ||
| { | ||
| throw new ArgumentException(); | ||
| } | ||
|
|
||
| for (int i = 0; i < block.Length; i++) | ||
| { | ||
| block[i] = (byte)(block[i] ^ key[i]); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // | ||
| // Copyright (c) .NET Foundation and Contributors | ||
| // Portions Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // See LICENSE file in the project root for full license information. | ||
| // | ||
|
|
||
| using nanoFramework.TestFramework; | ||
| using System.Security.Cryptography; | ||
|
|
||
| namespace System.Security.CryptographyTests | ||
| { | ||
| [TestClass] | ||
| public class AESTests | ||
| { | ||
| static byte[] cipherDataArray = new byte[] { 112, 15, 93, 166, 173, 66, 95, 251, 63, 172, 69, 69, 182, 109, 13, 93 }; | ||
| static byte[] clearDataArray = new byte[] { 78, 97, 110, 111, 102, 114, 97, 109, 101, 119, 111, 114, 107, 0, 0, 0 }; | ||
| static byte[] key = new byte[16] { 62, 110, 51, 201, 203, 48, 62, 150, 90, 219, 42, 55, 221, 109, 13, 93 }; | ||
|
|
||
| [TestMethod] | ||
| public void TestAesECBEncryptionAndDecryption() | ||
| { | ||
| OutputHelper.WriteLine($"Test Case: ECB Encryption/Decryption"); | ||
|
|
||
| AES aes = new AES(); | ||
| aes.Mode = CipherMode.ECB; | ||
|
|
||
| byte[] clearTextByteArrayWithPadding = clearDataArray; | ||
|
|
||
| // Encrypt the bytes | ||
| var enData = aes.Encrypt(key, clearTextByteArrayWithPadding); | ||
| CollectionAssert.AreEqual(cipherDataArray,enData); | ||
|
|
||
| // Decrypt the bytes | ||
| var decryptedByteArray = aes.Decrypt(enData, key); | ||
| CollectionAssert.AreEqual(clearDataArray, decryptedByteArray); | ||
|
|
||
josesimoes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.