Exemplo n.º 1
0
    private static ServerConnection ProcessServer(Socket s)
    {
        byte[] test = new byte[23];
        s.Receive(test);
        Console.WriteLine(Encoding.ASCII.GetString(test));
        ServerConnection conn = new ServerConnection();

        conn.Sock = s;
        conn.Aes  = new AesCryptoServiceProvider();
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
            using (RSACng rsa = new RSACng(3072))
            {
                var bytes = new byte[3072];
                conn.Sock.Receive(bytes);
                rsa.ImportRSAPublicKey(bytes, out _);
                conn.Sock.Send(rsa.Encrypt(conn.Aes.Key, RSAEncryptionPadding.Pkcs1));
            }
        }
        else
        {
            using (RSAOpenSsl rsa = new RSAOpenSsl(3072))
            {
                var bytes = new byte[3072];
                conn.Sock.Receive(bytes);
                rsa.ImportRSAPublicKey(bytes, out _);
                conn.Sock.Send(rsa.Encrypt(conn.Aes.Key, RSAEncryptionPadding.Pkcs1));
            }
        }
        var headerBytes = new byte[20];

        conn.Sock.Receive(headerBytes);
        conn.Aes.IV = headerBytes.Take(16).ToArray();
        int msgLength      = BitConverter.ToInt32(headerBytes.Skip(16).Take(4).ToArray());
        var encryptedBytes = new byte[msgLength];

        conn.Sock.Receive(encryptedBytes);
        string msg;

        using (MemoryStream ms = new MemoryStream(encryptedBytes))
        {
            using (CryptoStream cs = new CryptoStream(ms, conn.Aes.CreateDecryptor(), CryptoStreamMode.Read))
            {
                using (StreamReader sr = new StreamReader(cs))
                {
                    msg = sr.ReadToEnd();
                }
            }
        }
        if (msg != "OK!")
        {
            throw new Exception("Server didn't return \"OK!\"");
        }
        return(conn);
    }
Exemplo n.º 2
0
 public byte[] Encrypt(
     byte[] toBeEncrypted,
     JsonWebKey jsonWebKey)
 {
     if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
     {
         using (var rsa = new RSACryptoServiceProvider())
         {
             rsa.FromXmlStringNetCore(jsonWebKey.SerializedKey);
             return(rsa.Encrypt(toBeEncrypted, _oaep));
         }
     }
     else
     {
         using (var rsa = new RSAOpenSsl())
         {
             rsa.FromXmlStringNetCore(jsonWebKey.SerializedKey);
             return(rsa.Encrypt(toBeEncrypted, RSAEncryptionPadding.Pkcs1));
         }
     }
 }
Exemplo n.º 3
0
        public byte[] Encrypt(
            byte[] toBeEncrypted,
            JsonWebKey jsonWebKey)
        {
#if UAP
            // TODO : Implement
            return(null);
#elif NET46 || NET45
            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.FromXmlString(jsonWebKey.SerializedKey);
                return(rsa.Encrypt(toBeEncrypted, _oaep));
            }
#elif NETSTANDARD
            using (var rsa = new RSAOpenSsl())
            {
                rsa.FromXmlString(jsonWebKey.SerializedKey);
                return(rsa.Encrypt(toBeEncrypted, RSAEncryptionPadding.Pkcs1));
            }
#endif
        }