triple des
jeg har flg. kodepublic static string Bin2Hex(byte[] bin)
{
StringBuilder sb = new StringBuilder(bin.Length * 2);
foreach(byte b in bin) {
sb.Append(b.ToString("x").PadLeft(2, '0'));
}
return sb.ToString();
}
public static string tdes_enc(string strToEncrypt)
{
byte[] keyz = System.Text.Encoding.ASCII.GetBytes(key);
byte[] ivz = System.Text.Encoding.ASCII.GetBytes(iv);
byte[] data = System.Text.Encoding.ASCII.GetBytes(strToEncrypt);
byte[] enc = new byte[0];
System.Security.Cryptography.TripleDES tdes = System.Security.Cryptography.TripleDES.Create();
tdes.IV = ivz;
tdes.Key = keyz;
tdes.Mode = System.Security.Cryptography.CipherMode.CBC;
tdes.Padding = System.Security.Cryptography.PaddingMode.Zeros;
System.Security.Cryptography.ICryptoTransform ict = tdes.CreateEncryptor();
enc = ict.TransformFinalBlock(data, 0, data.Length);
return Bin2Hex(enc);
}
men kan ikke få den til at dekrypter har prvøet med
public static byte[] Hex2Bin(string hex)
{
string txt = "";
uint hexhold;
int ytel=0;
byte[] conv=new byte[(hex.Length/2)+1];
for (int xtel = 0; xtel < hex.Length; xtel = xtel + 2)
{
txt = hex.Substring(xtel, 2);
hexhold = System.Convert.ToUInt32(txt, 16);
conv[ytel] = System.Convert.ToByte(hexhold);
ytel++;
}
return conv;
}
public static string tdes_dec(string strToDecrypt)
{
byte[] keyz = System.Text.Encoding.ASCII.GetBytes(key);
byte[] ivz = System.Text.Encoding.ASCII.GetBytes(iv);
byte[] data = Hex2Bin(strToDecrypt);
byte[] enc = new byte[0];
//string rstr;
System.Security.Cryptography.TripleDES tdes = System.Security.Cryptography.TripleDES.Create();
tdes.IV = ivz;
tdes.Key = keyz;
tdes.Mode = System.Security.Cryptography.CipherMode.CBC;
tdes.Padding = System.Security.Cryptography.PaddingMode.Zeros;
System.Security.Cryptography.ICryptoTransform ict = tdes.CreateDecryptor();
//enc = ict.TransformFinalBlock(data, 0, data.Length);
return System.Text.Encoding.ASCII.GetString(enc);
}
nogen der ved hvorfor?
