原文链接
http://blog.csdn.net/wuyb_2004/article/details/51393290

using System;
using System.Collections;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Text;
using System.Security.Cryptography.X509Certificates;

namespace Examples.System.Net
{
public class SslTcpClient
{
private static Hashtable certificateErrors = new Hashtable();
// The following method is invoked by the RemoteCertificateValidationDelegate.
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true; Console.WriteLine("Certificate error: {0}", sslPolicyErrors); // Do not allow this client to communicate with unauthenticated servers.
return false;
} public static void RunClient(string machineName)
{
// Create a TCP/IP client socket.
// machineName is the host running the server application.
TcpClient client = new TcpClient(machineName, 901);
Console.WriteLine("Client connected.");
// Create an SSL stream that will close the client's stream.
SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
// The server name must match the name on the server certificate. X509Store store = new X509Store(StoreName.Root);
store.Open(OpenFlags.ReadWrite); //// 检索证书
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySubjectName, "TestClient", false);
try
{
sslStream.AuthenticateAsClient("TestServer", certs, SslProtocols.Tls, false);
}
catch (AuthenticationException e)
{
Console.WriteLine("Exception: {0}", e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
}
Console.WriteLine("Authentication failed - closing the connection.");
client.Close();
return;
}
// Encode a test message into a byte array.
// Signal the end of the message using the "<EOF>".
byte[] messsage = Encoding.UTF8.GetBytes("Hello from the client.<EOF>");
// Send hello message to the server.
sslStream.Write(messsage);
sslStream.Flush();
// Read message from the server.
string serverMessage = ReadMessage(sslStream);
Console.WriteLine("Server says: {0}", serverMessage); messsage = Encoding.UTF8.GetBytes("exit");
sslStream.Write(messsage);
sslStream.Flush(); // Close the client connection.
client.Close();
Console.WriteLine("Client closed.");
} static string ReadMessage(SslStream sslStream)
{
// Read the message sent by the server.
// The end of the message is signaled using the
// "<EOF>" marker.
byte[] buffer = new byte[2048];
StringBuilder messageData = new StringBuilder();
int bytes = -1;
do
{
bytes = sslStream.Read(buffer, 0, buffer.Length); // Use Decoder class to convert from bytes to UTF8
// in case a character spans two buffers.
Decoder decoder = Encoding.UTF8.GetDecoder();
char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
decoder.GetChars(buffer, 0, bytes, chars, 0);
messageData.Append(chars);
// Check for EOF.
if (messageData.ToString().IndexOf("<EOF>") != -1)
{
break;
}
} while (bytes != 0); return messageData.ToString();
} private static void DisplayUsage()
{
Console.WriteLine("To start the client specify:");
Console.WriteLine("clientSync machineName [serverName]");
Environment.Exit(1);
} public static void Main(string[] args)
{
string machineName = null;
machineName = "192.168.1.25";
try
{
RunClient(machineName);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}

使用C#实现SSLSocket加密通讯 Https的更多相关文章

  1. Javascript到PHP加密通讯的简单实现

    其实内容主要来源于上一篇博文,只是重新组织了语言,并做了原理性的阐述,更容易理解:P ----------------------------------------- 华丽的分割线 -------- ...

  2. EFK教程(4) - ElasticSearch集群TLS加密通讯

    基于TLS实现ElasticSearch集群加密通讯 作者:"发颠的小狼",欢迎转载 目录 ▪ 用途 ▪ ES节点信息 ▪ Step1. 关闭服务 ▪ Step2. 创建CA证书 ...

  3. EFK-4::ElasticSearch集群TLS加密通讯

    转载自:https://mp.weixin.qq.com/s?__biz=MzUyNzk0NTI4MQ==&mid=2247483822&idx=1&sn=6813b22eb5 ...

  4. axios前端加密通讯的处理

    axios前端加密通讯的处理 今天谈一谈前段时间,项目中遇见的前端axios加解密的处理. 先谈谈项目前景,因为安全的要求,所以我们要把前端所有的请求都得加密与服务端应用进行通讯,当然服务端的响应也是 ...

  5. 对称与非对称加密;SSL;HTTPS;AJP

    1.对称加密就是加密与解密的时候都是用一个密码 2.非对称加密,有一对密码A,B:用A加密就得用B解密,相对的用B加密就得用A解密 3.公钥与私钥,这一对密码,随便拿一个公布出去,那个就是公钥,剩下一 ...

  6. md5加密、Des加密对称可逆加密、RSA非对称可逆加密、https单边验证、银行U盾双边认证

    1.md5不可逆的加密方式,加密成一个32位的字符串.算法是公开的,任何语言的加密结果都是一样的.总有可能是重复的.     用途:             (1)防止明文存储:可以用作密码加密    ...

  7. linux-ssh加密与https安全-9

    非对称加密算法:RSA,DSA/DSS 对称加密算法:AES,RC4,3DES HASH算法:MD5,SHA1,SHA256 hash就是找到一种数据内容和数据存放地址之间的映射关系 (1) 文件校验 ...

  8. JS到PHP使用RSA算法进行加密通讯

    我们平时做用户登录表单提交,用户名密码都是明文直接POST到后端,这样很容易被别人从监听到. 在js上做rsa,感觉jsencrypt这个是封装的比较好的,但用起来还是遇到了些坑,所以踩进代码里填填坑 ...

  9. 确保安全的HTTPS(使用混合加密的HTTPS,前端面试常问)第二篇

    苹果已经确定,在iOS9中通信机制采用HTTPS了. 第一篇:http://www.cnblogs.com/ziyi--caolu/p/4742577.html 上一篇详细介绍了为什么要对HTTP进行 ...

随机推荐

  1. wpa_supplicant移植与使用(转)

    下载wpa_supplicant最新版和openssl(编译wpa_supplicant需要openssl的库) 我这里使用的是wpa_supplicant-0.7.3.tar.gz和openssl- ...

  2. 【原创】7. MYSQL++中的查询结果获取(各种Result类型)

    在本节中,我将首先介绍MYSQL++中的查询的几个简单例子用法,然后看一下mysqlpp::Query中的几个与查询相关的方法原型(重点关注返回值),最后对几个关键类型进行解释. 1. MYSQL++ ...

  3. C++获取时间并命名为文件名

    #include <time.h> char pStrPath1[20];time_t currTime;struct tm *mt;Mat saveImg1; currTime = ti ...

  4. 17.SQL 约束

    约束用于限制加入表的数据的类型. 可以在创建表时规定约束(通过 CREATE TABLE 语句),或者在表创建之后也可以(通过 ALTER TABLE 语句). 我们将主要探讨以下几种约束: NOT ...

  5. grid search 超参数寻优

    http://scikit-learn.org/stable/modules/grid_search.html 1. 超参数寻优方法 gridsearchCV 和  RandomizedSearchC ...

  6. CF407B Long Path

    好玩的题. 首先我们(看一下题解之后)发现当你第一次走到了一个点的时候,那么它之前的所有点一定都访问过了偶数次. 假设我们第一次走到了一个点$i$,那么$i - 1$一定访问了偶数次,那么第一次走$i ...

  7. ESP8266-iot-简介1

    ESP8266简介

  8. 使用Adobe Illustrator + ArcGIS绘制地图 | Map Design Using ArcGIS + Adobe Illustrator

    国内GIS/Cartography同行大部分使用CorelDraw绘制地图.相比之下,国外同行则更多使用Adobe Illustrator绘制地图.CorelDraw和Illustrator两个软件均 ...

  9. [算法基础]Big O Notation时间复杂度计算方法

    首先一点就是无视任何常量 从最简单的开始 statement; 这段时间复杂度为常数1,所以O(1). 然后 ; i < N; i++ ) statement; 这一段是线性的,则时间复杂度为N ...

  10. 那些年我们追过的SQL

    SQL是大学必修课程之一二维表结构,看着就是一种美感. 针对近期感情,聊一聊,在平时容易犯的一个错误,看看你是不是中枪了. 我们还是选用传统的student表(请不要考虑表的结构是否合理)ID     ...