Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Security.Cryptography;
- using System.Text;
- namespace MrSparklysManagementSystem
- {
- public class DataProtect
- // Class to encrypt and decrypt strings.
- // Various terms were not recognized until I added the System.Security reference manually via the explorer!
- {
- public string Protect // ISSUE HERE: "Extension methods must be static"
- // Encrypts a string.
- (
- this string decryptedText,
- string optionalEntropy = null,
- DataProtectionScope scope = DataProtectionScope.CurrentUser
- )
- {
- if (decryptedText == null)
- throw new ArgumentNullException("decryptedText");
- byte[] clearBytes = Encoding.UTF8.GetBytes(decryptedText);
- byte[] entropyBytes = string.IsNullOrEmpty(optionalEntropy)
- ? null
- : Encoding.UTF8.GetBytes(optionalEntropy);
- byte[] encryptedBytes = ProtectedData.Protect(clearBytes, entropyBytes, scope);
- return Convert.ToBase64String(encryptedBytes);
- }
- public string Unprotect // ISSUE HERE: "Extension methods must be static"
- // Decrypts a string.
- (
- this string encryptedText,
- string optionalEntropy = null,
- DataProtectionScope scope = DataProtectionScope.CurrentUser
- )
- {
- if (encryptedText == null)
- throw new ArgumentNullException("encryptedText");
- byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
- byte[] entropyBytes = string.IsNullOrEmpty(optionalEntropy)
- ? null
- : Encoding.UTF8.GetBytes(optionalEntropy);
- byte[] clearBytes = ProtectedData.Unprotect(encryptedBytes, entropyBytes, scope);
- return Encoding.UTF8.GetString(clearBytes);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement