Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
test_CryptoUtils.cs42 linesDownload Raw Back to csharp
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Management.Automation.Internal;6using System.Text;7using Xunit;8 9namespace PSTests.Parallel10{11    public static class CryptoUntilsTests12    {13        [Fact]14        public static void TestSessionKeyExchange()15        {16            using (var cryptoClient = PSRSACryptoServiceProvider.GetRSACryptoServiceProviderForClient())17            using (var cryptoServer = PSRSACryptoServiceProvider.GetRSACryptoServiceProviderForServer())18            {19                // generate, export, import public key20                cryptoClient.GenerateKeyPair();21                // public key generated by client22                string publicKey = cryptoClient.GetPublicKeyAsBase64EncodedString();23                cryptoServer.ImportPublicKeyFromBase64EncodedString(publicKey); // sent to and imported by server24 25                // generate, export, import session key26                cryptoServer.GenerateSessionKey();   // server provides the session key?27                string sessionKey = cryptoServer.SafeExportSessionKey();28                cryptoClient.ImportSessionKeyFromBase64EncodedString(sessionKey);29 30                // encrypt31                byte[] plainText = Encoding.UTF8.GetBytes("here is a message");32                byte[] cipherText = cryptoClient.EncryptWithSessionKey(plainText);33 34                // decrypt35                byte[] decrypt = cryptoServer.DecryptWithSessionKey(cipherText);36 37                Assert.Equal(plainText, decrypt);38            }39        }40    }41}42