Skip to content

Commit 3c13ffb

Browse files
Raj.KamalRaj.Kamal
Raj.Kamal
authored and
Raj.Kamal
committed
Added Cryptography project
1 parent 9f22a61 commit 3c13ffb

File tree

6 files changed

+255
-0
lines changed

6 files changed

+255
-0
lines changed

Cryptography/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>

Cryptography/CryptProgram.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.IO;
2+
using System.Text;
3+
using System;
4+
using CommandLineParser.Arguments;
5+
using CommandLineParser.Exceptions;
6+
using System.Text.RegularExpressions;
7+
8+
9+
namespace Cryptography
10+
{
11+
12+
class CryptProgram
13+
{
14+
static void Main(string[] args)
15+
{
16+
17+
CommandLineParser.CommandLineParser parser = new CommandLineParser.CommandLineParser();
18+
19+
var p = new CommandLineOptions();
20+
21+
Regex r = new Regex("(.*)=(.*)");
22+
Match m = r.Match(args[0]);
23+
24+
parser.AcceptEqualSignSyntaxForValueArguments = true;
25+
parser.PreserveValueQuotesForEqualsSignSyntax = true;
26+
parser.AllowShortSwitchGrouping = false;
27+
parser.AcceptHyphen = true;
28+
parser.ExtractArgumentAttributes(p);
29+
30+
try {
31+
parser.ParseCommandLine(args);
32+
}
33+
catch (CommandLineException e) {
34+
Console.WriteLine(e.Message);
35+
parser.ShowUsage();
36+
}
37+
38+
parser.ShowParsedArguments();
39+
var encrypter = new Encrypter();
40+
string text, pass, code;
41+
text = p.text;
42+
pass = p.key;
43+
if (p.decrypt == false) {
44+
code = encrypter.Encrypt(text, pass);
45+
Console.Write("Encrypted: ");
46+
Console.Write(code);
47+
48+
}
49+
else {
50+
try {
51+
code = encrypter.Decrypt(text, pass);
52+
Console.Write(code);
53+
Console.Write(code);
54+
55+
}
56+
catch(FormatException fex){
57+
Console.WriteLine("Format Exception: text is not in ocrrect format. \r\nException message: "+fex.Message);
58+
}
59+
Console.Write("Decrypted: ");
60+
}
61+
62+
63+
64+
}
65+
}
66+
class CommandLineOptions
67+
{
68+
[ValueArgument(typeof(string), "text", Description = "Plain/Cipher Text")]
69+
public string text { get; set; }
70+
71+
[ValueArgument(typeof(string), "key", Description = "Encryption Key")]
72+
public string key { get; set; }
73+
74+
/* SwitchArguments - for on/off boolean logic */
75+
[SwitchArgument('d', "decrypt", false, Description = "Set this to decrypt")]
76+
public bool decrypt { get; set; }
77+
/* SwitchArguments - for on/off boolean logic */
78+
79+
[SwitchArgument('e', "encrypt", false, Description = "Set this to decrypt")]
80+
public bool encrypt { get; set; }
81+
82+
83+
84+
}
85+
86+
}

Cryptography/Cryptography.csproj

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{1CD41E18-C241-4F3E-A9D1-25249C818AB1}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>Cryptography</RootNamespace>
11+
<AssemblyName>Cryptography</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="CommandLineArgumentsParser, Version=3.0.11.0, Culture=neutral, PublicKeyToken=16ad7bf6f4a1666c, processorArchitecture=MSIL">
36+
<HintPath>..\packages\CommandLineArgumentsParser.3.0.11\lib\net45\CommandLineArgumentsParser.dll</HintPath>
37+
<Private>True</Private>
38+
</Reference>
39+
<Reference Include="System" />
40+
<Reference Include="System.Core" />
41+
<Reference Include="System.Xml.Linq" />
42+
<Reference Include="System.Data.DataSetExtensions" />
43+
<Reference Include="Microsoft.CSharp" />
44+
<Reference Include="System.Data" />
45+
<Reference Include="System.Xml" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<Compile Include="encrypter.cs" />
49+
<Compile Include="CryptProgram.cs" />
50+
<Compile Include="Properties\AssemblyInfo.cs" />
51+
</ItemGroup>
52+
<ItemGroup>
53+
<None Include="App.config" />
54+
<None Include="packages.config" />
55+
</ItemGroup>
56+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
57+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
58+
Other similar extension points exist, see Microsoft.Common.targets.
59+
<Target Name="BeforeBuild">
60+
</Target>
61+
<Target Name="AfterBuild">
62+
</Target>
63+
-->
64+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Cryptography")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Cryptography")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("85f02b4a-6a31-4722-89f9-0332ac9909a0")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Cryptography/encrypter.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Security.Cryptography;
7+
using System.IO;
8+
9+
namespace Cryptography
10+
{
11+
12+
public class Encrypter
13+
{
14+
public byte[] randomBytes { get; set; }
15+
16+
public Encrypter(byte[] secretBytes = null)
17+
{
18+
if (secretBytes == null)
19+
secretBytes = new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x5d, 0x75, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 };
20+
this.randomBytes = secretBytes;
21+
}
22+
public string Encrypt(string clearText, string EncryptionKey)
23+
{
24+
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
25+
using (Aes encryptor = Aes.Create()) {
26+
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, this.randomBytes);
27+
encryptor.Key = pdb.GetBytes(32);
28+
encryptor.IV = pdb.GetBytes(16);
29+
using (MemoryStream ms = new MemoryStream()) {
30+
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) {
31+
cs.Write(clearBytes, 0, clearBytes.Length);
32+
cs.Close();
33+
}
34+
clearText = Convert.ToBase64String(ms.ToArray());
35+
}
36+
}
37+
return clearText;
38+
}
39+
public string Decrypt(string cipherText, string EncryptionKey)
40+
{
41+
cipherText = cipherText.Replace(" ", "+");
42+
byte[] cipherBytes = Convert.FromBase64String(cipherText);
43+
using (Aes encryptor = Aes.Create()) {
44+
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, this.randomBytes);
45+
encryptor.Key = pdb.GetBytes(32);
46+
encryptor.IV = pdb.GetBytes(16);
47+
using (MemoryStream ms = new MemoryStream()) {
48+
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) {
49+
cs.Write(cipherBytes, 0, cipherBytes.Length);
50+
cs.Close();
51+
}
52+
cipherText = Encoding.Unicode.GetString(ms.ToArray());
53+
}
54+
}
55+
return cipherText;
56+
}
57+
}
58+
59+
}

Cryptography/packages.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="CommandLineArgumentsParser" version="3.0.11" targetFramework="net45" />
4+
</packages>

0 commit comments

Comments
 (0)