none
System.Security.Cryptography.ProtectedData - Unable to reference this class.

    General discussion

  • I was attempting to use this class however I do not see the ProtectedData class under System.Security.Cryptography. Any reason why!
     
    I get the message "ProtectedData is not declared. What am I missing? (Its not visible using intellisense either. I do have the 2.0 framework installed.
     
    This is what I have so far ...
     
    Imports System.Security.Cryptography
    Imports System.text.Encoding
    Imports System
     
    Public Class Security
    Private Shared arrEntropybyte As Byte() = {1,2,3,4,5,6,7,8,9,0}
     
    Public Shared Function Encrypt(ByVal stringToEncrypt As String) As String
    Dim arrStringbyte As Byte()
    Dim encryptedDBString As Byte()
    Try
    ' Convert the string to a byte array:
    arrStringbyte = Unicode.GetBytes(stringToEncrypt)
    ' Encrypt string
    encryptedDBString = ProtectedData.Protect(stringToEncrypt, arrEntropybyte, DataProtectionScope.CurrentUser)
    Catch ex As Exception
    Throw ex
    End Try
    End Function

    End Class

     

    Friday, August 17, 2007 3:33 PM

All replies

  • The ProtectedData class is in the System.Security.dll assembly.  Does your project contain a reference to this assembly?
    Friday, August 17, 2007 4:44 PM
  • Hi agargye,

     

    We are changing the issue type to “Comment” because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question” by editing your initial post and changing the radio button at the top of the post editor window. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.  

     

    Thank you! 

    Tuesday, August 21, 2007 2:46 AM
  • My apologies .. I forgot about this post. This is what I implemented eventually if it helps anyone (mostly borrowed code and customized! so credit is not all mine :P)

    #Region " imports "
    Imports System.Security.Cryptography
    #End Region
    
    Public NotInheritable Class Cryptonite
    #Region " Private " Private Shared TripleDes As New TripleDESCryptoServiceProvider ''' <summary> ''' Private method: ''' </summary> ''' <param name="key"></param> ''' <param name="length"></param> ''' <returns></returns> ''' <remarks></remarks> Private Function TruncateHash(ByVal key As String, ByVal length As Integer) As Byte() Dim sha1 As New SHA1CryptoServiceProvider ' Hash the key. Dim keyBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(key) Dim hash() As Byte = sha1.ComputeHash(keyBytes) ' Truncate or pad the hash. ReDim Preserve hash(length - 1) Return hash End Function #End Region #Region " Constructors " ''' <summary> ''' New Constructor ''' </summary> ''' <remarks></remarks> Sub New() Dim key As String = "YOUR KEY HERE" ' Initialize the crypto provider. TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8) TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8) End Sub ''' <summary> ''' Overloaded constructor accepts custome key ''' </summary> ''' <param name="key"></param> ''' <remarks></remarks> Sub New(ByVal key As String) ' Initialize the crypto provider. TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8) TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8) End Sub #End Region #Region " Public Methods " ''' <summary> ''' Encrypts encrypted parameter string ''' </summary> ''' <param name="plaintext">text to encrypt</param> ''' <returns>Encrypted String</returns> ''' <remarks></remarks> Public Shared Function Encrypt(ByVal plaintext As String) As String ' Convert the plaintext string to a byte array. Dim plaintextBytes() As Byte = _ System.Text.Encoding.Unicode.GetBytes(plaintext) ' Create the stream. Dim ms As New System.IO.MemoryStream ' Create the encoder to write to the stream. Dim encStream As New CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write) ' Use the crypto stream to write the byte array to the stream. encStream.Write(plaintextBytes, 0, plaintextBytes.Length) encStream.FlushFinalBlock() ' Convert the encrypted stream to a printable string. Return Convert.ToBase64String(ms.ToArray) End Function ''' <summary> ''' Decrypts encrypted parameter string ''' </summary> ''' <param name="encryptedtext">Text to decrypt.</param> ''' <returns>decrypted string</returns> ''' <remarks></remarks> Public Shared Function Decrypt(ByVal encryptedtext As String) As String ' Convert the encrypted text string to a byte array. Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext) ' Create the stream. Dim ms As New System.IO.MemoryStream ' Create the decoder to write to the stream. Dim decStream As New CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write) ' Use the crypto stream to write the byte array to the stream. decStream.Write(encryptedBytes, 0, encryptedBytes.Length) decStream.FlushFinalBlock() ' Convert the plaintext stream to a string. Return System.Text.Encoding.Unicode.GetString(ms.ToArray) End Function #End Region End Class


    Friday, July 24, 2009 3:09 PM