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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace outreach.Classes
{
    public static class Cryptography
    {
        /// <summary>
        /// Encryptes a string using the supplied key. Encoding is done using RSA encryption.
        /// </summary>
        /// <param name="stringToEncrypt">String that must be encrypted.</param>
        /// <param name="key">Encryptionkey.</param>
        /// <returns>A string representing a byte array separated by a minus sign.</returns>
        /// <exception cref="ArgumentException">Occurs when stringToEncrypt or key is null or empty.</exception>
        /// 

        private static string key = "<RSAKeyValue><Modulus>ynKHV6Tm/tV7ZsTRMLNjMqlAkbftMXA/OkM5zi6+ih371Jgi6ZNX2T4ml0DmK1hojwumHadiX3YoPlaJ/xiys4NRxbDH9VuJJeTdZvoB8heKxACaIw9l1CCTEkx3jYVJ+h6M10fV3wChD0BOtiLg3rMO64LpR2DAGZNbVjXL3h0=</Modulus><Exponent>AQAB</Exponent><P>5QUpIuwCOqR03GqI0K7Y4yWn0hvYa7b9RkCA9Y1QQ0IyBqfiTYOT87cKBRsu4FxH6tdY0khfFCskFgumx22gOw==</P><Q>4kv69rnEMxSOzmg4ZkAuxrUc6wZCuIJAgSL9gGBFukQSIB6UVWXYhNUDzklcMtGE/lRmwJvwSIlpZBlVtlYthw==</Q><DP>ae7LKlYUad+sFlBI3I4j0F2YlL1AjAJmgNpRTEODPrkdvqplKQmVpAOkZNxAAJNuyJe3g/zpzcBuvqvBBzoUBQ==</DP><DQ>M+iL27aG+9SWYWBkt4e3cxsuU/burRYrp7OYBK+QrwZYRgfdrK0c+nNGWTZYsMuAvzorC7l5Z5olk7GACMBB1w==</DQ><InverseQ>UuLEf34YsvFxzgzKhdONvFhsSDRleC5rPR/XvpJl9NEikwXQHTr4547hYM+4soHRdBaiUFdzNtxLMlRW481L1A==</InverseQ><D>aNSFYMzzEGJbVAv2htFdPI6H8Ozc1gzZsMG+3RU3dS2xiZR9/5VcmBGmygvBJBDTk77kddaHCgeVLzKAoqeXYJGJgiNQgByE+jQz70HkOaAy0muNbAxLOk9UY6lkJ9kVbtSu1LyR83yj4+kSGNo4T63LBKV07TirAhDMJAk3o8E=</D></RSAKeyValue>";

        private static RSACryptoServiceProvider rsa = null;

        public static string Encrypt(this string stringToEncrypt)
        {
            if (string.IsNullOrEmpty(stringToEncrypt))
            {
                return stringToEncrypt;
            }


                if (rsa == null)
                {
                    CspParameters cspp = new CspParameters();

                    //Use MachineKeyStore to get around some permissions problems with the user store with websites (or tweak iis setting to load user profile).
                    cspp.Flags = CspProviderFlags.UseMachineKeyStore;
                    cspp.KeyNumber = (int)KeyNumber.Exchange;
                    rsa = new RSACryptoServiceProvider(cspp);
                }
                rsa.FromXmlString(key);
                byte[] bytes = rsa.Encrypt(System.Text.UTF8Encoding.UTF8.GetBytes(stringToEncrypt), true);

                return BitConverter.ToString(bytes);

        }

        /// <summary>
        /// Decryptes a string using the supplied key. Decoding is done using RSA encryption.
        /// </summary>
        /// <param name="stringToDecrypt">String that must be decrypted.</param>
        /// <param name="key">Decryptionkey.</param>
        /// <returns>The decrypted string or null if decryption failed.</returns>
        /// <exception cref="ArgumentException">Occurs when stringToDecrypt or key is null or empty.</exception>
        public static string Decrypt(this string stringToDecrypt)
        {
            string result = null;

            if (string.IsNullOrEmpty(stringToDecrypt))
            {
                return stringToDecrypt;
            }

            try
            {
                if (rsa == null)
                {
                    CspParameters cspp = new CspParameters();

                    //Use MachineKeyStore to get around some permissions problems with the user store with websites (or tweak iis setting to load user profile).
                    cspp.Flags = CspProviderFlags.UseMachineKeyStore;
                    cspp.KeyNumber = (int)KeyNumber.Exchange;
                    rsa = new RSACryptoServiceProvider(cspp);
                }
                    rsa.FromXmlString(key);
                    string[] decryptArray = stringToDecrypt.Split(new string[] { "-" }, StringSplitOptions.None);
                    byte[] decryptByteArray = Array.ConvertAll<string, byte>(decryptArray, (s => Convert.ToByte(byte.Parse(s, System.Globalization.NumberStyles.HexNumber))));


                    byte[] bytes = rsa.Decrypt(decryptByteArray, true);

                    result = System.Text.UTF8Encoding.UTF8.GetString(bytes);
            }
            finally
            {
                // no need for further processing
            }

            return result;
        }


    }
}