Skip to content

Commit

Permalink
Updated copyright year to 2015
Browse files Browse the repository at this point in the history
  • Loading branch information
gpailler committed Apr 26, 2015
1 parent 181a838 commit 07be3ae
Show file tree
Hide file tree
Showing 9 changed files with 650 additions and 650 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013 gpailler
Copyright (c) 2015 gpailler

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
344 changes: 172 additions & 172 deletions MegaApiClient/Crypto.cs
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
#region License

/*
The MIT License (MIT)
Copyright (c) 2014 Gregoire Pailler
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#endregion

using System;
using System.Security.Cryptography;
using Newtonsoft.Json;

namespace CG.Web.MegaApiClient
{
internal class Crypto
{
private static readonly Rijndael Rijndael;
private static readonly byte[] DefaultIv = new byte[16];

static Crypto()
{
Rijndael = Rijndael.Create();
Rijndael.Padding = PaddingMode.None;
Rijndael.Mode = CipherMode.CBC;
}

#region Key

public static byte[] DecryptKey(byte[] data, byte[] key)
{
byte[] result = new byte[data.Length];

for (int idx = 0; idx < data.Length; idx += 16)
{
byte[] block = data.CopySubArray(16, idx);
byte[] decryptedBlock = DecryptAes(block, key);
Array.Copy(decryptedBlock, 0, result, idx, 16);
}

return result;
}

public static byte[] EncryptKey(byte[] data, byte[] key)
{
byte[] result = new byte[data.Length];

for (int idx = 0; idx < data.Length; idx += 16)
{
byte[] block = data.CopySubArray(16, idx);
byte[] encryptedBlock = EncryptAes(block, key);
Array.Copy(encryptedBlock, 0, result, idx, 16);
}

return result;
}

public static void GetPartsFromDecryptedKey(byte[] decryptedKey, out byte[] iv, out byte[] metaMac, out byte[] fileKey)
#region License

/*
The MIT License (MIT)
Copyright (c) 2015 Gregoire Pailler
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#endregion

using System;
using System.Security.Cryptography;
using Newtonsoft.Json;

namespace CG.Web.MegaApiClient
{
internal class Crypto
{
private static readonly Rijndael Rijndael;
private static readonly byte[] DefaultIv = new byte[16];

static Crypto()
{
Rijndael = Rijndael.Create();
Rijndael.Padding = PaddingMode.None;
Rijndael.Mode = CipherMode.CBC;
}

#region Key

public static byte[] DecryptKey(byte[] data, byte[] key)
{
byte[] result = new byte[data.Length];

for (int idx = 0; idx < data.Length; idx += 16)
{
byte[] block = data.CopySubArray(16, idx);
byte[] decryptedBlock = DecryptAes(block, key);
Array.Copy(decryptedBlock, 0, result, idx, 16);
}

return result;
}

public static byte[] EncryptKey(byte[] data, byte[] key)
{
byte[] result = new byte[data.Length];

for (int idx = 0; idx < data.Length; idx += 16)
{
byte[] block = data.CopySubArray(16, idx);
byte[] encryptedBlock = EncryptAes(block, key);
Array.Copy(encryptedBlock, 0, result, idx, 16);
}

return result;
}

public static void GetPartsFromDecryptedKey(byte[] decryptedKey, out byte[] iv, out byte[] metaMac, out byte[] fileKey)
{
// Extract Iv and MetaMac
iv = new byte[8];
Expand All @@ -86,99 +86,99 @@ public static void GetPartsFromDecryptedKey(byte[] decryptedKey, out byte[] iv,
for (int idx = 0; idx < 16; idx++)
{
fileKey[idx] = (byte)(decryptedKey[idx] ^ decryptedKey[idx + 16]);
}
}
#endregion

#region Aes

public static byte[] DecryptAes(byte[] data, byte[] key)
{
using (ICryptoTransform decryptor = Rijndael.CreateDecryptor(key, DefaultIv))
{
return decryptor.TransformFinalBlock(data, 0, data.Length);
}
}

public static byte[] EncryptAes(byte[] data, byte[] key)
{
using (ICryptoTransform encryptor = Rijndael.CreateEncryptor(key, DefaultIv))
{
return encryptor.TransformFinalBlock(data, 0, data.Length);
}
}

public static byte[] CreateAesKey()
{
using (Rijndael rijndael = Rijndael.Create())
{
rijndael.Mode = CipherMode.CBC;
rijndael.KeySize = 128;
rijndael.Padding = PaddingMode.None;
rijndael.GenerateKey();
return rijndael.Key;
}
}

#endregion

#region Attributes

public static byte[] EncryptAttributes(Attributes attributes, byte[] nodeKey)
{
string data = "MEGA" + JsonConvert.SerializeObject(attributes, Formatting.None);
byte[] dataBytes = data.ToBytes();
dataBytes = dataBytes.CopySubArray(dataBytes.Length + 16 - (dataBytes.Length % 16));

return EncryptAes(dataBytes, nodeKey);
}

public static Attributes DecryptAttributes(byte[] attributes, byte[] nodeKey)
{
byte[] decryptedAttributes = DecryptAes(attributes, nodeKey);

// Remove MEGA prefix
try
{
return JsonConvert.DeserializeObject<Attributes>(decryptedAttributes.ToUTF8String().Substring(4));
}
catch (Exception ex)
{
return new Attributes(string.Format("Attribute deserialization failed: {0}", ex.Message));
}
}

#endregion

#region Rsa

public static BigInteger[] GetRsaPrivateKeyComponents(byte[] encodedRsaPrivateKey, byte[] masterKey)
{
// We need to add padding to obtain multiple of 16
encodedRsaPrivateKey = encodedRsaPrivateKey.CopySubArray(encodedRsaPrivateKey.Length + (16 - encodedRsaPrivateKey.Length % 16));
byte[] rsaPrivateKey = DecryptKey(encodedRsaPrivateKey, masterKey);

// rsaPrivateKeyComponents[0] => First factor p
// rsaPrivateKeyComponents[1] => Second factor q
// rsaPrivateKeyComponents[2] => Private exponent d
BigInteger[] rsaPrivateKeyComponents = new BigInteger[4];
for (int i = 0; i < 4; i++)
{
rsaPrivateKeyComponents[i] = rsaPrivateKey.FromMPINumber();

// Remove already retrieved part
int dataLength = ((rsaPrivateKey[0] * 256 + rsaPrivateKey[1] + 7) / 8);
rsaPrivateKey = rsaPrivateKey.CopySubArray(rsaPrivateKey.Length - dataLength - 2, dataLength + 2);
}

return rsaPrivateKeyComponents;
}

public static byte[] RsaDecrypt(BigInteger data, BigInteger p, BigInteger q, BigInteger d)
{
return data.modPow(d, p * q).getBytes();
}

#endregion
}
}
}
}
#endregion

#region Aes

public static byte[] DecryptAes(byte[] data, byte[] key)
{
using (ICryptoTransform decryptor = Rijndael.CreateDecryptor(key, DefaultIv))
{
return decryptor.TransformFinalBlock(data, 0, data.Length);
}
}

public static byte[] EncryptAes(byte[] data, byte[] key)
{
using (ICryptoTransform encryptor = Rijndael.CreateEncryptor(key, DefaultIv))
{
return encryptor.TransformFinalBlock(data, 0, data.Length);
}
}

public static byte[] CreateAesKey()
{
using (Rijndael rijndael = Rijndael.Create())
{
rijndael.Mode = CipherMode.CBC;
rijndael.KeySize = 128;
rijndael.Padding = PaddingMode.None;
rijndael.GenerateKey();
return rijndael.Key;
}
}

#endregion

#region Attributes

public static byte[] EncryptAttributes(Attributes attributes, byte[] nodeKey)
{
string data = "MEGA" + JsonConvert.SerializeObject(attributes, Formatting.None);
byte[] dataBytes = data.ToBytes();
dataBytes = dataBytes.CopySubArray(dataBytes.Length + 16 - (dataBytes.Length % 16));

return EncryptAes(dataBytes, nodeKey);
}

public static Attributes DecryptAttributes(byte[] attributes, byte[] nodeKey)
{
byte[] decryptedAttributes = DecryptAes(attributes, nodeKey);

// Remove MEGA prefix
try
{
return JsonConvert.DeserializeObject<Attributes>(decryptedAttributes.ToUTF8String().Substring(4));
}
catch (Exception ex)
{
return new Attributes(string.Format("Attribute deserialization failed: {0}", ex.Message));
}
}

#endregion

#region Rsa

public static BigInteger[] GetRsaPrivateKeyComponents(byte[] encodedRsaPrivateKey, byte[] masterKey)
{
// We need to add padding to obtain multiple of 16
encodedRsaPrivateKey = encodedRsaPrivateKey.CopySubArray(encodedRsaPrivateKey.Length + (16 - encodedRsaPrivateKey.Length % 16));
byte[] rsaPrivateKey = DecryptKey(encodedRsaPrivateKey, masterKey);

// rsaPrivateKeyComponents[0] => First factor p
// rsaPrivateKeyComponents[1] => Second factor q
// rsaPrivateKeyComponents[2] => Private exponent d
BigInteger[] rsaPrivateKeyComponents = new BigInteger[4];
for (int i = 0; i < 4; i++)
{
rsaPrivateKeyComponents[i] = rsaPrivateKey.FromMPINumber();

// Remove already retrieved part
int dataLength = ((rsaPrivateKey[0] * 256 + rsaPrivateKey[1] + 7) / 8);
rsaPrivateKey = rsaPrivateKey.CopySubArray(rsaPrivateKey.Length - dataLength - 2, dataLength + 2);
}

return rsaPrivateKeyComponents;
}

public static byte[] RsaDecrypt(BigInteger data, BigInteger p, BigInteger q, BigInteger d)
{
return data.modPow(d, p * q).getBytes();
}

#endregion
}
}
2 changes: 1 addition & 1 deletion MegaApiClient/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Gregoire Pailler
Copyright (c) 2015 Gregoire Pailler
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
2 changes: 1 addition & 1 deletion MegaApiClient/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Gregoire Pailler
Copyright (c) 2015 Gregoire Pailler
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
2 changes: 1 addition & 1 deletion MegaApiClient/IWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Gregoire Pailler
Copyright (c) 2015 Gregoire Pailler
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
Loading

0 comments on commit 07be3ae

Please sign in to comment.