سلام.
کدای این پستی که میخوام بذارم یه مقدار وقت گیرن و اگه اجازه بدین در چند نوبت میذارم.
ممنون از شما.
این تصویر برنامه ایه که باید نوشته بشه:
و این مطلب پایین توضیح تصویر بالاست:
Input ---> output
------ Form1 --------
streamWrite, streamReader
------ Form2 --------
user ali ---> 300 user is ok (give pass now) , 400 user is not found
pass 123 ---> 200 authenticated , 401 pass incorrect
isauth ---> 202 yes, 203 no
close ---> 201 connection closed
public static string EncryptAES(string plainText, string strKey, string strIV)
{
//byte[] Key = Encoding.ASCII.GetBytes(strKey);
//byte[] IV = Encoding.ASCII.GetBytes(strIV);
byte[] Key = Convert.FromBase64String(strKey);
byte[] IV = Convert.FromBase64String(strIV);
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
// Declare the streams used
// to encrypt to an in memory
// array of bytes.
MemoryStream msEncrypt = null;
CryptoStream csEncrypt = null;
StreamWriter swEncrypt = null;
// Declare the RijndaelManaged object
// used to encrypt the data.
RijndaelManaged aesAlg = null;
// Declare the bytes used to hold the
// encrypted data.
byte[] encrypted = null;
try
{
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decrytor to perform the stream transform.
//ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
msEncrypt = new MemoryStream();
csEncrypt = new CryptoStream(msEncrypt, aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV), CryptoStreamMode.Write);
swEncrypt = new StreamWriter(csEncrypt);
//csEncrypt.Write(byte[] ...)
//Write all data to the stream.
swEncrypt.Write(plainText);
}
finally
{
// Clean things up.
// Close the streams.
if (swEncrypt != null)
swEncrypt.Close();
if (csEncrypt != null)
csEncrypt.Close();
if (msEncrypt != null)
msEncrypt.Close();
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}
// Return the encrypted bytes from the memory stream.
return Convert.ToBase64String(msEncrypt.ToArray());
}
public static string DecryptAES(string str, string strKey, string strIV)
{
//byte[] Key = Encoding.ASCII.GetBytes(strKey);
//byte[] IV = Encoding.ASCII.GetBytes(strIV);
byte[] Key = Convert.FromBase64String(strKey);
byte[] IV = Convert.FromBase64String(strIV);
byte[] cipherText = Convert.FromBase64String(str);
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
// TDeclare the streams used
// to decrypt to an in memory
// array of bytes.
MemoryStream msDecrypt = null;
CryptoStream csDecrypt = null;
StreamReader srDecrypt = null;
// Declare the RijndaelManaged object
// used to decrypt the data.
RijndaelManaged aesAlg = null;
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
try
{
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
msDecrypt = new MemoryStream(cipherText);
csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
srDecrypt = new StreamReader(csDecrypt);
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
finally
{
// Clean things up.
// Close the streams.
if (srDecrypt != null)
srDecrypt.Close();
if (csDecrypt != null)
csDecrypt.Close();
if (msDecrypt != null)
msDecrypt.Close();
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}
return plaintext;
}
public static string produceKey(int keysize, string hashAlgorithm, int passwordIterations)
{
byte[] saltValueBytes = new byte[keysize];
Random r=new Random();
r.NextBytes(saltValueBytes);
string strPhrase = r.NextDouble().ToString();
PasswordDeriveBytes password = new PasswordDeriveBytes(
strPhrase,
saltValueBytes,
hashAlgorithm,
passwordIterations);
byte[] keyBytes = password.GetBytes(keysize);
string str = Convert.ToBase64String(keyBytes);
return str; //.Substring(0, 16);
}
}
و فرمهای مربوطه:
فرم اول:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;
namespace winYahooServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;
namespace winYahooServer
{
public partial class Form2 : Form
{
Dictionary<string, string> users;
switch (strcommand.ToLower())
{
case "user":
//hacker:
//if (isAuthenticated)
//{
// strResponse = "497 this connection is assigned to another user, first sign out :P";
//}
if (users.ContainsKey(strarg))
{
//hackers: isAuthenticated = false;
strResponse = "300 user is ok (give pass now)\r\n";
strUser = strarg;
}
else
{
strResponse = "400 user not found\r\n";
}
break;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;
namespace winYahooServer
{
public partial class Form3 : Form
{
Dictionary<string, string> users;
string strOnlineUsers = "";
switch (strcommand.ToLower())
{
case "user":
if (users.ContainsKey(strarg))
{
strResponse = "300 user is ok (give pass now)";
strUser = strarg;
}
else
{
strResponse = "400 user not found";
}
break;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;
namespace winYahooServer
{
public partial class Form4 : Form
{
Dictionary<string, string> users;
string strOnlineUsers = "";
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
users = new Dictionary<string, string>();
users["ali"] = "123";
users["hasan"] = "123";
users["akbar"] = "123";
//-----------uncomment----------------
Thread t = new Thread(new ThreadStart(Server));
t.IsBackground = true; // to avoid environment.exit
t.Start();
}
TcpListener listener;
Socket s;
private void Server()
{
IPAddress localip = IPAddress.Parse("127.0.0.1");
listener = new TcpListener(localip, 8080);
listener.Start();
while (true)
{
s = listener.AcceptSocket();
//-----------uncomment----------------
Thread t = new Thread(new ThreadStart(ListenIt));
t.IsBackground = true;
t.Start();
}
}
private void ListenIt()
{
Socket sok = s;
NetworkStream ns = new NetworkStream(sok);
StreamWriter writer = new StreamWriter(ns);
StreamReader reader = new StreamReader(ns);
switch (strcommand.ToLower())
{
case "user":
if (users.ContainsKey(strarg))
{
strResponse = "300 user is ok (give pass now)";
strUser = strarg;
}
else
{
strResponse = "400 user not found";
}
break;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;
namespace winYahooServer
{
public partial class Form5 : Form
{
Dictionary<string, string> users;
string strOnlineUsers = "";
//---------------------added----------------
Dictionary<string, StreamWriter> userWriter = new Dictionary<string, StreamWriter>();
//DataTable dtMsg = new DataTable();
//private void InitDtMsg()
//{
// dtMsg.Columns.Add("From", typeof(string));
// dtMsg.Columns.Add("To", typeof(string));
// dtMsg.Columns.Add("msg", typeof(string));
// dtMsg.Columns.Add("isSent", typeof(bool));
//}
//-----------------
public Form5()
{
//System.Diagnostics.Debugger.Launch();
InitializeComponent();
}
switch (strcommand.ToLower())
{
case "user":
if (users.ContainsKey(strarg))
{
strResponse = "300 user is ok (give pass now)";
strUser = strarg;
}
else
{
strResponse = "400 user not found";
}
break;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;
namespace winYahooServer
{
public partial class Form6 : Form
{
Dictionary<string, string> users;
string strOnlineUsers = "";
Dictionary<string, StreamWriter> userWriter = new Dictionary<string, StreamWriter>();
public Form6()
{
//System.Diagnostics.Debugger.Launch();
InitializeComponent();
}
switch (strcommand.ToLower())
{
case "user":
if (users.ContainsKey(strarg))
{
strResponse = "300 user is ok (give pass now)";
strUser = strarg;
}
else
{
strResponse = "400 user not found";
}
break;
case "onusers":
if (isAuthenticated)
strResponse = "260 " + strOnlineUsers;
else
strResponse = "402 unauthenticated";
break;
case "tell":
if (isAuthenticated)
if (strOnlineUsers.IndexOf(strarg + " ") >= 0)
{
strResponse = "240 told";
string strmsg = "";
for (int i = 3; i < strInputSegment.Length - 1; i++)
{
strmsg += strInputSegment[i] + " ";
}
userWriter[strarg].WriteLine("250 " + strUser + " \" " + strmsg + " \"");
userWriter[strarg].Flush();
}
else
strResponse = "403 " + strarg + " has gone";
else
strResponse = "402 unauthenticated";
break;
//---------------added-------------
case "adduser":
try
{
if (isAuthenticated)
strResponse = "405 you should log out first";
else if (users.ContainsKey(strInputSegment[1]))
strResponse = "404 user exsited";
else
{
users.Add(strInputSegment[1], strInputSegment[2]);
strResponse = "204 added";
}
}
catch (Exception)
{
strResponse = "502 wrong arguments";
}
break;
//----------------------------
default:
strResponse = "503 UnKnown Command";
break;
}
writer.Write(strResponse + "\r\n");
writer.Flush();
}
ns.Close();
sok.Close();
}
}
}
فرم هفتم:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;
namespace winYahooServer
{
public partial class Form7 : Form
{
Dictionary<string, string> users;
string strOnlineUsers = "";
Dictionary<string, StreamWriter> userWriter = new Dictionary<string, StreamWriter>();
public Form7()
{
//System.Diagnostics.Debugger.Launch();
InitializeComponent();
}
switch (strcommand.ToLower())
{
case "user":
if (users.ContainsKey(strarg))
{
strResponse = "300 user is ok (give pass now)";
strUser = strarg;
}
else
{
strResponse = "400 user not found";
}
break;
case "onusers":
if (isAuthenticated)
strResponse = "260 " + strOnlineUsers;
else
strResponse = "402 unauthenticated";
break;
case "tell":
if (isAuthenticated)
if (strOnlineUsers.IndexOf(strarg + " ") >= 0)
{
strResponse = "240 told";
string strmsg = "";
for (int i = 3; i < strInputSegment.Length - 1; i++)
{
strmsg += strInputSegment[i] + " ";
}
userWriter[strarg].WriteLine("250 " + strUser + " \" " + strmsg + " \"");
userWriter[strarg].Flush();
}
else
strResponse = "403 " + strarg + " has gone";
else
strResponse = "402 unauthenticated";
break;
//---------------updated-------------
case "adduser":
try
{
if (isAuthenticated)
strResponse = "405 you should log out first";
else if (users.ContainsKey(strInputSegment[1]))
strResponse = "404 user exsited";
else
{
users.Add(strInputSegment[1], clsSecurity.ComputeMD5(strInputSegment[2]));
strResponse = "204 added";
}
}
catch (Exception)
{
strResponse = "502 wrong arguments";
}
break;
//----------------------------
default:
strResponse = "503 UnKnown Command";
break;
}
writer.Write(strResponse + "\r\n");
writer.Flush();
}
ns.Close();
sok.Close();
}
}
}
فرم هشتم:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;
namespace winYahooServer
{
public partial class Form8 : Form
{
Dictionary<string, string> users;
string strOnlineUsers = "";
Dictionary<string, StreamWriter> userWriter = new Dictionary<string, StreamWriter>();
//-------------------added--------------
Dictionary<string, string> userKey = new Dictionary<string, string>();
Dictionary<string, string> userIV = new Dictionary<string, string>();
//--------------------------------------
public Form8()
{
//System.Diagnostics.Debugger.Launch();
InitializeComponent();
}
switch (strcommand.ToLower())
{
case "user":
if (users.ContainsKey(strarg))
{
strResponse = "300 user is ok (give pass now)";
strUser = strarg;
}
else
{
strResponse = "400 user not found";
}
break;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;
namespace winYahooServer
{
public partial class Form9 : Form
{
Dictionary<string, string> users;
string strOnlineUsers = "";
Dictionary<string, StreamWriter> userWriter = new Dictionary<string, StreamWriter>();
Dictionary<string, string> userKey = new Dictionary<string, string>();
Dictionary<string, string> userIV = new Dictionary<string, string>();
//----------------------added------------
string publickey, publicprivatekey;
//------------------------------------
public Form9()
{
//System.Diagnostics.Debugger.Launch();
InitializeComponent();
}
//----------------------added------------
//در واقع اینها رو از کومودو می گیریم
clsSecurity.getRSAkeys(out publickey, out publicprivatekey);
//-------------------------------------
Thread t = new Thread(new ThreadStart(Server));
t.IsBackground = true;
t.Start();
}
TcpListener listener;
Socket s;
private void Server()
{
IPAddress localip = IPAddress.Parse("127.0.0.1");
listener = new TcpListener(localip, 8080);
listener.Start();
while (true)
{
s = listener.AcceptSocket();
Thread t = new Thread(new ThreadStart(ListenIt));
t.IsBackground = true;
t.Start();
}
}
private void ListenIt()
{
Socket sok = s;
NetworkStream ns = new NetworkStream(sok);
StreamWriter writer = new StreamWriter(ns);
StreamReader reader = new StreamReader(ns);
switch (strcommand.ToLower())
{
case "user":
if (users.ContainsKey(strarg))
{
strResponse = "300 user is ok (give pass now)";
strUser = strarg;
}
else
{
strResponse = "400 user not found";
}
break;
ویرایش توسط Hossein : 4th July 2012 در ساعت 02:04 PMدلیل: لطفا توضیحات یادتون نره - زحمتی که میکشید رو با توضیحاتتون کامل کنید - مثلا اینجا کاربر که میاد نمیدونه چرا این
public static string EncryptAES(string plainText, string strKey, string strIV)
{
//byte[] Key = Encoding.ASCII.GetBytes(strKey);
//byte[] IV = Encoding.ASCII.GetBytes(strIV);
byte[] Key = Convert.FromBase64String(strKey);
byte[] IV = Convert.FromBase64String(strIV);
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
// Declare the streams used
// to encrypt to an in memory
// array of bytes.
MemoryStream msEncrypt = null;
CryptoStream csEncrypt = null;
StreamWriter swEncrypt = null;
// Declare the RijndaelManaged object
// used to encrypt the data.
RijndaelManaged aesAlg = null;
// Declare the bytes used to hold the
// encrypted data.
byte[] encrypted = null;
try
{
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
msEncrypt = new MemoryStream();
csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
swEncrypt = new StreamWriter(csEncrypt);
//csEncrypt.Write(byte[] ...)
//Write all data to the stream.
swEncrypt.Write(plainText);
}
finally
{
// Clean things up.
// Close the streams.
if (swEncrypt != null)
swEncrypt.Close();
if (csEncrypt != null)
csEncrypt.Close();
if (msEncrypt != null)
msEncrypt.Close();
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}
// Return the encrypted bytes from the memory stream.
return Convert.ToBase64String(msEncrypt.ToArray());
}
public static string DecryptAES(string str, string strKey, string strIV)
{
//byte[] Key = Encoding.ASCII.GetBytes(strKey);
//byte[] IV = Encoding.ASCII.GetBytes(strIV);
byte[] Key = Convert.FromBase64String(strKey);
byte[] IV = Convert.FromBase64String(strIV);
byte[] cipherText = Convert.FromBase64String(str);
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
// TDeclare the streams used
// to decrypt to an in memory
// array of bytes.
MemoryStream msDecrypt = null;
CryptoStream csDecrypt = null;
StreamReader srDecrypt = null;
// Declare the RijndaelManaged object
// used to decrypt the data.
RijndaelManaged aesAlg = null;
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
try
{
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
msDecrypt = new MemoryStream(cipherText);
csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
srDecrypt = new StreamReader(csDecrypt);
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
finally
{
// Clean things up.
// Close the streams.
if (srDecrypt != null)
srDecrypt.Close();
if (csDecrypt != null)
csDecrypt.Close();
if (msDecrypt != null)
msDecrypt.Close();
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}
return plaintext;
}
public static string produceKey(string strPhrase, int keysize, string hashAlgorithm, int passwordIterations)
{
byte[] saltValueBytes = new byte[keysize];
Random r=new Random();
r.NextBytes(saltValueBytes);
PasswordDeriveBytes password = new PasswordDeriveBytes(
strPhrase,
saltValueBytes,
hashAlgorithm,
passwordIterations);
byte[] keyBytes = password.GetBytes(16);
string str = Convert.ToBase64String(keyBytes);
return str;//str.Substring(0, 16);
}
public static string produceKey(int keysize, string hashAlgorithm, int passwordIterations)
{
byte[] saltValueBytes = new byte[keysize];
Random r = new Random();
r.NextBytes(saltValueBytes);
string strPhrase = r.NextDouble().ToString();
PasswordDeriveBytes password = new PasswordDeriveBytes(
strPhrase,
saltValueBytes,
hashAlgorithm,
passwordIterations);
byte[] keyBytes = password.GetBytes(keysize);
string str = Convert.ToBase64String(keyBytes);
return str; //.Substring(0, 16);
}
}
فرم اول:
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace winYahooClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TcpClient tcp;
NetworkStream ns;
StreamWriter writer;
StreamReader reader;
while (true)
{
string strCode, strStatus;
ReadResult(out strCode, out strStatus);
switch (strCode)
{
case "250": //sth to show to user
int idx = strStatus.IndexOf(' ');
string sfreind = strStatus.Substring(0, idx);
string msg = strStatus.Substring(idx + 1).Replace("\"", "").Trim();
string message = "\r\n" + sfreind + " (" + DateTime.Now.ToLongTimeString() + ")>> " + msg;
//textBox1.Text += message;
//SetTxtText(message);
this.Invoke(dlgSetTxtText, new object[] { message });
break;
case "260": //onusers
/*
cboUsers.Items.Clear();
string[] users = strStatus.Split(' ');
foreach (string str in users)
{
cboUsers.Items.Add(str);
}
*/
this.Invoke(dlgSetCboUsers, new object[] { strStatus });
break;
case "201": //connection close
this.Invoke(dlgSetSignOutInThreading);
break;
case "240": //msg is told
case "403": //user is gone
case "402": //user is not authenticated to see onusers
this.Invoke(dlgSetToolStripStatusLabel1, new object[] { strStatus });
break;
default:
string strmsg = "unknown code error (" + strCode + ") came from server";
this.Invoke(dlgSetToolStripStatusLabel1, new object[] { strmsg });
break;
}
}
}
# region DoSomething
public delegate void DoSomething();
public DoSomething dlgSetSignOutInThreading;
private void SetForSignout()
{
lblusername.Text = "";
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace winYahooClient
{
public partial class SecureForm2 : Form
{
public SecureForm2()
{
InitializeComponent();
}
TcpClient tcp;
NetworkStream ns;
StreamWriter writer;
StreamReader reader;
Thread t;
//---------------------------added-----------------------------
string strKey="", strIV="";
int isSecure = 0;
//--------------------------------------------------------
private void button1_Click(object sender, EventArgs e)
{
try
{
//user ali
string strCode, strStatus;
WriteCommand("user " + txtUsername.Text.Trim());
ReadResult(out strCode, out strStatus);
if (strCode != "300")
{
MessageBox.Show("Wrong username");
return;
}
//pass 123
WriteCommand("pass " + txtPassword.Text.Trim());
ReadResult(out strCode, out strStatus);
if (strCode != "200")
{
MessageBox.Show("Wrong password");
return;
}
while (true)
{
string strCode, strStatus;
ReadResult(out strCode, out strStatus);
switch (strCode)
{
case "250": //sth to show to user
int idx = strStatus.IndexOf(' ');
string sfreind = strStatus.Substring(0, idx);
string msg = strStatus.Substring(idx + 1).Replace("\"", "").Trim();
string message = "\r\n" + sfreind + " (" + DateTime.Now.ToLongTimeString() + ")>> " + msg;
//textBox1.Text += message;
//SetTxtText(message);
this.Invoke(dlgSetTxtText, new object[] { message });
break;
case "260": //onusers
/*
cboUsers.Items.Clear();
string[] users = strStatus.Split(' ');
foreach (string str in users)
{
cboUsers.Items.Add(str);
}
*/
this.Invoke(dlgSetCboUsers, new object[] { strStatus });
break;
case "201": //connection close
this.Invoke(dlgSetSignOutInThreading);
Thread.CurrentThread.Abort();
break;
case "240": //msg is told
case "403": //user is gone
case "402": //user is not authenticated to see onusers
this.Invoke(dlgSetToolStripStatusLabel1, new object[] { strStatus });
break;
default:
string strmsg = "unknown code error (" + strCode + ") came from server";
this.Invoke(dlgSetToolStripStatusLabel1, new object[] { strmsg });
break;
}
}
}
# region DoSomething
public delegate void DoSomething();
public DoSomething dlgSetSignOutInThreading;
private void SetForSignout()
{
lblusername.Text = "";
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace winYahooClient
{
public partial class SecureForm3 : Form
{
public SecureForm3()
{
InitializeComponent();
}
TcpClient tcp;
NetworkStream ns;
StreamWriter writer;
StreamReader reader;
Thread t;
string strKey = "", strIV = "";
int isSecure = 0;
while (true)
{
string strCode, strStatus;
ReadResult(out strCode, out strStatus);
switch (strCode)
{
case "250": //sth to show to user
int idx = strStatus.IndexOf(' ');
string sfreind = strStatus.Substring(0, idx);
string msg = strStatus.Substring(idx + 1).Replace("\"", "").Trim();
string message = "\r\n" + sfreind + " (" + DateTime.Now.ToLongTimeString() + ")>> " + msg;
//textBox1.Text += message;
//SetTxtText(message);
this.Invoke(dlgSetTxtText, new object[] { message });
break;
case "260": //onusers
/*
cboUsers.Items.Clear();
string[] users = strStatus.Split(' ');
foreach (string str in users)
{
cboUsers.Items.Add(str);
}
*/
this.Invoke(dlgSetCboUsers, new object[] { strStatus });
break;
case "201": //connection close
this.Invoke(dlgSetSignOutInThreading);
Thread.CurrentThread.Abort();
break;
case "240": //msg is told
case "403": //user is gone
case "402": //user is not authenticated to see onusers
this.Invoke(dlgSetToolStripStatusLabel1, new object[] { strStatus });
break;
default:
string strmsg = "unknown code error (" + strCode + ") came from server";
this.Invoke(dlgSetToolStripStatusLabel1, new object[] { strmsg });
break;
}
}
}
# region DoSomething
public delegate void DoSomething();
public DoSomething dlgSetSignOutInThreading;
private void SetForSignout()
{
lblusername.Text = "";
من توضیح توابعی که آشنا هستم تا اندازه ای که می دونم میذارم تا ایشاله کار بقیه دوستان سبک تر بشه. کلاس clssecurity برای تامین اهداف امنیتی در نظر گرفته شده. مثل سری موندن اطلاعات، احراز هویت کاربران، غیر قابل انکار بودن پیام ها و نظارت بر صحت اطلاعات. این کار با رمز نگاری انجام میشه و در کنار اون از درهمسازی یا هشینگ استفاده میشه. دو نوع الگوریتم رمز نگاری وجود داره شامل متقارن و نامتقارن. در رمزنگاری متقارن تنها از یک کلید یعنی کلید خصوصی که تنها بین فرستنده و گیرنده رد و بدل میشه استفاده میشه. و در رمزنگاری نامتقارن هر فرد دو کلید عمومی و خصوصی داره که کلید عمومی همه افراد در اختیار دیگران قرار داره اما کلید خصوصی مختص خود فرد هست و دیگران از اون آگاهی ندارن.در رمز نگاری نا متقارن امکان استفاده از امضای دیجیتال هم وجود داره.به عنوان مثال رد و بدل کردن اطلاعات بین دو فرد "الف" و "ب" با این شیوه، به این شکله که ابتدا فرد "الف" با کلید خصوصی خودش متن رو و یا خلاصه ای از متن درهمسازی (هش) شده رو امضا می کنه و بعد با کلید عمومی فرد "ب" متن اصلی رو رمز می کنه و بعد متن رمز شده و متن امضا شده رو در قالب یک پیغام ارسال می کنه. فرد "ب" با دریافت پیام اول با کلید خصوصی خودش متن رو از حالت رمز در میاره و بعد با کلید عمومی شخص "الف" صحت امضا رو چک می کنه. در واقع امضای دیجیتال تایید می کنه که محتوای پیغام دستکاری نشده. در حالت کلی رمزنگاری سری بودن اطلاعات رو تضمین می کنه و اگه همراه با امضای دیجیتال باشه در کنار سری بودن، صحت و غیر قابل انکار بودن اطلاعات هم چک میشه. این بحث خیلی مفصله و به نظر من یه تاپیک جدا می طلبه. البته فصل 8 کتاب شبکه های کامپیوتری تنن بام با عنوان امنیت مفصل و جامع در این مورد توضیح داده. فکر می کنم این اندازه توضیح به عنوان مقدمه این بخش کافی باشه . برای درهمسازی از دو نمونه از توابع MD5 و SHA1 استفاده میشه و برای رمزنگاری نامتقارن از RSA . حالا در ارتباط با توابع این کلاس: توابع computeMD5 و computeSHA1 برای درهمسازی استفاده میشن یعنی یک متن رو به صورت رشته دریافت و یک متن درهمریخته رو به صورت رشته بر می گردونن. با ذکر این نکته که این متن درهمسازی شده دیگه قابل برگشت نیست. این تابع به عنوان مثال برای هش کردن پسوورد مناسبه. تابع getRSAkeys برای تولید کلید های خصوصی و عمومیه.خروجی اون یک کلید عمومی و یک کلید عمومی_خصوصیه. که کاربر بعد از دریافت، اون رو روی یه فایل متنی ذخیره می کنه و در مواقع لزوم از اون استفاده می کنه. تابع EncryptRSAwithPublickey برای رمزنگاری با کلید عمومی به کار میره که مورد کاربردش گفته شد. این تابع کلید عمومی و متن اصلی رو دریافت و متن رمز شده رو بر می گردونه. تابع DecryptRSAwithPublicPrivatekey با دریافت کلید عمومی _خصوصی و متن رمز شده متن اصلی رو بر می گردونه. در واقع متن رو از رمز در میاره. تابع SignwithRSA هم برای امضای دیجیتال کاربرد داره که با دریافت کلید عمومی _ خصوصی و متن (که می تونیم قبلش اونو با توابع گفته شده هش کنیم) متن امضا شده رو بر می گردونه. تابع VeryfywithRSA با در یافت کلید عمومی و متن اصلی (که قبلا توسط گیرنده از رمز دراومده) و متن امضا شده صحت امضای فرستنده رو چک می کنه. خروجی اون یک مقدار بولینه . تابع EncryptAES برای رمز نگاری به صورت متقارن استفاده میشه که متن اصلی، کلید و یک عدد تصادفی 16 تایی رو به عنوان ورودی دریافت و متن رمز شده رو بر میگردونه و تابع DecryptAES برای رمزگشایی متقارن به کار میره به این شکل که متن رمز شده، کلید و یک عدد تصادفی 16 تایی رو میگیره و متن اصلی رو برمی گردونه. امیدوارم توضیحاتم مفید واقع بشه.
علاقه مندی ها (Bookmarks)