Skip to content

Commit

Permalink
Solve issue tuya#1 tuya#1
Browse files Browse the repository at this point in the history
  • Loading branch information
0xZunia committed Nov 28, 2024
1 parent 51cd1b7 commit b38e9e1
Showing 1 changed file with 44 additions and 48 deletions.
92 changes: 44 additions & 48 deletions AesUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,61 @@
class AesUtil {

public static string DecryptMessage(IMessage message, string accessKey) {
message.Properties.TryGetValue("em", out var decrypt_model);
Console.WriteLine($"Received: {decrypt_model}");
string data = Encoding.UTF8.GetString(message.Data);
message.Properties.TryGetValue("em", out var decryptModel);
Console.WriteLine($"Received: {decryptModel}");
var data = Encoding.UTF8.GetString(message.Data);
ArgumentNullException.ThrowIfNull(data, nameof(data));
JObject payloadJson = (JObject)JsonConvert.DeserializeObject(data);

if (decrypt_model == "aes_gcm") {
return DecryptByGcm(payloadJson["data"].ToString(),accessKey.Substring(8,16)).Replace("\f","").Replace("\r","").Replace("\n","").Replace("\t","").Replace("\v","").Replace("\b","");
} else {
return DecryptByEcb(payloadJson["data"].ToString(),accessKey.Substring(8,16)).Replace("\f","").Replace("\r","").Replace("\n","").Replace("\t","").Replace("\v","").Replace("\b","");
}
var payloadJson = (JObject?)JsonConvert.DeserializeObject(data);
if (payloadJson == null) return string.Empty;

return decryptModel == "aes_gcm" ?
DecryptByGcm(payloadJson["data"].ToString(),accessKey.Substring(8,16)).Replace("\f","").Replace("\r","").Replace("\n","").Replace("\t","").Replace("\v","").Replace("\b","")
: DecryptByEcb(payloadJson["data"].ToString(),accessKey.Substring(8,16)).Replace("\f","").Replace("\r","").Replace("\n","").Replace("\t","").Replace("\v","").Replace("\b","");
}

//decrypt_by_gcm
private static string DecryptByGcm(string decryptStr, string Key) {
byte[] cadenaBytes = Convert.FromBase64String(decryptStr);
byte[] claveBytes = Encoding.UTF8.GetBytes(Key);
// The first 12 bytes are the nonce
byte[] nonce = new byte[12];
Array.Copy(cadenaBytes, 0, nonce, 0, nonce.Length);
private static string DecryptByGcm(string decryptStr, string key)
{
var encryptedBytes = Convert.FromBase64String(decryptStr);
var keyBytes = Encoding.UTF8.GetBytes(key);

var nonce = new byte[12];
Array.Copy(encryptedBytes, 0, nonce, 0, nonce.Length);

var ciphertext = new byte[encryptedBytes.Length - nonce.Length - 16];
Array.Copy(encryptedBytes, nonce.Length, ciphertext, 0, ciphertext.Length);

var tag = new byte[16];
Array.Copy(encryptedBytes, encryptedBytes.Length - 16, tag, 0, tag.Length);

// The data to decrypt (excluding nonce and tag)
byte[] ciphertext = new byte[cadenaBytes.Length - nonce.Length - 16];
Array.Copy(cadenaBytes, nonce.Length, ciphertext, 0, ciphertext.Length);
var plaintext = new byte[ciphertext.Length];
using var aesGcm = new AesGcm(keyBytes, 16);
aesGcm.Decrypt(nonce, ciphertext, tag, plaintext);

// The last 16 bytes are the authentication tag
byte[] tag = new byte[16];
Array.Copy(cadenaBytes, cadenaBytes.Length - 16, tag, 0, tag.Length);

using (AesGcm aesGcm = new AesGcm(claveBytes))
{
byte[] plaintext = new byte[ciphertext.Length];
aesGcm.Decrypt(nonce, ciphertext, tag, plaintext);
return System.Text.Encoding.UTF8.GetString(plaintext);
}
return Encoding.UTF8.GetString(plaintext);
}

//decrypt_by_aes
private static string DecryptByEcb(string decryptStr, string Key) {
try{
byte[] cadenaBytes = Convert.FromBase64String(decryptStr);
byte[] claveBytes = Encoding.UTF8.GetBytes(Key);
private static string? DecryptByEcb(string decryptStr, string key)
{
try
{
var encryptedBytes = Convert.FromBase64String(decryptStr);
var keyBytes = Encoding.UTF8.GetBytes(key);

RijndaelManaged rijndaelManaged = new RijndaelManaged();
rijndaelManaged.Mode = CipherMode.ECB;
rijndaelManaged.BlockSize = 128;
rijndaelManaged.Padding = PaddingMode.Zeros;
ICryptoTransform desencriptador;
desencriptador = rijndaelManaged.CreateDecryptor(claveBytes, rijndaelManaged.IV);
MemoryStream memStream = new MemoryStream(cadenaBytes);
CryptoStream cryptoStream;
cryptoStream = new CryptoStream(memStream, desencriptador, CryptoStreamMode.Read);
StreamReader streamReader = new StreamReader(cryptoStream);
string resultStr = streamReader.ReadToEnd();
using var aes = Aes.Create();
aes.Mode = CipherMode.ECB;
aes.BlockSize = 128;
aes.Padding = PaddingMode.Zeros;

memStream.Close();
cryptoStream.Close();
return resultStr;
}catch (Exception ex){
using var decryptor = aes.CreateDecryptor(keyBytes, null);
using var memStream = new MemoryStream(encryptedBytes);
using var cryptoStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read);
using var reader = new StreamReader(cryptoStream);
return reader.ReadToEnd();
}
catch (Exception ex)
{
Console.WriteLine(ex);
return null;
}
Expand Down

0 comments on commit b38e9e1

Please sign in to comment.