source: trunk/CrypPlugins/KeySearcher_IControl/KeySearcher_IControl.cs @ 1448

Last change on this file since 1448 was 1448, checked in by Sven Rech, 12 years ago

replaced all BigInteger stuff with the new BigInteger class from .net 4.0

But there are still problems with some plugins (Keysearcher, BigInteger Operations...)

File size: 8.2 KB
Line 
1/*
2   Copyright 2009 Christian Arnold, Universität Duisburg-Essen
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using Cryptool.PluginBase.IO;
22using Cryptool.PluginBase;
23using System.ComponentModel;
24using Cryptool.PluginBase.Miscellaneous;
25using System.Windows.Controls;
26using Cryptool.PluginBase.Analysis;
27// KeyPattern kicked out of this project and will be sourced from the namespace KeySearcher
28using KeySearcher;
29using Cryptool.PluginBase.Control;
30using Cryptool.Plugins.PeerToPeer;
31using Cryptool.Plugins.PeerToPeer.Jobs;
32using System.Threading;
33using System.Numerics;
34
35namespace Cryptool.Plugins.KeySearcher_IControl
36{
37    [Author("Christian Arnold", "arnold@cryptool.org", "Universität Duisburg-Essen, Fachgebiet Verteilte Systeme", "http://www.vs.uni-due.de")]
38    [PluginInfo(false, "Peer-based KeySearcher", "Peer-driven KeySearcher. KeySearcher gets the search pattern from a managing peer.", null, "KeySearcher_IControl/P2PKeySearcher.png")]
39    public class KeySearcher_IControl : KeySearcher.KeySearcher, IAnalysisMisc
40    {
41        //only change: mandatory = false!!!
42        [PropertyInfo(Direction.InputData, "Encrypted Data", "Encrypted data out of an Encryption PlugIn", "", false, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, "")]
43        public override byte[] EncryptedData
44        {
45            get
46            {
47                return base.EncryptedData;
48            }
49            set
50            {
51                base.EncryptedData = value;
52            }
53        }
54
55        //only change: mandatory = false!!!
56        [PropertyInfo(Direction.InputData, "Initialization Vector", "Initialization vector with which the data were encrypted", "",false,false, DisplayLevel.Beginner,QuickWatchFormat.Hex,"")]
57        public override byte[] InitVector
58        {
59            get
60            {
61                return base.InitVector;
62            }
63            set
64            {
65                base.InitVector = value;
66            }
67        }
68
69        #region IKeySearcherControl Members
70
71        private IControlWorker controlWorker;
72        [PropertyInfo(Direction.ControlSlave, "Master Worker", "For distributed job processing", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
73        public IControlWorker ControlWorker
74        {
75            get
76            {
77                if (this.controlWorker == null)
78                {
79                    // to commit the settings of the plugin to the IControl
80                    this.controlWorker = new KeySearcherMaster(this);
81                }
82                return this.controlWorker;
83            }
84        }
85
86        #endregion
87    }
88
89    public class KeySearcherMaster : IControlWorker
90    {
91        #region processing and result variables and properties
92       
93        private BigInteger JobId;
94        private DateTime dtStartProcessing;
95        private DateTime dtEndProcessing;
96
97        #endregion
98
99        private KeySearcher_IControl keySearcher;
100
101        public KeySearcherMaster(KeySearcher_IControl keysearcher)
102        {
103            this.keySearcher = keysearcher;
104            this.keySearcher.OnBruteforcingEnded += new KeySearcher.KeySearcher.BruteforcingEnded(keySearcher_OnBruteforcingEnded);
105        }
106
107        public IControlEncryption GetEncyptionControl()
108        {
109            return this.keySearcher.ControlMaster;
110        }
111
112        public IControlCost GetCostControl()
113        {
114            return this.keySearcher.CostMaster;
115        }
116
117        void keySearcher_OnBruteforcingEnded(LinkedList<KeySearcher.KeySearcher.ValueKey> top10List)
118        {
119            //wacker: you need to test if BOTH conditions apply, hence &&
120            //if (this.JobId != null || top10List != null)
121            if (this.JobId != null && top10List != null)
122            {
123                this.dtEndProcessing = DateTime.Now;
124                // Create a new JobResult
125                TimeSpan processingTime = this.dtEndProcessing.Subtract(this.dtStartProcessing);
126                KeyPatternJobResult jobResult =
127                    new KeyPatternJobResult(this.JobId, top10List, processingTime);
128
129                GuiLogging("Ended bruteforcing JobId '" + this.JobId.ToString() + "' in "
130                    + processingTime.TotalMinutes.ToString() + " minutes", NotificationLevel.Info);
131
132                // if registered, sending the serialized Job Result
133                if (OnProcessingSuccessfullyEnded != null)
134                {
135                    OnProcessingSuccessfullyEnded(this.JobId, jobResult.Serialize());
136                }
137            }
138            else
139            {
140                GuiLogging("Bruteforcing was canceled, because jobId and/or jobResult are null.", NotificationLevel.Info);
141                if (OnProcessingCanceled != null)
142                    OnProcessingCanceled(null);
143            }
144        }
145
146        #region IControl Members
147
148        public event IControlStatusChangedEventHandler OnStatusChanged;
149
150        #endregion
151
152        #region IControlWorker Members
153        public event ProcessingSuccessfullyEnded OnProcessingSuccessfullyEnded;
154        public event ProcessingCanceled OnProcessingCanceled;
155        public event InfoText OnInfoTextReceived;
156
157        string sTopicName = String.Empty;
158        public string TopicName
159        {
160            get { return this.sTopicName; }
161            set { this.sTopicName = value; }
162        }
163
164        public bool StartProcessing(byte[] job, out BigInteger jobId)
165        {
166            jobId = -1; //out parameter
167            if (job != null)
168            {
169                KeyPatternJobPart jobPart = new KeyPatternJobPart(job);
170                if (jobPart != null)
171                {
172                    this.JobId = jobPart.JobId;
173                    // fill out parameter
174                    jobId = this.JobId;
175                    this.dtStartProcessing = DateTime.Now;
176
177                    GuiLogging("Deserializing job with id '" + jobPart.JobId + "' was successful. Start bruteforcing the KeyPattern '" + jobPart.Pattern.WildcardKey + "'", NotificationLevel.Info);
178
179                    // call bruteforcing method in a thread, so this method didn't block the flow
180                    Thread bruteforcingThread = new Thread(StartBruteforcing);
181                    bruteforcingThread.Start(jobPart);
182
183                    return true;
184                }
185                else
186                {
187                    GuiLogging("The received job byte[] wasn't null, but couldn't be deserialized!", NotificationLevel.Warning);
188                }
189            }
190            else
191            {
192                GuiLogging("Received job byte[] was null. Nothing to do.", NotificationLevel.Warning);
193            }
194            return false;
195        }
196
197        private void StartBruteforcing(object what)
198        {
199            if (what is KeyPatternJobPart)
200            {
201                KeyPatternJobPart jobPart = what as KeyPatternJobPart;
202                this.keySearcher.BruteforcePattern(jobPart.Pattern, jobPart.EncryptData, jobPart.InitVector,
203                            this.keySearcher.ControlMaster, this.keySearcher.CostMaster);
204            }
205            else
206                throw(new Exception("Bruteforcing object wasn't from Type 'KeyPatternJobPart'!"));
207        }
208
209        public void StopProcessing()
210        {
211            this.keySearcher.Stop();
212        }
213
214        private void GuiLogging(string sText, NotificationLevel notLevel)
215        {
216            if (OnInfoTextReceived != null)
217                OnInfoTextReceived(sText, notLevel);
218        }
219
220        #endregion
221    }
222}
Note: See TracBrowser for help on using the repository browser.