public static void DecryptAttrs(IEncryptableAttributes node) { var nodeKey = node.NodeKey.DecryptedKey; if (nodeKey == null) { node.Attributes = new NodeAttributes(); return; } var key2 = new byte[16]; // do we need to reduce var r = nodeKey.Length > key2.Length; for (var i = 0; i < 16; i++) { key2[i] = r ? (byte)(nodeKey[i] ^ nodeKey[i + 16]) : nodeKey[i]; } var attrBytes = Crypto.DecryptCbc(key2, node.encryptedAttributes); var attrString = Encoding.UTF8.GetString(attrBytes) .Replace("\0", String.Empty) .Replace("MEGA", String.Empty); if (string.IsNullOrEmpty(attrString)) { return; } try { node.Attributes = JsonConvert.DeserializeObject <NodeAttributes>(attrString); } catch (JsonException) { node.Attributes = new NodeAttributes { Name = "error_loading_attrs" }; } }
public static void EncryptAttrs(IEncryptableAttributes node) { var nodeKey = node.NodeKey.DecryptedKey; var attrs = JsonConvert.SerializeObject(node.Attributes); var attr = Encoding.UTF8.GetBytes("MEGA" + attrs); var needsPadding = attr.Length % 16 != 0; var pAttr = new byte[needsPadding ? attr.Length + 16 - (attr.Length % 16) : attr.Length]; Array.Copy(attr, pAttr, attr.Length); var newKey = new byte[nodeKey.Length]; Array.Copy(nodeKey, newKey, nodeKey.Length); if (newKey.Length > 16) { newKey.XorWith(0, newKey, 16, 16); } var aes_key = new byte[16]; Array.Copy(newKey, aes_key, 16); node.encryptedAttributes = Crypto.EncryptCbc(aes_key, pAttr); }
public static void DecryptAttrs(IEncryptableAttributes node) { var nodeKey = node.NodeKey.DecryptedKey; if (nodeKey == null) { node.Attributes = new NodeAttributes(); return; } var key2 = new byte[16]; // do we need to reduce var r = nodeKey.Length > key2.Length; for (var i = 0; i < 16; i++) { key2[i] = r ? (byte)(nodeKey[i] ^ nodeKey[i + 16]) : nodeKey[i]; } var attrBytes = Crypto.DecryptCbc(key2, node.encryptedAttributes); var attrString = Encoding.UTF8.GetString(attrBytes) .Replace("\0", String.Empty) .Replace("MEGA", String.Empty); if (string.IsNullOrEmpty(attrString)) { return; } try { node.Attributes = JsonConvert.DeserializeObject<NodeAttributes>(attrString); } catch (JsonException) { node.Attributes = new NodeAttributes { Name = "error_loading_attrs" }; } }