Last active
July 24, 2024 08:59
-
-
Save bronze1man/68f88494c88eb3fc647c447b36138134 to your computer and use it in GitHub Desktop.
c# aes ctr implement works on vs2017/uwp , seen testRun for usage example.
This file contains 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
using System.Security.Cryptography; | |
using System.Diagnostics; | |
using System; | |
using System.Runtime.InteropServices.WindowsRuntime; | |
namespace abc { | |
public sealed class AesCtr { | |
public static void testRun() { | |
for (var i2 = 0; i2 < 3; i2++) { | |
var outputV = Encrypt(new byte[32], new byte[16], new byte[30]); | |
for (var i = 0; i < 30; i++) { | |
Debug.WriteLine("runTest " + i.ToString() + " " + outputV[i].ToString()); | |
} | |
} | |
} | |
public static byte[] Encrypt([ReadOnlyArray]byte[] psk, [ReadOnlyArray]byte[] iv, [ReadOnlyArray]byte[] inData) { | |
var aesObj = Aes.Create(); | |
aesObj.Mode = CipherMode.ECB; | |
aesObj.Padding = PaddingMode.None; | |
var zeroIv = new byte[16]; | |
var Encryptor = aesObj.CreateEncryptor(psk, zeroIv); | |
var counter = new byte[16]; // copy input iv (do not modify it) | |
for (var i = 0; i < 16; i++) { | |
counter[i] = iv[i]; | |
} | |
var ctrOut = new byte[16]; | |
var output = new byte[inData.Length]; | |
var pos = 0; | |
while (true) { | |
if (pos >= inData.Length) { | |
break; | |
} | |
Encryptor.TransformBlock(counter, 0, 16, ctrOut, 0); | |
for (var i = 0; i < 16; i++) { | |
if (pos >= inData.Length) { | |
break; | |
} | |
output[pos] = (byte)(inData[pos] ^ ctrOut[i]); | |
pos++; | |
} | |
// increment counter | |
for (var i = 15; i >= 0; i--) { | |
counter[i]++; | |
if (counter[i] != 0) { | |
break; | |
} | |
} | |
} | |
return output; | |
} | |
} | |
} |
Please share Decrypt
@binbon2d there is not Decrypt of aes-ctr. Just call Encrypt with the same psk,iv and encrypted data, you will get plain data(or decrypted data)
Warning about aes-ctr:
- Never repeat the pair of key and iv range even once, or your data will be decrypted very easily by two encrypted datas with same key and iv.
- Aes-ctr do not have any correct check of your data.You may need hash or hmac to rejected incorrected data. Or others can change the content of your data by change the encrypted data without your software notice. You will get some strange bytes because of this.
- Try use tls1.3 or aes-gcm or ChaCha20-Poly1305 if you design new software and do not understand what i said or the wiki said. Those algorithms may be safer and faster
https://pkg.go.dev/crypto/cipher#NewCTR
https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
Ok thank
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works nice mate! Thank you for sharing. Did help me a lot!
Sad that
CreateEncryptor
is not acceptingReadOnlySpan
. It would be much cleaner for theEncrypt
function's signature. :)