Hi All,
I want to download some files on Windows 7 & 8.1 using cipher suites which are not available by default on Win 7 & 8.1
I am using HttpClient class (with TLS 1.2 as default protocol) and .NET framework 4.6 to download and it is working fine on Windows 10. However the same code does not work on Windows 7 & 8.1 because of missing cipher suites on these OS.
I found we can programmatically select a particular cipher suite using Bouncy Castle as per links:
need-to-set-cipher-suite-at-the-code-in-c
and
bouncycastle-net-custom-tlsclient-for-anon-ciphersuite-working-on-android-but
Using the example provided in the above links, I am able to create the connection using my preferred cipher suite (TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) and able to download files from source server on Win 7 & 8.1
However the problem I am facing now is few of the files have certain characters in them because of which even though the file is downloaded with correct length, because of some characters which do not transfer/download properly, the downloaded files are corrupted.
To resolve this, I tried to force the byte array[] to be returned in UTF-8 encoding by adding "Accept-Charset: utf-8" in the Http GET request, still the bytes are not downloaded in the correct way which is leading to corrupting.
Could anyone provide me with pointers on how I can make Bouncy Castle to send me the bytes in the encoding I want? Or I am I missing certain settings/properties which I could use to correct the issue.
Please see below code block which I am using as of now.
using Org.BouncyCastle.Crypto.Tls;
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
public void GetFileStream(string fileUrl, out Stream stream)
string contentType = string.Empty;
long fileSize = 0;
string url = "sourceserverhostname";
TcpClient client = new TcpClient(url, 443);
NetworkStream targetStream = client.GetStream();
targetStream.ReadTimeout = 10000;
targetStream.WriteTimeout = 10000;
TlsClientProtocol protocol = new TlsClientProtocol(targetStream, new Org.BouncyCastle.Security.SecureRandom());
protocol.Connect(new CustomTlsClient());
string message = @"GET {0} HTTP/1.1
" + Environment.NewLine + "Accept-Charset: utf-8" + "\r\n\r\n";
message = string.Format(message, fileUrl);
//Send Http Get Request
byte[] bytes = Encoding.UTF8.GetBytes(message);
protocol.Stream.Write(bytes, 0, bytes.Length);
var reader = new StreamReader(protocol.Stream, Encoding.UTF8);
//check and extract http response parameters
int index = 0;
string lineData = reader.ReadLine();
if (!string.IsNullOrEmpty(lineData))
if (lineData.ToLower().Contains("content-type"))
string[] keyValueList = lineData.Split(new char[] { ':' });
if (keyValueList != null && keyValueList.Length > 1)
contentType = keyValueList[1];
if (lineData.ToLower().Contains("content-length"))
string[] keyValueList = lineData.Split(new char[] { ':' });
if (keyValueList != null && keyValueList.Length > 1)
fileSize = long.Parse(keyValueList[1]);
index = index + lineData.Length + 2;
break;
} while (!reader.EndOfStream);
//Send Http Get Request again to get hold of Stream object
bytes = Encoding.UTF8.GetBytes(message);
protocol.Stream.Write(bytes, 0, bytes.Length);
//Read the Http Get Request response header content into dataToDiscard
byte[] dataToDiscard = new byte[index + 2];
protocol.Stream.Read(dataToDiscard, 0, index + 2);
result.MimeType = contentType;
result.FileSize = fileSize;
//Return stream object with current pointer set to location from wherer the data starts
stream = protocol.Stream;
//Org.BouncyCastle.Crypto.Encodings.ISO9796d1Encoding iSO9796D1Encoding;
//Org.BouncyCastle.Crypto.Encodings.OaepEncoding oaepEncoding;
//Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding pkcs1Encoding;
catch (Exception exp)
throw exp;
@Timon Yang-MSFT thank you for your suggestion, I have asked question in GitHub repository at link 285
On another note, could you please let me know if there is any way we can do it using .NET framework itself?
The current version I am using is 4.6, if required I can move to higher .NET version if it can solve my above problem on Win 7 & 8.1?
Thanks,
Vinayak