private static byte[] calc_resp(byte[] nonce, byte[] data) { /* * takes a 21 byte array and treats it as 3 56-bit DES keys. The * 8 byte nonce is encrypted with each key and the resulting 24 * bytes are stored in the results array. */ byte[] response = new byte[24]; System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create(); des.Mode = System.Security.Cryptography.CipherMode.ECB; des.Key = setup_des_key(data, 0); System.Security.Cryptography.ICryptoTransform ct = des.CreateEncryptor(); ct.TransformBlock(nonce, 0, 8, response, 0); des.Key = setup_des_key(data, 7); ct = des.CreateEncryptor(); ct.TransformBlock(nonce, 0, 8, response, 8); des.Key = setup_des_key(data, 14); ct = des.CreateEncryptor(); ct.TransformBlock(nonce, 0, 8, response, 16); string s = ByteArrayToString(response); System.Console.WriteLine(s); return(response); }
private static void Initialize() { alg = System.Security.Cryptography.DES.Create(); alg.Mode = System.Security.Cryptography.CipherMode.ECB; byte[] key = new byte[8]; Util.Random.NextBytes(key); alg.Key = key; alg.IV = new byte[8]; encryptor = alg.CreateEncryptor(); _initialized = true; }
private static string DoEncrypt(string source) { string result; byte[] iV = new Byte[8]; Array.Copy(IV, iV, 8); System.Security.Cryptography.DES des = System.Security.Cryptography.DESCryptoServiceProvider.Create(); byte[] encodeStr = System.Text.Encoding.Default.GetBytes(source); System.Security.Cryptography.ICryptoTransform cryptor = des.CreateEncryptor(Key, iV); byte[] encode = cryptor.TransformFinalBlock(encodeStr, 0, encodeStr.Length); result = System.Convert.ToBase64String(encode); return(result); }
private static string DoDecrypt(string source) { string result; byte[] iV = new Byte[8]; Array.Copy(IV, iV, 8); System.Security.Cryptography.DES des = System.Security.Cryptography.DESCryptoServiceProvider.Create(); System.Security.Cryptography.ICryptoTransform decryptor = des.CreateDecryptor(Key, iV); byte[] decodeSrc = System.Convert.FromBase64String(source); byte[] decode = decryptor.TransformFinalBlock(decodeSrc, 0, decodeSrc.Length); result = System.Text.Encoding.Default.GetString(decode, 0, decode.Length); return(result); }
/// <summary> /// Calculates NTLM NT response. /// </summary> /// <param name="nonce">Server nonce.</param> /// <param name="password">Password.</param> /// <returns>Returns NTLM NT response.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>nonce</b> or <b>password</b> is null reference.</exception> public static byte[] CalculateLM(byte[] nonce, string password) { if (nonce == null) { throw new ArgumentNullException("nonce"); } if (password == null) { throw new ArgumentNullException("password"); } byte[] lmBuffer = new byte[21]; byte[] magic = { 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 }; byte[] nullEncMagic = { 0xAA, 0xD3, 0xB4, 0x35, 0xB5, 0x14, 0x04, 0xEE }; // create Lan Manager password System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create(); des.Mode = System.Security.Cryptography.CipherMode.ECB; // Note: In .NET DES cannot accept a weak key // this can happen for a null password if (password.Length < 1) { Buffer.BlockCopy(nullEncMagic, 0, lmBuffer, 0, 8); } else { des.Key = PasswordToKey(password, 0); des.CreateEncryptor().TransformBlock(magic, 0, 8, lmBuffer, 0); } // and if a password has less than 8 characters if (password.Length < 8) { Buffer.BlockCopy(nullEncMagic, 0, lmBuffer, 8, 8); } else { des.Key = PasswordToKey(password, 7); des.CreateEncryptor().TransformBlock(magic, 0, 8, lmBuffer, 8); } string s = ByteArrayToString(lmBuffer); System.Console.WriteLine(s); return(calc_resp(nonce, lmBuffer)); }
public static bool _Create_System_String( ) { //Parameters System.String algName = null; //ReturnType/Value System.Security.Cryptography.DES returnVal_Real = null; System.Security.Cryptography.DES returnVal_Intercepted = null; //Exception Exception exception_Real = null; Exception exception_Intercepted = null; InterceptionMaintenance.disableInterception( ); try { returnValue_Real = System.Security.Cryptography.DES.Create(algName); } catch (Exception e) { exception_Real = e; } InterceptionMaintenance.enableInterception( ); try { returnValue_Intercepted = System.Security.Cryptography.DES.Create(algName); } catch (Exception e) { exception_Intercepted = e; } Return((exception_Real.Messsage == exception_Intercepted.Message) && (returnValue_Real == returnValue_Intercepted)); }
internal static string ToCrypt(string chave, string mensagem) { System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create(); des.Mode = System.Security.Cryptography.CipherMode.CBC; des.KeySize = 64; byte[] chaveBytes; byte[] criptografiaBytes; byte[] mensagemBytes; string criptografia; chaveBytes = Encoding.UTF8.GetBytes(chave); mensagemBytes = Encoding.UTF8.GetBytes(mensagem); System.Security.Cryptography.ICryptoTransform cryptor = des.CreateEncryptor(chaveBytes, chaveBytes); criptografiaBytes = cryptor.TransformFinalBlock(mensagemBytes, 0, mensagemBytes.Length); cryptor.Dispose(); criptografia = Convert.ToBase64String(criptografiaBytes); return(criptografia); }
private string RunEsOrDs(string ValueString, string Key, bool EsOrDs) { string RET = string.Empty; try { if (!string.IsNullOrEmpty(ValueString) && !string.IsNullOrEmpty(Key)) { Key = Key + Key.Length; string k = string.Empty; using (System.Security.Cryptography.MD5 md = System.Security.Cryptography.MD5.Create()) { k = BitConverter.ToString(md.ComputeHash(Encoding.UTF8.GetBytes(Key))).Replace("-", string.Empty); } byte[] inputByteArray = EsOrDs ? System.Text.Encoding.UTF8.GetBytes(ValueString) : System.Convert.FromBase64String(ValueString); byte[] rgbKey = System.Text.Encoding.UTF8.GetBytes(k.Substring(0, 8)); byte[] rgbIV = System.Text.Encoding.UTF8.GetBytes(k.Substring(k.Length - 8, 8)); using (System.Security.Cryptography.DES DCSP = System.Security.Cryptography.DES.Create()) { using (System.IO.MemoryStream mStream = new System.IO.MemoryStream()) { using (System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, EsOrDs ? DCSP.CreateEncryptor(rgbKey, rgbIV) : DCSP.CreateDecryptor(rgbKey, rgbIV), System.Security.Cryptography.CryptoStreamMode.Write)) { cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); RET = EsOrDs ? System.Convert.ToBase64String(mStream.ToArray()) : System.Text.Encoding.UTF8.GetString(mStream.ToArray()); } } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } return(RET); }
private string Decrypt(string Text) { if (string.IsNullOrEmpty(Text.Trim())) { return(string.Empty); } using (System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create()) { try { string sKey = "beijingjarlinfo"; int len = Text.Trim().Length / 2; byte[] inputByteArray = new byte[len]; for (int x = 0; x < len; x++) { inputByteArray[x] = Convert.ToByte(Convert.ToInt32(Text.Substring(x * 2, 2), 16)); } des.Key = Encoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = Encoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); using (MemoryStream ms = new MemoryStream()) { using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); } return(Encoding.Default.GetString(ms.ToArray())); } } catch (Exception ex) { MessageBox.Show(ex.Message); return(string.Empty); } } }