forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_CryptoUtils.cs
More file actions
41 lines (35 loc) · 1.54 KB
/
test_CryptoUtils.cs
File metadata and controls
41 lines (35 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Management.Automation.Internal;
using System.Text;
using Xunit;
namespace PSTests.Parallel
{
public static class CryptoUntilsTests
{
[Fact]
public static void TestSessionKeyExchange()
{
using (var cryptoClient = PSRSACryptoServiceProvider.GetRSACryptoServiceProviderForClient())
using (var cryptoServer = PSRSACryptoServiceProvider.GetRSACryptoServiceProviderForServer())
{
// generate, export, import public key
cryptoClient.GenerateKeyPair();
// public key generated by client
string publicKey = cryptoClient.GetPublicKeyAsBase64EncodedString();
cryptoServer.ImportPublicKeyFromBase64EncodedString(publicKey); // sent to and imported by server
// generate, export, import session key
cryptoServer.GenerateSessionKey(); // server provides the session key?
string sessionKey = cryptoServer.SafeExportSessionKey();
cryptoClient.ImportSessionKeyFromBase64EncodedString(sessionKey);
// encrypt
byte[] plainText = Encoding.UTF8.GetBytes("here is a message");
byte[] cipherText = cryptoClient.EncryptWithSessionKey(plainText);
// decrypt
byte[] decrypt = cryptoServer.DecryptWithSessionKey(cipherText);
Assert.Equal(plainText, decrypt);
}
}
}
}