source: trunk/CrypPlugins/KeySearcher/OpenCL/KeySearcherOpenCLCode.cs @ 2295

Last change on this file since 2295 was 2295, checked in by Sven Rech, 11 years ago

Prepared KeySearcher for Localization

File size: 3.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Cryptool.PluginBase;
6using Cryptool.PluginBase.Control;
7using KeySearcher.Properties;
8using OpenCLNet;
9
10namespace KeySearcher
11{
12    class KeySearcherOpenCLCode
13    {
14        private readonly KeySearcher keySearcher;
15        private byte[] encryptedData;
16        private IControlCost controlCost;
17        private IControlEncryption encryptionController;
18        private int approximateNumberOfKeys;
19
20        private IKeyTranslator keyTranslatorOfCode = null;
21        private string openCLCode = null;
22        private Kernel openCLKernel = null;
23
24        public KeySearcherOpenCLCode(KeySearcher keySearcher, byte[] encryptedData, IControlEncryption encryptionController, IControlCost controlCost, int approximateNumberOfKeys)
25        {
26            this.keySearcher = keySearcher;
27            this.encryptedData = encryptedData;
28            this.encryptionController = encryptionController;
29            this.controlCost = controlCost;
30            this.approximateNumberOfKeys = approximateNumberOfKeys;
31        }
32
33        public string CreateOpenCLBruteForceCode(IKeyTranslator keyTranslator)
34        {
35            if (keyTranslatorOfCode == keyTranslator)
36            {
37                return openCLCode;
38            }
39
40            int bytesUsed = controlCost.getBytesToUse();
41            if (encryptedData.Length < bytesUsed)
42                bytesUsed = encryptedData.Length;
43
44            string code = encryptionController.GetOpenCLCode(bytesUsed);
45            if (code == null)
46                throw new Exception("OpenCL not supported in this configuration!");
47
48            //put cost function stuff into code:
49            code = controlCost.ModifyOpenCLCode(code);
50
51            //put input to be bruteforced into code:
52            string inputarray = string.Format("__constant unsigned char inn[{0}] = {{ \n", bytesUsed);
53            for (int i = 0; i < bytesUsed; i++)
54            {
55                inputarray += String.Format("0x{0:X2}, ", this.encryptedData[i]);
56            }
57            inputarray = inputarray.Substring(0, inputarray.Length - 2);
58            inputarray += "}; \n";
59            code = code.Replace("$$INPUTARRAY$$", inputarray);
60
61            //put key movement of pattern into code:
62            code = keyTranslator.ModifyOpenCLCode(code, approximateNumberOfKeys);
63
64            keyTranslatorOfCode = keyTranslator;
65            this.openCLCode = code;
66           
67            return code;
68        }
69
70
71        public Kernel GetBruteforceKernel(OpenCLManager oclManager, IKeyTranslator keyTranslator)
72        {
73            if (keyTranslatorOfCode == keyTranslator)
74            {
75                return openCLKernel;
76            }
77
78            try
79            {
80                var program = oclManager.CompileSource(CreateOpenCLBruteForceCode(keyTranslator));
81                //keySearcher.GuiLogMessage(string.Format("Using OpenCL with (virtually) {0} threads.", keyTranslator.GetOpenCLBatchSize()), NotificationLevel.Info);
82                openCLKernel = program.CreateKernel("bruteforceKernel");
83                return openCLKernel;
84            }
85            catch (Exception ex)
86            {
87                throw new Exception(Resources.An_error_occured_when_trying_to_compile_OpenCL_code__ + ex.Message);
88            }
89        }
90    }
91}
Note: See TracBrowser for help on using the repository browser.