윈도우 방화벽 규칙 추가/삭제 – C#

방화벽 관련해서 참고는 구글링 웹문서들과 mdsn
https://msdn.microsoft.com/ko-kr/library/windows/desktop/aa365344(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ff956124(v=vs.85).aspx

c#에서 방화벽에 규칙을 추가하는 방법을 찾던중에
쉽게 할 수 있는 방법이 있어 해당 코드를 안내합니다.

참조추가
FirewallAPI.dll 파일을 참조로 추가합니다.

규칙추가

[csharp]
INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
firewallRule.Description = "방화벽 규칙에 대한 설명을 입력합니다";
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN;
firewallRule.ApplicationName = Application.ExecutablePath + @"\app.exe";
firewallRule.InterfaceTypes = "All";
firewallRule.Name = "Rule name"; // 방화벽 규칙을 구분하는 이름, 삭제시에도 사용됩니다
firewallRule.Enabled = true;

INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(firewallRule);
[/csharp]

규칙삭제

[csharp]
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Remove("Rule name");
[/csharp]

기존에 추가된 방화벽 규칙을 확인

[csharp]
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
foreach(INetFwRule rule in firewallPolicy.Rules)
{
Console.WriteLine(rule.Name);
}
[/csharp]

C#, PHP – RIJNDAEL 256bit 암/복호화

C#과 php간에 데이터를 주고 받아야 하는데
평문으로 통신하기에 무리가 있던 도중
기존에 사용하던 php 암호화 라이브러리와 연동되는 c# 코드를 발견~
살짝 수정해서 사용하는데 무리가 없음

기본은 128bit지만 256bit로 변경했음
php의 경우 블록에서 남는 부분을 zero로 채워넣기 때문에
c#에서 PaddingMode.Zeros를 설정해야 정상적으로 동작합니다.

Encryption.cs

[csharp]
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace TestClass
{
class Encryption
{
private static byte[] rijnKey = Encoding.UTF8.GetBytes("abcdefg_abcdefg_abcdefg_abcdefg_");
private static byte[] rijnIV = Encoding.UTF8.GetBytes("abcdefg_abcdefg_abcdefg_abcdefg_");

public static String EncryptIt(String s)
{
String result;
RijndaelManaged rijn = new RijndaelManaged();
rijn.Mode = CipherMode.CBC;
rijn.Padding = PaddingMode.Zeros; // php와의 연동에서 꼭 확인
rijn.BlockSize = 256;

using (MemoryStream msEncrypt = new MemoryStream())
{
using (ICryptoTransform encryptor = rijn.CreateEncryptor(rijnKey, rijnIV))
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(s);
}
}
}
result = Convert.ToBase64String(msEncrypt.ToArray());
}
rijn.Clear();

result = Base64UrlEncode(result);

return result;
}

public static String DecryptIt(String s)
{
String result;
RijndaelManaged rijn = new RijndaelManaged();
rijn.Mode = CipherMode.CBC;
rijn.Padding = PaddingMode.Zeros;
rijn.BlockSize = 256;

s = Base64UrlDecode(s);

using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(s)))
{
using (ICryptoTransform decryptor = rijn.CreateDecryptor(rijnKey, rijnIV))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader swDecrypt = new StreamReader(csDecrypt))
{
result = swDecrypt.ReadToEnd();
}
}
}
}
rijn.Clear();

return result;
}

public static string Base64UrlEncode(string arg)
{
arg = arg.Split(‘=’)[0]; // Remove any trailing ‘=’s
arg = arg.Replace(‘+’, ‘-‘); // 62nd char of encoding
arg = arg.Replace(‘/’, ‘_’); // 63rd char of encoding
return arg;
}

public static string Base64UrlDecode(string arg)
{
string s = arg;
s = s.Replace(‘-‘, ‘+’); // 62nd char of encoding
s = s.Replace(‘_’, ‘/’); // 63rd char of encoding
switch (s.Length % 4) // Pad with trailing ‘=’s
{
case 0: break; // No pad chars in this case
case 2: s += "=="; break; // Two pad chars
case 3: s += "="; break; // One pad char
default:
throw new System.Exception("Illegal base64url string!");
}
return s;
}
}
}

[/csharp]

test.php

[php]
//Encryption function
function encryption($encrypt, $key, $iv)
{
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, ”, MCRYPT_MODE_CBC, ”);
mcrypt_generic_init($td, $key, $iv);
$encrypted = mcrypt_generic($td, $encrypt);
$encode = base64_encode($encrypted);
$encode = base64safeEncode($encode);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);

return $encode;
}

//Decryption function
function decryption($decrypt, $key, $iv)
{
$decoded = base64safeDecode($decrypt);
$decoded = base64_decode($decoded);
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, ”, MCRYPT_MODE_CBC, ”);
mcrypt_generic_init($td, $key, $iv);
$decrypted = mdecrypt_generic($td, $decoded);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);

return trim($decrypted);
}

function base64safeEncode($data) {
$data = str_replace("=", "", $data);
$data = str_replace("+", "-", $data);
$data = str_replace("/", "_", $data);
return $data;
}

function base64safeDecode($data) {
$data = str_replace("-", "+", $data);
$data = str_replace("_", "/", $data);

switch (strlen($data) % 4) {
case 0: break;
case 2: $data .= "=="; break;
case 3: $data .= "="; break;
}

return $data;
}

$key = "abcdefg_abcdefg_abcdefg_abcdefg_";
$iv = "abcdefg_abcdefg_abcdefg_abcdefg_";

echo encryption("Hello world!", $key, $iv);
echo decryption("1ERpu8RaiyMqsXdjhCEu9IVMyQlz/A3fBR6vGbNxaVs=", $key, $iv);
[/php]