Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.DS_Store
.git
.vs
bin
obj
################################################################################
# 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。
################################################################################

/WinFormsP2PConnect/.vs/WinFormsP2PConnect.slnx
/WinFormsP2PConnect/WinFormsP2PConnect/bin/Debug/net10.0-windows
/WinFormsP2PConnect/WinFormsP2PConnect/obj
/WinFormsP2PConnect/.vs/ProjectEvaluation
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

47 changes: 0 additions & 47 deletions README.md

This file was deleted.

3 changes: 3 additions & 0 deletions WinFormsP2PConnect/WinFormsP2PConnect.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="WinFormsP2PConnect/WinFormsP2PConnect.csproj" />
</Solution>
260 changes: 130 additions & 130 deletions p2pconn/Cryptography/Aes256.cs → ...WinFormsP2PConnect/Cryptography/Aes256.cs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,130 +1,130 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Cryptography
{
#region " Aes256"
public class Aes256
{
private const int KeyLength = 32;
private const int AuthKeyLength = 64;
private const int IvLength = 16;
private const int HmacSha256Length = 32;
private readonly byte[] _key;
private readonly byte[] _authKey;
private static readonly byte[] Salt =
{
0xBF, 0xEB, 0x1E, 0x56, 0xFB, 0xCD, 0x97, 0x3B, 0xB2, 0x19, 0x2, 0x24, 0x30, 0xA5, 0x78, 0x43, 0x0, 0x3D, 0x56,
0x44, 0xD2, 0x1E, 0x62, 0xB9, 0xD4, 0xF1, 0x80, 0xE7, 0xE6, 0xC3, 0x39, 0x41
};
public Aes256(string masterKey)
{
if (string.IsNullOrEmpty(masterKey))
throw new ArgumentException($"{nameof(masterKey)} can not be null or empty.");
using (Rfc2898DeriveBytes derive = new Rfc2898DeriveBytes(masterKey, Salt, 5000))
{
_key = derive.GetBytes(KeyLength);
_authKey = derive.GetBytes(AuthKeyLength);
}
}
public string Encrypt(string input)
{
return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(input)));
}
/* FORMAT
* ----------------------------------------
* | HMAC | IV | CIPHERTEXT |
* ----------------------------------------
* 32 bytes 16 bytes
*/
public byte[] Encrypt(byte[] input)
{
if (input == null)
throw new ArgumentNullException($"{nameof(input)} can not be null.");
using (var ms = new MemoryStream())
{
ms.Position = HmacSha256Length; // reserve first 32 bytes for HMAC
using (var aesProvider = new AesCryptoServiceProvider())
{
aesProvider.KeySize = 256;
aesProvider.BlockSize = 128;
aesProvider.Mode = CipherMode.CBC;
aesProvider.Padding = PaddingMode.PKCS7;
aesProvider.Key = _key;
aesProvider.GenerateIV();
using (var cs = new CryptoStream(ms, aesProvider.CreateEncryptor(), CryptoStreamMode.Write))
{
ms.Write(aesProvider.IV, 0, aesProvider.IV.Length); // write next 16 bytes the IV, followed by ciphertext
cs.Write(input, 0, input.Length);
cs.FlushFinalBlock();
using (var hmac = new HMACSHA256(_authKey))
{
byte[] hash = hmac.ComputeHash(ms.ToArray(), HmacSha256Length, ms.ToArray().Length - HmacSha256Length); // compute the HMAC of IV and ciphertext
ms.Position = 0; // write hash at beginning
ms.Write(hash, 0, hash.Length);
}
}
}
return ms.ToArray();
}
}
public string Decrypt(string input)
{
return Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(input)));
}
public byte[] Decrypt(byte[] input)
{
if (input == null)
throw new ArgumentNullException($"{nameof(input)} can not be null.");
using (var ms = new MemoryStream(input))
{
using (var aesProvider = new AesCryptoServiceProvider())
{
aesProvider.KeySize = 256;
aesProvider.BlockSize = 128;
aesProvider.Mode = CipherMode.CBC;
aesProvider.Padding = PaddingMode.PKCS7;
aesProvider.Key = _key;
// read first 32 bytes for HMAC
using (var hmac = new HMACSHA256(_authKey))
{
var hash = hmac.ComputeHash(ms.ToArray(), HmacSha256Length, ms.ToArray().Length - HmacSha256Length);
byte[] receivedHash = new byte[HmacSha256Length];
ms.Read(receivedHash, 0, receivedHash.Length);
if (!SafeComparison.AreEqual(hash, receivedHash))
throw new CryptographicException("Invalid message authentication code (MAC).");
}
byte[] iv = new byte[IvLength];
ms.Read(iv, 0, IvLength); // read next 16 bytes for IV, followed by ciphertext
aesProvider.IV = iv;
using (var cs = new CryptoStream(ms, aesProvider.CreateDecryptor(), CryptoStreamMode.Read))
{
byte[] temp = new byte[ms.Length - IvLength + 1];
byte[] data = new byte[cs.Read(temp, 0, temp.Length)];
Buffer.BlockCopy(temp, 0, data, 0, data.Length);
return data;
}
}
}
}
}
#endregion
}
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace WinFormsP2PConnect.Cryptography
{
#region " Aes256"
public class Aes256
{
private const int KeyLength = 32;
private const int AuthKeyLength = 64;
private const int IvLength = 16;
private const int HmacSha256Length = 32;
private readonly byte[] _key;
private readonly byte[] _authKey;

private static readonly byte[] Salt =
{
0xBF, 0xEB, 0x1E, 0x56, 0xFB, 0xCD, 0x97, 0x3B, 0xB2, 0x19, 0x2, 0x24, 0x30, 0xA5, 0x78, 0x43, 0x0, 0x3D, 0x56,
0x44, 0xD2, 0x1E, 0x62, 0xB9, 0xD4, 0xF1, 0x80, 0xE7, 0xE6, 0xC3, 0x39, 0x41
};

public Aes256(string masterKey)
{
if (string.IsNullOrEmpty(masterKey))
throw new ArgumentException($"{nameof(masterKey)} can not be null or empty.");

using (Rfc2898DeriveBytes derive = new Rfc2898DeriveBytes(masterKey, Salt, 5000))
{
_key = derive.GetBytes(KeyLength);
_authKey = derive.GetBytes(AuthKeyLength);
}
}

public string Encrypt(string input)
{
return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(input)));
}

