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

Last change on this file since 1154 was 1154, checked in by Arno Wacker, 12 years ago

KeySearcher_IControl

  • Fixed a null pointer exception when a wrong key pattern is received (jobResult/top10List == null)
  • Still unclear/bug: Why is the list null?? This needs quick fixing!
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;
33
34namespace Cryptool.Plugins.KeySearcher_IControl
35{
36    [Author("Christian Arnold", "arnold@cryptool.org", "Universität Duisburg-Essen, Fachgebiet Verteilte Systeme", "http://www.vs.uni-due.de")]
37    [PluginInfo(false, "Peer-based KeySearcher", "Peer-driven KeySearcher. KeySearcher gets the search pattern from a managing peer.", null, "KeySearcher_IControl/P2PKeySearcher.png")]
38    public class KeySearcher_IControl : KeySearcher.KeySearcher, IAnalysisMisc
39    {
40        //only change: mandatory = false!!!
41        [PropertyInfo(Direction.InputData, "Encrypted Data", "Encrypted data out of an Encryption PlugIn", "", false, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, "")]
42        public override byte[] EncryptedData
43        {
44            get
45            {
46                return base.EncryptedData;
47            }
48            set
49            {
50                base.EncryptedData = value;
51            }
52        }
53
54        //only change: mandatory = false!!!
55        [PropertyInfo(Direction.InputData, "Initialization Vector", "Initialization vector with which the data were encrypted", "",false,false, DisplayLevel.Beginner,QuickWatchFormat.Hex,"")]
56        public override byte[] InitVector
57        {
58            get
59            {
60                return base.InitVector;
61            }
62            set
63            {
64                base.InitVector = value;
65            }
66        }
67
68        #region IKeySearcherControl Members
69
70        private IControlWorker controlWorker;
71        [PropertyInfo(Direction.ControlSlave, "Master Worker", "For distributed job processing", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
72        public IControlWorker ControlWorker
73        {
74            get
75            {
76                if (this.controlWorker == null)
77                {
78                    // to commit the settings of the plugin to the IControl
79                    this.controlWorker = new KeySearcherMaster(this);
80                }
81                return this.controlWorker;
82            }
83        }
84
85        #endregion
86    }
87
88    public class KeySearcherMaster : IControlWorker
89    {
90        #region processing and result variables and properties
91       
92        private BigInteger JobId;
93        private DateTime dtStartProcessing;
94        private DateTime dtEndProcessing;
95
96        #endregion
97
98        private KeySearcher_IControl keySearcher;
99
100        public KeySearcherMaster(KeySearcher_IControl keysearcher)
101        {
102            this.keySearcher = keysearcher;
103            this.keySearcher.OnBruteforcingEnded += new KeySearcher.KeySearcher.BruteforcingEnded(keySearcher_OnBruteforcingEnded);
104        }
105
106        public IControlEncryption GetEncyptionControl()
107        {
108            return this.keySearcher.ControlMaster;
109        }
110
111        public IControlCost GetCostControl()
112        {
113            return this.keySearcher.CostMaster;
114        }
115
116        void keySearcher_OnBruteforcingEnded(LinkedList<KeySearcher.KeySearcher.ValueKey> top10List)
117        {
118            //wacker: you need to test if BOTH conditions apply, hence &&
119            //if (this.JobId != null || top10List != null)
120            if (this.JobId != null && top10List != null)
121            {
122                this.dtEndProcessing = DateTime.Now;
123                // Create a new JobResult
124                TimeSpan processingTime = this.dtEndProcessing.Subtract(this.dtStartProcessing);
125                KeyPatternJobResult jobResult =
126                    new KeyPatternJobResult(this.JobId, top10List, processingTime);
127
128                GuiLogging("Ended bruteforcing JobId '" + this.JobId.ToString() + "' in "
129                    + processingTime.TotalMinutes.ToString() + " minutes", NotificationLevel.Info);
130
131                // if registered, sending the serialized Job Result
132                if (OnProcessingSuccessfullyEnded != null)
133                {
134                    OnProcessingSuccessfullyEnded(this.JobId, jobResult.Serialize());
135                }
136            }
137            else
138            {
139                GuiLogging("Bruteforcing was canceled, because jobId and/or jobResult are null.", NotificationLevel.Info);
140                if (OnProcessingCanceled != null)
141                    OnProcessingCanceled(null);
142            }
143        }
144
145        #region IControl Members
146
147        public event IControlStatusChangedEventHandler OnStatusChanged;
148
149        #endregion
150
151        #region IControlWorker Members
152        public event ProcessingSuccessfullyEnded OnProcessingSuccessfullyEnded;
153        public event ProcessingCanceled OnProcessingCanceled;
154        public event InfoText OnInfoTextReceived;
155
156        string sTopicName = String.Empty;
157        public string TopicName
158        {
159            get { return this.sTopicName; }
160            set { this.sTopicName = value; }
161        }
162
163        public bool StartProcessing(byte[] job, out BigInteger jobId)
164        {
165            jobId = null; //out parameter
166            if (job != null)
167            {
168                KeyPatternJobPart jobPart = new KeyPatternJobPart(job);
169                if (jobPart != null)
170                {
171                    this.JobId = jobPart.JobId;
172                    // fill out parameter
173                    jobId = this.JobId;
174                    this.dtStartProcessing = DateTime.Now;
175
176                    GuiLogging("Deserializing job with id '" + jobPart.JobId + "' was successful. Start bruteforcing the KeyPattern '" + jobPart.Pattern.WildcardKey + "'", NotificationLevel.Info);
177
178                    // call bruteforcing method in a thread, so this method didn't block the flow
179                    Thread bruteforcingThread = new Thread(StartBruteforcing);
180                    bruteforcingThread.Start(jobPart);
181
182                    return true;
183                }
184                else
185                {
186                    GuiLogging("The received job byte[] wasn't null, but couldn't be deserialized!", NotificationLevel.Warning);
187                }
188            }
189            else
190            {
191                GuiLogging("Received job byte[] was null. Nothing to do.", NotificationLevel.Warning);
192            }
193            return false;
194        }
195
196        private void StartBruteforcing(object what)
197        {
198            if (what is KeyPatternJobPart)
199            {
200                KeyPatternJobPart jobPart = what as KeyPatternJobPart;
201                this.keySearcher.BruteforcePattern(jobPart.Pattern, jobPart.EncryptData, jobPart.InitVector,
202                            this.keySearcher.ControlMaster, this.keySearcher.CostMaster);
203            }
204            else
205                throw(new Exception("Bruteforcing object wasn't from Type 'KeyPatternJobPart'!"));
206        }
207
208        public void StopProcessing()
209        {
210            this.keySearcher.Stop();
211        }
212
213        private void GuiLogging(string sText, NotificationLevel notLevel)
214        {
215            if (OnInfoTextReceived != null)
216                OnInfoTextReceived(sText, notLevel);
217        }
218
219        #endregion
220    }
221}
Note: See TracBrowser for help on using the repository browser.