-
Notifications
You must be signed in to change notification settings - Fork 695
/
EncryptionUtility.cs
40 lines (34 loc) · 1.56 KB
/
EncryptionUtility.cs
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Security.Cryptography;
using System.Text;
using NuGet.Common;
namespace NuGet.Configuration
{
public static class EncryptionUtility
{
private static readonly byte[] _entropyBytes = Encoding.UTF8.GetBytes("NuGet");
public static string EncryptString(string value)
{
if (!RuntimeEnvironmentHelper.IsWindows && !RuntimeEnvironmentHelper.IsMono)
{
throw new NotSupportedException(Resources.Error_EncryptionUnsupported);
}
var decryptedByteArray = Encoding.UTF8.GetBytes(value);
var encryptedByteArray = ProtectedData.Protect(decryptedByteArray, _entropyBytes, DataProtectionScope.CurrentUser);
var encryptedString = Convert.ToBase64String(encryptedByteArray);
return encryptedString;
}
public static string DecryptString(string encryptedString)
{
if (!RuntimeEnvironmentHelper.IsWindows && !RuntimeEnvironmentHelper.IsMono)
{
throw new NotSupportedException(Resources.Error_EncryptionUnsupported);
}
var encryptedByteArray = Convert.FromBase64String(encryptedString);
var decryptedByteArray = ProtectedData.Unprotect(encryptedByteArray, _entropyBytes, DataProtectionScope.CurrentUser);
return Encoding.UTF8.GetString(decryptedByteArray);
}
}
}