/* FORMAT
* ----------------------------------------
* | HMAC | IV | CIPHERTEXT |
* ----------------------------------------
* 32 bytes 16 bytes
*/
public byte[] Encrypt(byte[] input)
{
if (input == null)
throw new ArgumentNullException($"{nameof(input)} can not be null.");

using (var ms = new MemoryStream())
{
ms.Position = HmacSha256Length; // reserve first 32 bytes for HMAC
using (var aesProvider = new AesCryptoServiceProvider())
{
aesProvider.KeySize = 256;
aesProvider.BlockSize = 128;
aesProvider.Mode = CipherMode.CBC;
aesProvider.Padding = PaddingMode.PKCS7;
aesProvider.Key = _key;
aesProvider.GenerateIV();

using (var cs = new CryptoStream(ms, aesProvider.CreateEncryptor(), CryptoStreamMode.Write))
{
ms.Write(aesProvider.IV, 0, aesProvider.IV.Length); // write next 16 bytes the IV, followed by ciphertext
cs.Write(input, 0, input.Length);
cs.FlushFinalBlock();

using (var hmac = new HMACSHA256(_authKey))
{
byte[] hash = hmac.ComputeHash(ms.ToArray(), HmacSha256Length, ms.ToArray().Length - HmacSha256Length); // compute the HMAC of IV and ciphertext
ms.Position = 0; // write hash at beginning
ms.Write(hash, 0, hash.Length);
}
}
}

return ms.ToArray();
}
}

public string Decrypt(string input)
{
return Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(input)));
}

public byte[] Decrypt(byte[] input)
{
if (input == null)
throw new ArgumentNullException($"{nameof(input)} can not be null.");

using (var ms = new MemoryStream(input))
{
using (var aesProvider = new AesCryptoServiceProvider())
{
aesProvider.KeySize = 256;
aesProvider.BlockSize = 128;
aesProvider.Mode = CipherMode.CBC;
aesProvider.Padding = PaddingMode.PKCS7;
aesProvider.Key = _key;

// read first 32 bytes for HMAC
using (var hmac = new HMACSHA256(_authKey))
{
var hash = hmac.ComputeHash(ms.ToArray(), HmacSha256Length, ms.ToArray().Length - HmacSha256Length);
byte[] receivedHash = new byte[HmacSha256Length];
ms.Read(receivedHash, 0, receivedHash.Length);

if (!SafeComparison.AreEqual(hash, receivedHash))
throw new CryptographicException("Invalid message authentication code (MAC).");
}

byte[] iv = new byte[IvLength];
ms.Read(iv, 0, IvLength); // read next 16 bytes for IV, followed by ciphertext
aesProvider.IV = iv;

using (var cs = new CryptoStream(ms, aesProvider.CreateDecryptor(), CryptoStreamMode.Read))
{
byte[] temp = new byte[ms.Length - IvLength + 1];
byte[] data = new byte[cs.Read(temp, 0, temp.Length)];
Buffer.BlockCopy(temp, 0, data, 0, data.Length);
return data;
}
}
}
}
}
#endregion
}
6 changes: 3 additions & 3 deletions p2pconn/Cryptography/SafeComparison.cs → ...P2PConnect/Cryptography/SafeComparison.cs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Runtime.CompilerServices;

namespace Cryptography
{
namespace WinFormsP2PConnect.Cryptography
{
#region " safe comparsion"
public class SafeComparison
{
Expand All @@ -26,6 +26,6 @@ public static bool AreEqual(byte[] a1, byte[] a2)
}
return result;
}
}
}
#endregion
}
6 changes: 3 additions & 3 deletions p2pconn/Cryptography/Sha256.cs → ...WinFormsP2PConnect/Cryptography/Sha256.cs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Security.Cryptography;
using System.Text;

namespace Cryptography
{
namespace WinFormsP2PConnect.Cryptography
{
#region " Sha256 check"
public static class Sha256
{
Expand Down Expand Up @@ -30,6 +30,6 @@ public static byte[] ComputeHash(byte[] input)
return sha.ComputeHash(input);
}
}
}
}
#endregion
}
Loading