Using C# to solve some exercises
Dev Ghai

Dev Ghai commited on 2013-10-24 18:25:22
Showing 5 changed files, with 398 additions and 0 deletions.

... ...
@@ -0,0 +1,204 @@
1
+
2
+Microsoft Visual Studio Solution File, Format Version 11.00
3
+# Visual Studio 2010
4
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "crypto", "crypto\crypto.csproj", "{B61B534B-99B9-4200-A471-3B31023F541B}"
5
+EndProject
6
+Global
7
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
8
+		Debug|x86 = Debug|x86
9
+		Release|x86 = Release|x86
10
+	EndGlobalSection
11
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
12
+		{B61B534B-99B9-4200-A471-3B31023F541B}.Debug|x86.ActiveCfg = Debug|x86
13
+		{B61B534B-99B9-4200-A471-3B31023F541B}.Debug|x86.Build.0 = Debug|x86
14
+		{B61B534B-99B9-4200-A471-3B31023F541B}.Release|x86.ActiveCfg = Release|x86
15
+		{B61B534B-99B9-4200-A471-3B31023F541B}.Release|x86.Build.0 = Release|x86
16
+	EndGlobalSection
17
+	GlobalSection(MonoDevelopProperties) = preSolution
18
+		StartupItem = crypto\crypto.csproj
19
+		Policies = $0
20
+		$0.DotNetNamingPolicy = $1
21
+		$1.DirectoryNamespaceAssociation = None
22
+		$1.ResourceNamePolicy = FileFormatDefault
23
+		$0.TextStylePolicy = $2
24
+		$2.inheritsSet = VisualStudio
25
+		$2.inheritsScope = text/plain
26
+		$2.scope = text/x-csharp
27
+		$0.CSharpFormattingPolicy = $3
28
+		$3.IndentSwitchBody = True
29
+		$3.AnonymousMethodBraceStyle = NextLine
30
+		$3.PropertyBraceStyle = NextLine
31
+		$3.PropertyGetBraceStyle = NextLine
32
+		$3.PropertySetBraceStyle = NextLine
33
+		$3.EventBraceStyle = NextLine
34
+		$3.EventAddBraceStyle = NextLine
35
+		$3.EventRemoveBraceStyle = NextLine
36
+		$3.StatementBraceStyle = NextLine
37
+		$3.ArrayInitializerBraceStyle = NextLine
38
+		$3.BeforeMethodDeclarationParentheses = False
39
+		$3.BeforeMethodCallParentheses = False
40
+		$3.BeforeConstructorDeclarationParentheses = False
41
+		$3.BeforeDelegateDeclarationParentheses = False
42
+		$3.NewParentheses = False
43
+		$3.inheritsSet = Mono
44
+		$3.inheritsScope = text/x-csharp
45
+		$3.scope = text/x-csharp
46
+		$0.StandardHeader = $4
47
+		$4.Text = 
48
+		$4.IncludeInNewFiles = True
49
+		$0.NameConventionPolicy = $5
50
+		$5.Rules = $6
51
+		$6.NamingRule = $7
52
+		$7.Name = Namespaces
53
+		$7.AffectedEntity = Namespace
54
+		$7.VisibilityMask = VisibilityMask
55
+		$7.NamingStyle = PascalCase
56
+		$7.IncludeInstanceMembers = True
57
+		$7.IncludeStaticEntities = True
58
+		$6.NamingRule = $8
59
+		$8.Name = Types
60
+		$8.AffectedEntity = Class, Struct, Enum, Delegate
61
+		$8.VisibilityMask = VisibilityMask
62
+		$8.NamingStyle = PascalCase
63
+		$8.IncludeInstanceMembers = True
64
+		$8.IncludeStaticEntities = True
65
+		$6.NamingRule = $9
66
+		$9.Name = Interfaces
67
+		$9.RequiredPrefixes = $10
68
+		$10.String = I
69
+		$9.AffectedEntity = Interface
70
+		$9.VisibilityMask = VisibilityMask
71
+		$9.NamingStyle = PascalCase
72
+		$9.IncludeInstanceMembers = True
73
+		$9.IncludeStaticEntities = True
74
+		$6.NamingRule = $11
75
+		$11.Name = Attributes
76
+		$11.RequiredSuffixes = $12
77
+		$12.String = Attribute
78
+		$11.AffectedEntity = CustomAttributes
79
+		$11.VisibilityMask = VisibilityMask
80
+		$11.NamingStyle = PascalCase
81
+		$11.IncludeInstanceMembers = True
82
+		$11.IncludeStaticEntities = True
83
+		$6.NamingRule = $13
84
+		$13.Name = Event Arguments
85
+		$13.RequiredSuffixes = $14
86
+		$14.String = EventArgs
87
+		$13.AffectedEntity = CustomEventArgs
88
+		$13.VisibilityMask = VisibilityMask
89
+		$13.NamingStyle = PascalCase
90
+		$13.IncludeInstanceMembers = True
91
+		$13.IncludeStaticEntities = True
92
+		$6.NamingRule = $15
93
+		$15.Name = Exceptions
94
+		$15.RequiredSuffixes = $16
95
+		$16.String = Exception
96
+		$15.AffectedEntity = CustomExceptions
97
+		$15.VisibilityMask = VisibilityMask
98
+		$15.NamingStyle = PascalCase
99
+		$15.IncludeInstanceMembers = True
100
+		$15.IncludeStaticEntities = True
101
+		$6.NamingRule = $17
102
+		$17.Name = Methods
103
+		$17.AffectedEntity = Methods
104
+		$17.VisibilityMask = VisibilityMask
105
+		$17.NamingStyle = PascalCase
106
+		$17.IncludeInstanceMembers = True
107
+		$17.IncludeStaticEntities = True
108
+		$6.NamingRule = $18
109
+		$18.Name = Static Readonly Fields
110
+		$18.AffectedEntity = ReadonlyField
111
+		$18.VisibilityMask = Internal, Protected, Public
112
+		$18.NamingStyle = PascalCase
113
+		$18.IncludeInstanceMembers = False
114
+		$18.IncludeStaticEntities = True
115
+		$6.NamingRule = $19
116
+		$19.Name = Fields (Non Private)
117
+		$19.AffectedEntity = Field
118
+		$19.VisibilityMask = Internal, Protected, Public
119
+		$19.NamingStyle = PascalCase
120
+		$19.IncludeInstanceMembers = True
121
+		$19.IncludeStaticEntities = True
122
+		$6.NamingRule = $20
123
+		$20.Name = ReadOnly Fields (Non Private)
124
+		$20.AffectedEntity = ReadonlyField
125
+		$20.VisibilityMask = Internal, Protected, Public
126
+		$20.NamingStyle = PascalCase
127
+		$20.IncludeInstanceMembers = True
128
+		$20.IncludeStaticEntities = False
129
+		$6.NamingRule = $21
130
+		$21.Name = Fields (Private)
131
+		$21.AllowedPrefixes = $22
132
+		$22.String = _
133
+		$22.String = m_
134
+		$21.AffectedEntity = Field, ReadonlyField
135
+		$21.VisibilityMask = Private
136
+		$21.NamingStyle = CamelCase
137
+		$21.IncludeInstanceMembers = True
138
+		$21.IncludeStaticEntities = False
139
+		$6.NamingRule = $23
140
+		$23.Name = Static Fields (Private)
141
+		$23.AffectedEntity = Field
142
+		$23.VisibilityMask = Private
143
+		$23.NamingStyle = CamelCase
144
+		$23.IncludeInstanceMembers = False
145
+		$23.IncludeStaticEntities = True
146
+		$6.NamingRule = $24
147
+		$24.Name = ReadOnly Fields (Private)
148
+		$24.AllowedPrefixes = $25
149
+		$25.String = _
150
+		$25.String = m_
151
+		$24.AffectedEntity = ReadonlyField
152
+		$24.VisibilityMask = Private
153
+		$24.NamingStyle = CamelCase
154
+		$24.IncludeInstanceMembers = True
155
+		$24.IncludeStaticEntities = False
156
+		$6.NamingRule = $26
157
+		$26.Name = Constant Fields
158
+		$26.AffectedEntity = ConstantField
159
+		$26.VisibilityMask = VisibilityMask
160
+		$26.NamingStyle = PascalCase
161
+		$26.IncludeInstanceMembers = True
162
+		$26.IncludeStaticEntities = True
163
+		$6.NamingRule = $27
164
+		$27.Name = Properties
165
+		$27.AffectedEntity = Property
166
+		$27.VisibilityMask = VisibilityMask
167
+		$27.NamingStyle = PascalCase
168
+		$27.IncludeInstanceMembers = True
169
+		$27.IncludeStaticEntities = True
170
+		$6.NamingRule = $28
171
+		$28.Name = Events
172
+		$28.AffectedEntity = Event
173
+		$28.VisibilityMask = VisibilityMask
174
+		$28.NamingStyle = PascalCase
175
+		$28.IncludeInstanceMembers = True
176
+		$28.IncludeStaticEntities = True
177
+		$6.NamingRule = $29
178
+		$29.Name = Enum Members
179
+		$29.AffectedEntity = EnumMember
180
+		$29.VisibilityMask = VisibilityMask
181
+		$29.NamingStyle = PascalCase
182
+		$29.IncludeInstanceMembers = True
183
+		$29.IncludeStaticEntities = True
184
+		$6.NamingRule = $30
185
+		$30.Name = Parameters
186
+		$30.AffectedEntity = Parameter
187
+		$30.VisibilityMask = VisibilityMask
188
+		$30.NamingStyle = CamelCase
189
+		$30.IncludeInstanceMembers = True
190
+		$30.IncludeStaticEntities = True
191
+		$6.NamingRule = $31
192
+		$31.Name = Type Parameters
193
+		$31.RequiredPrefixes = $32
194
+		$32.String = T
195
+		$31.AffectedEntity = TypeParameter
196
+		$31.VisibilityMask = VisibilityMask
197
+		$31.NamingStyle = PascalCase
198
+		$31.IncludeInstanceMembers = True
199
+		$31.IncludeStaticEntities = True
200
+	EndGlobalSection
201
+	GlobalSection(SolutionProperties) = preSolution
202
+		HideSolutionNode = FALSE
203
+	EndGlobalSection
204
+EndGlobal
... ...
@@ -0,0 +1,103 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using System.Text;
5
+
6
+namespace crypto
7
+{
8
+    class Week5
9
+    {
10
+        //Which of the following numbers is a generator of Z13∗?
11
+        //8,⟨8⟩={1,8,12,5}
12
+        //4,⟨4⟩={1,4,3,12,9,10}
13
+        //2,⟨2⟩={1,2,4,8,3,6,12,11,9,5,10,7}
14
+        //7,⟨7⟩={1,7,10,5,9,11,12,6,3,8,4,2}
15
+        //3,⟨3⟩={1,3,9}
16
+        private void PrintSet(List<int> set)
17
+        {
18
+            for (int i = 0; i < set.Count; i++)
19
+            {
20
+                Console.Write("{0}, ", set[i]);
21
+            }
22
+        }
23
+        public bool IsGenerator(int g, int prime, List<int> set, bool print)
24
+        {
25
+            bool isGen = false;
26
+            //Array of list of ints
27
+            List<int> cyclicSet = new List<int>(prime - 1);
28
+            for (int i = 1; i < prime; i++)
29
+            {
30
+                cyclicSet.Add(i);
31
+            }
32
+
33
+            if (cyclicSet.Intersect(set).Count() == cyclicSet.Count)
34
+            {
35
+                isGen = true;
36
+            }
37
+
38
+            if (print)
39
+            {
40
+                Console.WriteLine("Generator = {0}, \ntest set: ", g);
41
+                this.PrintSet(set);
42
+                Console.WriteLine("\nGenerated Set:");
43
+                this.PrintSet(cyclicSet);
44
+                Console.WriteLine("\nReturning: {0}\n============", isGen);
45
+            }
46
+
47
+            return isGen;
48
+        }
49
+
50
+        public int GetOrder(int generator, int baseNum)
51
+        {
52
+            int exponent = 1;
53
+            while (Math.Pow(generator, exponent) % baseNum != 1)
54
+            {
55
+                exponent++;
56
+            }
57
+            return exponent;
58
+        }
59
+
60
+        public int GetSquareRoot(int number, int primeBase)
61
+        {
62
+            int root = 1;
63
+            while ((int)Math.Pow(root, 2) % primeBase != number)
64
+            {
65
+                root++;
66
+            }
67
+            return root;
68
+        }
69
+
70
+        public double GetMultiplier(int x, int baseNum, int result)
71
+        {
72
+            int i=1;
73
+            //Num of elements in set with baseNum
74
+            while (Math.Pow (x, i) % baseNum != result)
75
+            {
76
+                i++;
77
+            }
78
+            return Math.Pow (x, i-1) % baseNum;
79
+        }
80
+    }
81
+
82
+    class Crypto
83
+    {
84
+        static void Main(string[] args)
85
+        {
86
+            Week5 w = new Week5();
87
+//            int prime = 13;
88
+//            bool printSets = true;
89
+//            w.IsGenerator(6, prime, new List<int>() {1,6,10,8,9,2,12,7,3,5,4,11}, printSets);
90
+//            w.IsGenerator(4, prime, new List<int>() {1,4,3,12,9,10}, printSets);
91
+//            w.IsGenerator(3, prime, new List<int>() {1,3,9}, printSets);
92
+//            w.IsGenerator(8, prime, new List<int>() {1,8,12,5}, printSets);
93
+//            w.IsGenerator(7, prime, new List<int>() {1,7,10,5,9,11,12,6,3,8,4,2}, printSets);
94
+            
95
+//            Console.WriteLine("Order of 2 in Z35: ", w.GetOrder(2, 35));
96
+
97
+//            Console.WriteLine("Square root of 12 in Z23 is {0}", w.GetSquareRoot(12, 23));
98
+//            Console.WriteLine ("Inverse of {0} in Z{1} is: {2}", 7, 23, w.GetMultiplier(7, 23, 1));
99
+            Console.WriteLine("Inverse of 3, such that 3x=5 in Z19 is {0}", w.GetMultiplier(3, 19, 5));
100
+            Console.ReadKey();
101
+        }
102
+    }
103
+}
... ...
@@ -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("crypto")]
9
+[assembly: AssemblyDescription("")]
10
+[assembly: AssemblyConfiguration("")]
11
+[assembly: AssemblyCompany("")]
12
+[assembly: AssemblyProduct("crypto")]
13
+[assembly: AssemblyCopyright("Copyright ©  2013")]
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("d67b5016-14c0-4b18-9bd5-e9d06c0e40f8")]
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")]
... ...
@@ -0,0 +1,55 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3
+  <PropertyGroup>
4
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5
+    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6
+    <ProductVersion>8.0.30703</ProductVersion>
7
+    <SchemaVersion>2.0</SchemaVersion>
8
+    <ProjectGuid>{B61B534B-99B9-4200-A471-3B31023F541B}</ProjectGuid>
9
+    <OutputType>Exe</OutputType>
10
+    <AppDesignerFolder>Properties</AppDesignerFolder>
11
+    <RootNamespace>crypto</RootNamespace>
12
+    <AssemblyName>crypto</AssemblyName>
13
+    <FileAlignment>512</FileAlignment>
14
+  </PropertyGroup>
15
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
16
+    <PlatformTarget>x86</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|x86' ">
26
+    <PlatformTarget>x86</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="System" />
36
+    <Reference Include="System.Core" />
37
+    <Reference Include="System.Xml.Linq" />
38
+    <Reference Include="System.Data.DataSetExtensions" />
39
+    <Reference Include="Microsoft.CSharp" />
40
+    <Reference Include="System.Data" />
41
+    <Reference Include="System.Xml" />
42
+  </ItemGroup>
43
+  <ItemGroup>
44
+    <Compile Include="Program.cs" />
45
+    <Compile Include="Properties\AssemblyInfo.cs" />
46
+  </ItemGroup>
47
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
48
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
49
+       Other similar extension points exist, see Microsoft.Common.targets.
50
+  <Target Name="BeforeBuild">
51
+  </Target>
52
+  <Target Name="AfterBuild">
53
+  </Target>
54
+  -->
55
+</Project>
0 56
\ No newline at end of file
1 57