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