-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
123 changed files
with
7,837 additions
and
3,306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
using System.Diagnostics; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace CypherRSA; | ||
|
||
// Max Lengh Support 379 Characters | ||
public static class CypherRSA | ||
{ | ||
public static void GenerateKeys(out byte[] publicKey, out byte[] privateKey) | ||
{ | ||
try | ||
{ | ||
using RSA rsaKey = RSA.Create(4096); | ||
publicKey = rsaKey.ExportRSAPublicKey(); | ||
privateKey = rsaKey.ExportRSAPrivateKey(); | ||
rsaKey.Clear(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
publicKey = Array.Empty<byte>(); | ||
privateKey = Array.Empty<byte>(); | ||
Debug.WriteLine("CypherTool GenerateKeys: " + ex.Message); | ||
} | ||
} | ||
|
||
public static bool TryEncrypt(byte[] input, byte[] publicKey, out byte[] encryptedBytes, out string encryptedHex) | ||
{ | ||
try | ||
{ | ||
using RSA rsaKey = RSA.Create(4096); | ||
rsaKey.ImportRSAPublicKey(publicKey, out _); | ||
encryptedBytes = rsaKey.Encrypt(input, RSAEncryptionPadding.OaepSHA512); | ||
encryptedHex = Convert.ToHexString(encryptedBytes); | ||
|
||
rsaKey.Clear(); | ||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
encryptedBytes = Array.Empty<byte>(); | ||
encryptedHex = string.Empty; | ||
Debug.WriteLine("CypherTool TryEncrypt 1: " + ex.Message); | ||
return false; | ||
} | ||
} | ||
|
||
public static bool TryEncrypt(string inputText, byte[] publicKey, out byte[] encryptedBytes, out string encryptedHex) | ||
{ | ||
try | ||
{ | ||
byte[] inputBytes = Encoding.UTF8.GetBytes(inputText); | ||
return TryEncrypt(inputBytes, publicKey, out encryptedBytes, out encryptedHex); | ||
} | ||
catch (Exception ex) | ||
{ | ||
encryptedBytes = Array.Empty<byte>(); | ||
encryptedHex = string.Empty; | ||
Debug.WriteLine("CypherTool TryEncrypt 2: " + ex.Message); | ||
return false; | ||
} | ||
} | ||
|
||
public static bool TryDecrypt(byte[] encryptedBytes, byte[] privateKey, out byte[] decryptedBytes) | ||
{ | ||
try | ||
{ | ||
using RSA rsaKey = RSA.Create(4096); | ||
rsaKey.ImportRSAPrivateKey(privateKey, out _); | ||
decryptedBytes = rsaKey.Decrypt(encryptedBytes, RSAEncryptionPadding.OaepSHA512); | ||
rsaKey.Clear(); | ||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
decryptedBytes = Array.Empty<byte>(); | ||
Debug.WriteLine("CypherTool TryDecrypt 1: " + ex.Message); | ||
return false; | ||
} | ||
} | ||
|
||
public static bool TryDecrypt(string encryptedHex, byte[] privateKey, out byte[] decryptedBytes) | ||
{ | ||
try | ||
{ | ||
byte[] encryptedBytes = Convert.FromHexString(encryptedHex); | ||
return TryDecrypt(encryptedBytes, privateKey, out decryptedBytes); | ||
} | ||
catch (Exception ex) | ||
{ | ||
decryptedBytes = Array.Empty<byte>(); | ||
Debug.WriteLine("CypherTool TryDecrypt 2: " + ex.Message); | ||
return false; | ||
} | ||
} | ||
|
||
public static bool TryDecrypt(byte[] encryptedBytes, byte[] privateKey, out string decryptedText) | ||
{ | ||
bool isDecryptionSuccess = TryDecrypt(encryptedBytes, privateKey, out byte[] decryptedBytes); | ||
if (isDecryptionSuccess) | ||
{ | ||
try | ||
{ | ||
decryptedText = Encoding.UTF8.GetString(decryptedBytes); | ||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
decryptedText = string.Empty; | ||
Debug.WriteLine("CypherTool TryDecrypt 3: " + ex.Message); | ||
return false; | ||
} | ||
} | ||
else | ||
{ | ||
decryptedText = string.Empty; | ||
return false; | ||
} | ||
} | ||
|
||
public static bool TryDecrypt(string encryptedHex, byte[] privateKey, out string decryptedText) | ||
{ | ||
try | ||
{ | ||
byte[] encryptedBytes = Convert.FromHexString(encryptedHex); | ||
return TryDecrypt(encryptedBytes, privateKey, out decryptedText); | ||
} | ||
catch (Exception ex) | ||
{ | ||
decryptedText = string.Empty; | ||
Debug.WriteLine("CypherTool TryDecrypt 4: " + ex.Message); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<Title>Cypher RSA Tool</Title> | ||
<Version>$(VersionPrefix)1.0.0</Version> | ||
<Copyright>MSasanMH</Copyright> | ||
<PackageIcon>SecureDNSClient.png</PackageIcon> | ||
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract> | ||
<Platforms>AnyCPU;x64;x86</Platforms> | ||
<ApplicationIcon>SecureDNSClientMulti.ico</ApplicationIcon> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="SecureDNSClientMulti.ico" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\SecureDNSClient.png"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<_LastSelectedProfileId>D:\PPWin\SecureDNSClient\CypherRSA\Properties\PublishProfiles\X64.pubxml</_LastSelectedProfileId> | ||
</PropertyGroup> | ||
</Project> |
Oops, something went wrong.