3DES加密,以及避过.net弱密钥检测。代码根据网上代码进行的修改
private static byte[] ThreeDES(byte[] key, byte[] str)
{
TripleDESCryptoServiceProvider tdsc = new TripleDESCryptoServiceProvider();
tdsc.Padding = PaddingMode.None;
byte[] IV = { 0xB0, 0xA2, 0xB8, 0xA3, 0xDA, 0xCC, 0xDA, 0xCC };
//指定密匙长度,默认为192位
tdsc.KeySize = 128;
//使用指定的key和IV(加密向量)
Type t = Type.GetType("System.Security.Cryptography.CryptoAPITransformMode");
object obj = t.GetField("Encrypt", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly).GetValue(t);
MethodInfo mi = tdsc.GetType().GetMethod("_NewEncryptor", BindingFlags.Instance | BindingFlags.NonPublic);
ICryptoTransform desCrypt = (ICryptoTransform)mi.Invoke(tdsc, new object[] { key, CipherMode.ECB, null, 0, obj });
tdsc.IV = IV;
//加密模式,偏移
tdsc.Mode = CipherMode.ECB;
//进行加密转换运算
//ICryptoTransform ct = tdsc.CreateDecryptor();
//8很关键,加密结果是8字节数组
byte[] results = desCrypt.TransformFinalBlock(str, 0, 8);
return results;
}