using System;
using System.Collections.Generic;
using System.Text;
namespace IndianHealthService.BMXNet.Net
{
///
/// This implementation EncryptionProvider is the crypto used for the standard Indian Health Service socket-based BMXNET Broker.
///
public sealed class BMXAdeCrypto : EncryptionProvider
{
public static EncryptionProvider Keyed(String[] aKey)
{
BMXAdeCrypto answer = new BMXAdeCrypto();
answer.SetKey(aKey);
return answer;
}
public BMXAdeCrypto()
{
}
private string[] DefaultKey
{
get
{
return new string[]
{
@"wkEo-ZJt!dG)49K{nX1BS$vH<&:Myf*>Ae0jQW=;|#PsO`'%+rmb[gpqN,l6/hFC@DcUa ]z~R}""V\iIxu?872.(TYL5_3",
@"rKv`R;M/9BqAF%&tSs#Vh)dO1DZP> *fX'u[.4lY=-mg_ci802N7LTG<]!CWo:3?{+,5Q}(@jaExn$~p\IyHwzU""|k6Jeb",
@"\pV(ZJk""WQmCn!Y,y@1d+~8s?[lNMxgHEt=uw|X:qSLjAI*}6zoF{T3#;ca)/h5%`P4$r]G'9e2if_>UDKb7H=CT8S!",
@"NZW:1}K$byP;jk)7'`x90B|cq@iSsEnu,(l-hf.&Y_?J#R]+voQXU8mrV[!p4tg~OMez CAaGFD6H53%L/dT2<*>""{\wI=",
@"vCiJ[D_0xR32c*4.P""G{r7}E8wUgyudF+6-:B=$(sY,LkbHa#'@Q",
@"hvMX,'4Ty;[a8/{6l~F_V""}qLI\!@x(D7bRmUH]W15J%N0BYPkrs&9:$)Zj>u|zwQ=ieC-oGA.#?tfdcO3gp`S+En K2*<",
@"jd!W5[];4'?ghBzIFN}fAK""#`p_TqtD*1E37XGVs@0nmSe+Y6Qyo-aUu%i8c=H2vJ\) R:MLb.9,wlO~P",
@"2ThtjEM+!=xXb)7,ZV{*ci3""8@_l-HS69L>]\AUF/Q%:qD?1~m(yvO0e'<#o$p4dnIzKP|`NrkaGg.ufCRB[; sJYwW}5&",
@"vB\5/zl-9y:Pj|=(R'7QJI *&CTX""p0]_3.idcuOefVU#omwNZ`$Fs?L+1Sk<,b)hM4A6[Y%aDrg@~KqEW8t>H};n!2xG{",
@"sFz0Bo@_HfnK>LR}qWXV+D6`Y28=4Cm~G/7-5A\b9!a#rP.l&M$hc3ijQk;),TvUd<[:I""u1'NZSOw]*gxtE{eJp|y (?%",
@"M@,D}|LJyGO8`$*ZqH .j>c~hanG",
@"xVa1']_GU#zm+:5b@06O3Ap8=*7ZFY!H-uEQk; .q)i&rhd",
@"I]Jz7AG@QX.""%3Lq>METUo{Pp_ |a6<0dYVSv8:b)~W9NK`(r'4fs&wim\kReC2hg=HOj$1B*/nxt,;c#y+![?lFuZ-5D}",
@"Rr(Ge6F Hx>q$m&C%M~Tn,:""o'tX/*yP.{lZ!YkiVhuw_y|m};d)-7DZ""Fe/Y9 WidFN,1KsmwQ)GJM{I4:C%}#Ep(?HB/r;t.&U8o|l['Lg""2hRDyZ5`nbf]qjc0!zS-TkYO<_=76a\X@$Pe3+xVvu",
@"yYgjf""5VdHc#uA,W1i+v'6|@pr{n;DJ!8(btPGaQM.LT3oe?NB/&9>Z`-}02*%x<7lsqz4OS ~E$\R]KI[:UwC_=h)kXmF",
@"5:iar.{YU7mBZR@-K|2 ""+~`M%8sq4JhPo<_X\Sg3WC;Tuxz,fvEQ1p9=w}FAI&j/keD0c?)LN6OHV]lGy'$*>nd[(tb!#",
};
}
}
///
/// Set a site specific key
///
/// The required key for BMX Ade
public void SetKey(String[] aKey)
{
this.Key = aKey;
}
private string[] _key = null;
private string[] Key
{
get {return (_key == null) ? this.DefaultKey : this._key; }
set { _key = value; }
}
///
/// BMXAde Implementation
///
/// String to encrypt
/// Encrypt string
public string Encrypt(string aString)
{
return this.ADEEncryp(aString);
}
///
/// BMXAde Implementation
///
/// String to decrypt
/// Decrypted string
public string Decrypt(string aString)
{
return this.ADEDecryp(aString);
}
private string ADEEncryp(string sInput)
{
//Encrypt a string
string strPercent;
string strAssoc;
string strIdix;
int nPercent;
int nAssocix;
int nIdix;
System.Random rRand = new Random(DateTime.Now.Second);
nPercent = rRand.Next(0, 10000);
nPercent += 72439;
nAssocix = nPercent % 20;
nAssocix++;
this.Assert(nAssocix < 21, "Associx to large");
strPercent = nPercent.ToString();
strPercent = strPercent.Substring(1, 2);
nIdix = Convert.ToInt32(strPercent);
nIdix = nIdix % 20;
nIdix++;
this.Assert(nIdix < 21, "Idix to large");
const int nEncryptBase = 101;
strAssoc = LoadKey(nEncryptBase + nAssocix);
this.Assert(strAssoc.Length == 94, "Associator length invalid");
strIdix = LoadKey(nEncryptBase + nIdix);
this.Assert(strAssoc.Length == 94, "Identifier length invalid");
string sEncrypted = "";
foreach (char c in sInput)
{
string d = c.ToString();
int nFindChar = strIdix.IndexOf(c);
if (nFindChar > -1)
{
d = strAssoc.Substring(nFindChar, 1);
}
sEncrypted += d;
}
String debugPreview = (char)(nIdix + 31) + sEncrypted + (char)(nAssocix + 31);
return debugPreview;
}
private string LoadKey(int nID)
{
nID -= 102;
this.Assert(nID < 20, "Key array length too long");
return this.Key[nID];
}
private void Assert(bool aBoolean, String aMessage)
{
if (!aBoolean)
{
throw new BMXNetException("BmxAdeEncryptionProvider Assertion failed: " + aMessage);
}
}
private string ADEDecryp(string sInput)
{
//Encrypt a string
string strAssoc;
string strIdix;
int nAssocix;
int nIdix;
this.Assert(sInput != "", "Unable to encrypt empty string");
//get associator string index
char cAssocix = sInput[sInput.Length - 1];
nAssocix = (int)cAssocix;
nAssocix -= 31;
this.Assert(nAssocix < 21, "Associx to large");
//get identifier string index
char cIdix = sInput[0];
nIdix = (int)cIdix;
nIdix -= 31;
this.Assert(nIdix < 21, "Idix to large");
//get associator string
const int nEncryptBase = 101;
strAssoc = LoadKey(nEncryptBase + nAssocix);
this.Assert(strAssoc.Length == 94, "Associator length invalid");
//get identifier string
strIdix = LoadKey(nEncryptBase + nIdix);
this.Assert(strAssoc.Length == 94, "Identifier length invalid");
//translated result
string sDecrypted = "";
sInput = sInput.Substring(1, sInput.Length - 2);
foreach (char c in sInput)
{
string d = c.ToString();
int nFindChar = strAssoc.IndexOf(c);
if (nFindChar > -1)
{
d = strIdix.Substring(nFindChar, 1);
}
sDecrypted += d;
}
return sDecrypted;
}
}
}