原文链接
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. JDK和CGLIB生成动态代理类的区别(转)

     关于动态代理和静态代理 当一个对象(客户端)不能或者不想直接引用另一个对象(目标对象),这时可以应用代理模式在这两者之间构建一个桥梁--代理对象. 按照代理对象的创建时期不同,可以分为两种: 静态代 ...

  2. http头部信息解析

    HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type. 2. Accept-Chars ...

  3. 刷题向》DP》放苹果 (normal)

    这篇博客可能字数比较多,而且很难讲清楚,我会努力给你们讲清楚: 首先,放苹果是一道DP,之所以难,是因为很难想到,我的确有同学用三维数组做出来,然而三维的的确比二维好理解,但三维复杂度太高,虽然DP一 ...

  4. 类型或命名空间名称“Interop”在类或命名空间“Microsoft.Office”中不存在(是否缺少程序集引用?)

    准备用C#编写Web程序,生成Excel报表,在使用下面语句时报错. using Microsoft.Office.Interop.Excel; 报错信息:类型或命名空间名称“Interop”在类或命 ...

  5. NanoPi2

    https://item.taobao.com/item.htm?spm=a230r.1.14.9.Ijhc8S&id=526981593477&ns=1&abbucket=1 ...

  6. Apache logresolve命令

    一.简介 logresolve是一个解析Apache访问日志中IP地址的后处理程序. 二.语法 logresolve [ -s filename ] [ -c ] < access_log &g ...

  7. 【未整理】web.xml加载顺序.RP

    一 1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Ser ...

  8. Java 设计模式 和七大设计原则

    创建型模式 抽象工厂模式(Abstract factory pattern): 提供一个接口, 用于创建相关或依赖对象的家族, 而不需要指定具体类. 生成器模式(Builder pattern): 使 ...

  9. OSG3.2+Qt5.2.1+VS2012+OSGEarth 2.5编译问题记录

    问题1:CMake Error at D:/Qt/Qt5.2.1/5.2.1/msvc2012_64_opengl/lib/cmake/Qt5Gui/Qt5GuiConfigExtras.cmake: ...

  10. web.xml配置及详解

    1.web.xml 是网络程序中的一个很重要的配置文件. 2.XML基础标准是为XML的进一步实用化制定的标准,它规定了采用XML制定标准时的一些公用特征.方法或规则.XML Schema描述了更加严 ...