Advertisement
Guest User

C# Extension Method Issue

a guest
Mar 28th, 2015
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4.  
  5. namespace MrSparklysManagementSystem
  6. {
  7.     public class DataProtect
  8.     // Class to encrypt and decrypt strings.
  9.     // Various terms were not recognized until I added the System.Security reference manually via the explorer!
  10.     {
  11.         public string Protect  // ISSUE HERE: "Extension methods must be static"
  12.         // Encrypts a string.
  13.         (
  14.             this string decryptedText,
  15.             string optionalEntropy = null,
  16.             DataProtectionScope scope = DataProtectionScope.CurrentUser
  17.         )
  18.         {
  19.             if (decryptedText == null)
  20.                 throw new ArgumentNullException("decryptedText");
  21.             byte[] clearBytes = Encoding.UTF8.GetBytes(decryptedText);
  22.             byte[] entropyBytes = string.IsNullOrEmpty(optionalEntropy)
  23.                 ? null
  24.                 : Encoding.UTF8.GetBytes(optionalEntropy);
  25.             byte[] encryptedBytes = ProtectedData.Protect(clearBytes, entropyBytes, scope);
  26.             return Convert.ToBase64String(encryptedBytes);
  27.         }
  28.  
  29.         public string Unprotect // ISSUE HERE: "Extension methods must be static"
  30.         // Decrypts a string.
  31.         (
  32.             this string encryptedText,
  33.             string optionalEntropy = null,
  34.             DataProtectionScope scope = DataProtectionScope.CurrentUser
  35.         )
  36.         {
  37.             if (encryptedText == null)
  38.                 throw new ArgumentNullException("encryptedText");
  39.             byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
  40.             byte[] entropyBytes = string.IsNullOrEmpty(optionalEntropy)
  41.                 ? null
  42.                 : Encoding.UTF8.GetBytes(optionalEntropy);
  43.             byte[] clearBytes = ProtectedData.Unprotect(encryptedBytes, entropyBytes, scope);
  44.             return Encoding.UTF8.GetString(clearBytes);
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement