source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/BMX41000/IHS BMX Framework/IndianHealthService.BMXNet/Net/BMXAdeCrypto.cs@ 1551

Last change on this file since 1551 was 1146, checked in by Sam Habiel, 14 years ago

Initial Import of BMX4

File size: 7.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace IndianHealthService.BMXNet.Net
6{
7 /// <summary>
8 /// This implementation EncryptionProvider is the crypto used for the standard Indian Health Service socket-based BMXNET Broker.
9 /// </summary>
10 public sealed class BMXAdeCrypto : EncryptionProvider
11 {
12 public static EncryptionProvider Keyed(String[] aKey)
13 {
14 BMXAdeCrypto answer = new BMXAdeCrypto();
15 answer.SetKey(aKey);
16 return answer;
17 }
18
19 public BMXAdeCrypto()
20 {
21 }
22
23 private string[] DefaultKey
24 {
25 get
26 {
27 return new string[]
28 {
29 @"wkEo-ZJt!dG)49K{nX1BS$vH<&:Myf*>Ae0jQW=;|#PsO`'%+rmb[gpqN,l6/hFC@DcUa ]z~R}""V\iIxu?872.(TYL5_3",
30 @"rKv`R;M/9BqAF%&tSs#Vh)dO1DZP> *fX'u[.4lY=-mg_ci802N7LTG<]!CWo:3?{+,5Q}(@jaExn$~p\IyHwzU""|k6Jeb",
31 @"\pV(ZJk""WQmCn!Y,y@1d+~8s?[lNMxgHEt=uw|X:qSLjAI*}6zoF{T3#;ca)/h5%`P4$r]G'9e2if_>UDKb7<v0&- RBO.",
32 @"depjt3g4W)qD0V~NJar\B ""?OYhcu[<Ms%Z`RIL_6:]AX-zG.#}$@vk7/5x&*m;(yb2Fn+l'PwUof1K{9,|EQi>H=CT8S!",
33 @"NZW:1}K$byP;jk)7'`x90B|cq@iSsEnu,(l-hf.&Y_?J#R]+voQXU8mrV[!p4tg~OMez CAaGFD6H53%L/dT2<*>""{\wI=",
34 @"vCiJ<oZ9|phXVNn)m K`t/SI%]A5qOWe\&?;jT~M!fz1l>[D_0xR32c*4.P""G{r7}E8wUgyudF+6-:B=$(sY,LkbHa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e/Y<B:xwojR,Vh]O0Sc[`$sg8GXE!1&Qrzp._W%TNK(=J 3i*2abuHA4C'?Mv\Pq{n#56LftUl@9+",
46 @"~A*>9 WidFN,1KsmwQ)GJM{I4:C%}#Ep(?HB/r;t.&U8o|l['Lg""2hRDyZ5`nbf]qjc0!zS-TkYO<_=76a\X@$Pe3+xVvu",
47 @"yYgjf""5VdHc#uA,W1i+v'6|@pr{n;DJ!8(btPGaQM.LT3oe?NB/&9>Z`-}02*%x<7lsqz4OS ~E$\R]KI[:UwC_=h)kXmF",
48 @"5:iar.{YU7mBZR@-K|2 ""+~`M%8sq4JhPo<_X\Sg3WC;Tuxz,fvEQ1p9=w}FAI&j/keD0c?)LN6OHV]lGy'$*>nd[(tb!#",
49 };
50 }
51 }
52
53 /// <summary>
54 /// Set a site specific key
55 /// </summary>
56 /// <param name="aKey">The required key for BMX Ade</param>
57 public void SetKey(String[] aKey)
58 {
59 this.Key = aKey;
60 }
61
62 private string[] _key = null;
63
64 private string[] Key
65 {
66 get {return (_key == null) ? this.DefaultKey : this._key; }
67 set { _key = value; }
68 }
69
70 /// <summary>
71 /// BMXAde Implementation
72 /// </summary>
73 /// <param name="aString">String to encrypt</param>
74 /// <returns>Encrypt string</returns>
75 public string Encrypt(string aString)
76 {
77 return this.ADEEncryp(aString);
78 }
79
80 /// <summary>
81 /// BMXAde Implementation
82 /// </summary>
83 /// <param name="aString">String to decrypt</param>
84 /// <returns>Decrypted string</returns>
85 public string Decrypt(string aString)
86 {
87 return this.ADEDecryp(aString);
88 }
89
90
91 private string ADEEncryp(string sInput)
92 {
93 //Encrypt a string
94 string strPercent;
95 string strAssoc;
96 string strIdix;
97 int nPercent;
98 int nAssocix;
99 int nIdix;
100
101 System.Random rRand = new Random(DateTime.Now.Second);
102
103 nPercent = rRand.Next(0, 10000);
104 nPercent += 72439;
105 nAssocix = nPercent % 20;
106 nAssocix++;
107 this.Assert(nAssocix < 21, "Associx to large");
108 strPercent = nPercent.ToString();
109 strPercent = strPercent.Substring(1, 2);
110 nIdix = Convert.ToInt32(strPercent);
111 nIdix = nIdix % 20;
112 nIdix++;
113 this.Assert(nIdix < 21, "Idix to large");
114
115 const int nEncryptBase = 101;
116 strAssoc = LoadKey(nEncryptBase + nAssocix);
117 this.Assert(strAssoc.Length == 94, "Associator length invalid");
118 strIdix = LoadKey(nEncryptBase + nIdix);
119 this.Assert(strAssoc.Length == 94, "Identifier length invalid");
120 string sEncrypted = "";
121
122 foreach (char c in sInput)
123 {
124 string d = c.ToString();
125 int nFindChar = strIdix.IndexOf(c);
126 if (nFindChar > -1)
127 {
128 d = strAssoc.Substring(nFindChar, 1);
129 }
130 sEncrypted += d;
131 }
132
133 String debugPreview = (char)(nIdix + 31) + sEncrypted + (char)(nAssocix + 31);
134 return debugPreview;
135 }
136
137
138 private string LoadKey(int nID)
139 {
140 nID -= 102;
141
142 this.Assert(nID < 20, "Key array length too long");
143
144 return this.Key[nID];
145 }
146
147 private void Assert(bool aBoolean, String aMessage)
148 {
149 if (!aBoolean)
150 {
151 throw new BMXNetException("BmxAdeEncryptionProvider Assertion failed: " + aMessage);
152 }
153 }
154
155 private string ADEDecryp(string sInput)
156 {
157 //Encrypt a string
158 string strAssoc;
159 string strIdix;
160 int nAssocix;
161 int nIdix;
162 this.Assert(sInput != "", "Unable to encrypt empty string");
163
164 //get associator string index
165 char cAssocix = sInput[sInput.Length - 1];
166 nAssocix = (int)cAssocix;
167 nAssocix -= 31;
168 this.Assert(nAssocix < 21, "Associx to large");
169
170
171 //get identifier string index
172 char cIdix = sInput[0];
173 nIdix = (int)cIdix;
174 nIdix -= 31;
175 this.Assert(nIdix < 21, "Idix to large");
176
177 //get associator string
178 const int nEncryptBase = 101;
179 strAssoc = LoadKey(nEncryptBase + nAssocix);
180 this.Assert(strAssoc.Length == 94, "Associator length invalid");
181
182 //get identifier string
183 strIdix = LoadKey(nEncryptBase + nIdix);
184 this.Assert(strAssoc.Length == 94, "Identifier length invalid");
185
186 //translated result
187 string sDecrypted = "";
188 sInput = sInput.Substring(1, sInput.Length - 2);
189 foreach (char c in sInput)
190 {
191 string d = c.ToString();
192 int nFindChar = strAssoc.IndexOf(c);
193 if (nFindChar > -1)
194 {
195 d = strIdix.Substring(nFindChar, 1);
196 }
197 sDecrypted += d;
198 }
199
200 return sDecrypted;
201 }
202 }
203}
Note: See TracBrowser for help on using the repository browser.