SslHelper.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Security.Authentication;
  4. using System.Security.Cryptography.X509Certificates;
  5. using System.Net.Security;
  6. namespace IFramework.Net.WebSocket
  7. {
  8. public class SslHelper
  9. {
  10. private string certificate = string.Empty;
  11. X509Certificate serverCertificate = null;
  12. private int bufSize = 4096;
  13. public SslHelper(string certificateFile)
  14. {
  15. serverCertificate = X509Certificate.CreateFromCertFile(certificateFile);
  16. }
  17. public byte[] DeCryptFromServer(byte[] buffer, int offset, int size)
  18. {
  19. using (SslStream sslStream = new SslStream(new MemoryStream(buffer, offset, size), false))
  20. {
  21. sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls, true);
  22. List<byte> msg = new List<byte>(bufSize);
  23. int b = -1;
  24. {
  25. b = sslStream.ReadByte();
  26. if (b != -1) msg.Add((byte)b);
  27. } while (b != -1) ;
  28. return msg.ToArray();
  29. }
  30. }
  31. public byte[] EnCryptFromServer()
  32. {
  33. return null;
  34. }
  35. public byte[] DeCryptFromClient()
  36. {
  37. return null;
  38. }
  39. public byte[] EnCryptFromClient()
  40. {
  41. return null;
  42. }
  43. }
  44. }