sharee.bike-App/LockIt.BusinessLogic/Services/BluetoothLock/Crypto/Cipher.cs

89 lines
2.7 KiB
C#
Raw Normal View History

2021-05-13 17:07:16 +02:00
using System;
using System.IO;
using System.Security.Cryptography;
2024-04-09 12:53:23 +02:00
using ShareeBike.Model.Device;
2021-05-13 17:07:16 +02:00
2024-04-09 12:53:23 +02:00
namespace ShareeBike.Services.BluetoothLock.Crypto
2021-05-13 17:07:16 +02:00
{
2022-09-06 16:08:19 +02:00
public class Cipher : ICipher
{
/// <summary> Decrypt data. </summary>
/// <remarks>
/// Further info see:
/// https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.aes?view=netcore-3.1 for further info
// https://docs.microsoft.com/en-us/dotnet/standard/security/cryptographic-services
// https://stackoverflow.com/questions/24903575/how-to-return-byte-when-decrypt-using-cryptostream-descryptoserviceprovider/24903689
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
/// </remarks>
/// <param name="key">Key to decrypt data with.</param>
/// <param name="encrypted">Encrpyted data to decrypt.</param>
/// <returns>Decrypted data.</returns>
public byte[] Decrypt(byte[] key, byte[] encrypted)
{
// Check arguments.
if (encrypted == null || encrypted.Length <= 0)
throw new ArgumentNullException(nameof(encrypted));
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
if (key == null || key.Length <= 0)
throw new ArgumentNullException(nameof(key));
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
using (Aes aesAlg = Aes.Create())
{
aesAlg.KeySize = 192;
aesAlg.Mode = CipherMode.ECB;
aesAlg.Padding = PaddingMode.None;
aesAlg.Key = key;
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
// Create the streams used for decryption.
using (var msDecrypt = new MemoryStream())
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write))
{
csDecrypt.Write(encrypted, 0, encrypted.Length);
csDecrypt.FlushFinalBlock();
return msDecrypt.ToArray();
}
}
}
}
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
public byte[] Encrypt(byte[] key, byte[] clear)
{
// Check arguments.
if (clear == null || clear.Length <= 0)
throw new ArgumentNullException("plainText");
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
if (key == null || key.Length <= 0)
throw new ArgumentNullException("Key");
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
// Create an AesCryptoServiceProvider object
// with the specified key and IV.
using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
aesAlg.KeySize = 192;
aesAlg.Mode = CipherMode.ECB;
aesAlg.Padding = PaddingMode.None;
aesAlg.Key = key;
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
// Create the streams used for encryption.
using (var msEncrypt = new MemoryStream())
{
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
csEncrypt.Write(clear, 0, clear.Length);
csEncrypt.FlushFinalBlock();
return msEncrypt.ToArray();
}
}
}
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
}
}
2021-05-13 17:07:16 +02:00
}