Encryption. DPAPI. MACHINE_STORE. Server re-install ?

Discussion in 'ASP .Net Security' started by Dominick Baier [DevelopMentor], Apr 4, 2006.

  1. yes - exactly.

    the DPAPI machine key is uniqe for every machine. I would not use DPAPI in
    this scenario. If you want to encrypt the data use the normal symmetric/asymmetric
    algorithm that allow you to specify a key.

    ---------------------------------------
    Dominick Baier - DevelopMentor
    http://www.leastprivilege.com

    > Hi there,
    >
    > I've been reading/doing this
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnet
    > sec/html/SecNetHT08.asp
    > and have a question about this:
    > ---------------
    > Note If you use DPAPI with the machine store, the encrypted string
    > is
    > specific to a given computer and therefore you must generate the
    > encrypted
    > data on every computer. Do not copy the encrypted data across
    > computers in a
    > farm or cluster.
    > ------------------
    > My scenario is: I have a web-app in .NET 2.0. The user enters some
    > data. I encrypt it. I store the encrypted data in a SQL server table.
    > I can then retrieve the data, decrypt it, and show it to the user.
    >
    > But what happens if my IIS-server crashes and have to be re-installed
    > ? What does "a given computer" mean ? Is my data forever encrypted and
    > can never be decrypted ?
    >
    > /jim
    >
    Dominick Baier [DevelopMentor], Apr 4, 2006
    #1
    1. Advertising

  2. Dominick Baier [DevelopMentor]

    Jim Andersen Guest

    Hi there,

    I've been reading/doing this
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT08.asp

    and have a question about this:
    ---------------
    Note If you use DPAPI with the machine store, the encrypted string is
    specific to a given computer and therefore you must generate the encrypted
    data on every computer. Do not copy the encrypted data across computers in a
    farm or cluster.
    ------------------

    My scenario is: I have a web-app in .NET 2.0. The user enters some data. I
    encrypt it. I store the encrypted data in a SQL server table. I can then
    retrieve the data, decrypt it, and show it to the user.

    But what happens if my IIS-server crashes and have to be re-installed ? What
    does "a given computer" mean ? Is my data forever encrypted and can never be
    decrypted ?

    /jim
    Jim Andersen, Apr 4, 2006
    #2
    1. Advertising

  3. Dominick Baier [DevelopMentor]

    Jim Andersen Guest

    "Dominick Baier [DevelopMentor]" <>
    skrev i en meddelelse
    news:...
    > yes - exactly.
    >
    > the DPAPI machine key is uniqe for every machine. I would not use DPAPI in
    > this scenario. If you want to encrypt the data use the normal
    > symmetric/asymmetric algorithm that allow you to specify a key.


    I thought I was doing that, and storing the key in DPAPI ?

    I am already specifying one more key (or salt or whatever its called) as it
    suggests in the example in step 5.
    ----
    // Could pass random value (stored by the application) for added
    // security when using DPAPI with the machine store.
    ----

    What do you mean by "the normal symmetric/asymmetric algorithm", and where
    do I store the key, and isn't that a less secure storing area than the
    machine-store ?

    thx
    /jim


    >
    > ---------------------------------------
    > Dominick Baier - DevelopMentor
    > http://www.leastprivilege.com
    >
    >> Hi there,
    >>
    >> I've been reading/doing this
    >> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnet
    >> sec/html/SecNetHT08.asp
    >> and have a question about this:
    >> ---------------
    >> Note If you use DPAPI with the machine store, the encrypted string
    >> is
    >> specific to a given computer and therefore you must generate the
    >> encrypted
    >> data on every computer. Do not copy the encrypted data across
    >> computers in a
    >> farm or cluster.
    >> ------------------
    >> My scenario is: I have a web-app in .NET 2.0. The user enters some
    >> data. I encrypt it. I store the encrypted data in a SQL server table.
    >> I can then retrieve the data, decrypt it, and show it to the user.
    >>
    >> But what happens if my IIS-server crashes and have to be re-installed
    >> ? What does "a given computer" mean ? Is my data forever encrypted and
    >> can never be decrypted ?
    >>
    >> /jim
    >>

    >
    >
    Jim Andersen, Apr 5, 2006
    #3
  4. you are right - this is less secure - depending on your threat model at least...

    DPAPI takes care of the key management for you - but it is bound to a machine
    - so decide yourself what you want to do..

    the other algorithms are in the System.Security.Cryptography namesspace like
    3DES or RijndaelManaged.

    The entropy as the call it just augments the key with additional data - but
    the key is still bound to the machine.

    ---------------------------------------
    Dominick Baier - DevelopMentor
    http://www.leastprivilege.com

    > "Dominick Baier [DevelopMentor]"
    > <> skrev i en meddelelse
    > news:...
    >
    >> yes - exactly.
    >>
    >> the DPAPI machine key is uniqe for every machine. I would not use
    >> DPAPI in this scenario. If you want to encrypt the data use the
    >> normal symmetric/asymmetric algorithm that allow you to specify a
    >> key.
    >>

    > I thought I was doing that, and storing the key in DPAPI ?
    >
    > I am already specifying one more key (or salt or whatever its called)
    > as it
    > suggests in the example in step 5.
    > ----
    > // Could pass random value (stored by the application) for added
    > // security when using DPAPI with the machine store.
    > ----
    > What do you mean by "the normal symmetric/asymmetric algorithm", and
    > where do I store the key, and isn't that a less secure storing area
    > than the machine-store ?
    >
    > thx
    > /jim
    >> ---------------------------------------
    >> Dominick Baier - DevelopMentor
    >> http://www.leastprivilege.com
    >>> Hi there,
    >>>
    >>> I've been reading/doing this
    >>> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnn
    >>> et
    >>> sec/html/SecNetHT08.asp
    >>> and have a question about this:
    >>> ---------------
    >>> Note If you use DPAPI with the machine store, the encrypted string
    >>> is
    >>> specific to a given computer and therefore you must generate the
    >>> encrypted
    >>> data on every computer. Do not copy the encrypted data across
    >>> computers in a
    >>> farm or cluster.
    >>> ------------------
    >>> My scenario is: I have a web-app in .NET 2.0. The user enters some
    >>> data. I encrypt it. I store the encrypted data in a SQL server
    >>> table.
    >>> I can then retrieve the data, decrypt it, and show it to the user.
    >>> But what happens if my IIS-server crashes and have to be
    >>> re-installed ? What does "a given computer" mean ? Is my data
    >>> forever encrypted and can never be decrypted ?
    >>>
    >>> /jim
    >>>
    Dominick Baier [DevelopMentor], Apr 5, 2006
    #4
  5. Hi All,
    I am trying also to do the encrypt-decrypt processes separately.

    I knew that for symmetric encryption, I only have 2 class algorithms:
    Either RijndaelManaged or TripleDES
    I tried the RijndaelManaged class, and everything was OK except a very
    strange sticky error.

    Length of the data to decrypt is invalid
    Line 70: cs.Read(fromEncrypted, 0, fromEncrypted.Length);


    My Code:
    --------------------------------------------------

    using System;

    using System.Data;

    using System.Configuration;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    using System.IO;

    using System.Text;

    using System.Security.Cryptography;

    public class MyEncryption : System.Web.UI.Page

    {

    public MyEncryption()

    {

    }

    public static string Encrypt(string original)

    {

    byte[] encrypted;

    byte[] toEncrypt;

    byte[] key;

    byte[] IV;

    ASCIIEncoding textConverter = new ASCIIEncoding();

    toEncrypt = textConverter.GetBytes(original);

    RijndaelManaged myRijndael = new RijndaelManaged();

    myRijndael.GenerateKey();

    myRijndael.GenerateIV();

    key = myRijndael.Key;

    IV = myRijndael.IV;

    MyEncryption me = new MyEncryption();

    me.SetVariables(key, IV);

    MemoryStream ms = new MemoryStream();

    ICryptoTransform encryptor = myRijndael.CreateEncryptor(key,IV);

    CryptoStream cs = new CryptoStream(ms, encryptor,CryptoStreamMode.Write);

    cs.Write(toEncrypt, 0, toEncrypt.Length);

    cs.FlushFinalBlock();

    encrypted = ms.ToArray();

    string final = Convert.ToBase64String(encrypted);

    return final;

    }

    public static string Decrypt(string encryptedString)

    {

    byte[] key;

    byte[] IV;

    byte[] encrypted;

    byte[] fromEncrypted;

    MyEncryption me = new MyEncryption();

    me.GetVariables(out key, out IV);

    ASCIIEncoding textConverter = new ASCIIEncoding();

    encrypted = textConverter.GetBytes(encryptedString);

    fromEncrypted = new byte[encrypted.Length];

    MemoryStream ms = new MemoryStream(encrypted);

    RijndaelManaged myRijndael = new RijndaelManaged();

    ICryptoTransform decryptor = myRijndael.CreateDecryptor(key,IV);

    CryptoStream cs = new CryptoStream(ms, decryptor,CryptoStreamMode.Read);

    cs.Read(fromEncrypted, 0, fromEncrypted.Length);

    string decryptedString = Convert.ToBase64String(fromEncrypted);

    return decryptedString;

    }

    private void SetVariables(byte[] key, byte[] IV)

    {

    Session["key"] = key;

    Session["IV"] = IV;

    }

    private void GetVariables(out byte[] key, out byte[] IV)

    {

    key = (byte[])Session["key"];

    IV = (byte[])Session["IV"];

    }

    }


    -------------------------------------------------------------------------



    "Jim Andersen" <> wrote in message
    news:...
    >
    > "Dominick Baier [DevelopMentor]" <>
    > skrev i en meddelelse
    > news:...
    >> yes - exactly.
    >>
    >> the DPAPI machine key is uniqe for every machine. I would not use DPAPI
    >> in this scenario. If you want to encrypt the data use the normal
    >> symmetric/asymmetric algorithm that allow you to specify a key.

    >
    > I thought I was doing that, and storing the key in DPAPI ?
    >
    > I am already specifying one more key (or salt or whatever its called) as
    > it suggests in the example in step 5.
    > ----
    > // Could pass random value (stored by the application) for added
    > // security when using DPAPI with the machine store.
    > ----
    >
    > What do you mean by "the normal symmetric/asymmetric algorithm", and where
    > do I store the key, and isn't that a less secure storing area than the
    > machine-store ?
    >
    > thx
    > /jim
    >
    >
    >>
    >> ---------------------------------------
    >> Dominick Baier - DevelopMentor
    >> http://www.leastprivilege.com
    >>
    >>> Hi there,
    >>>
    >>> I've been reading/doing this
    >>> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnet
    >>> sec/html/SecNetHT08.asp
    >>> and have a question about this:
    >>> ---------------
    >>> Note If you use DPAPI with the machine store, the encrypted string
    >>> is
    >>> specific to a given computer and therefore you must generate the
    >>> encrypted
    >>> data on every computer. Do not copy the encrypted data across
    >>> computers in a
    >>> farm or cluster.
    >>> ------------------
    >>> My scenario is: I have a web-app in .NET 2.0. The user enters some
    >>> data. I encrypt it. I store the encrypted data in a SQL server table.
    >>> I can then retrieve the data, decrypt it, and show it to the user.
    >>>
    >>> But what happens if my IIS-server crashes and have to be re-installed
    >>> ? What does "a given computer" mean ? Is my data forever encrypted and
    >>> can never be decrypted ?
    >>>
    >>> /jim
    >>>

    >>
    >>

    >
    >
    Bishoy George, Apr 5, 2006
    #5
  6. Dominick Baier [DevelopMentor]

    Jim Andersen Guest

    "Dominick Baier [DevelopMentor]" <>
    skrev i en meddelelse
    news:...
    > you are right - this is less secure - depending on your threat model at
    > least...
    >
    > DPAPI takes care of the key management for you - but it is bound to a
    > machine - so decide yourself what you want to do..


    Well, there's not much to decide.... I can't just cross my fingers and hope
    the server will never crash.
    Unless.... is there some way I can get the server to tell me it's key ?
    Write it down and lock it in a safe. And then when (not if...) the server
    dies, I can still decrypt all the data on the sql-server, and then encrypt
    it again on the new server ?

    Key mangement... is there a good link somewhere. Most examples on encryption
    I've seen just have the key in a session variable or it is passed to the
    function as an argument, or "this is outside the scope of this article".
    This is why I jumped at the DPAPI solution. No key management !

    thx
    /jim
    Jim Andersen, Apr 5, 2006
    #6
  7. Dominick Baier [DevelopMentor]

    Kaustav Guest

    Hi Jim,

    AFAIK, if IIS Server crashes, you still will be able to restore your data
    using DPAPI configured with a machine store. This is because the machine
    store probably uses OS specific data for generating the Key. So as long as
    the OS is okay, you shouldn't have a problem. Yet another option can be to
    use an User store instead of the machine store. You can have the user profile
    backed up and restore it on a different machine if required just in case it
    gets corrupt on the parent machine where it is being used.

    HTH.

    "Jim Andersen" wrote:

    > Hi there,
    >
    > I've been reading/doing this
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT08.asp
    >
    > and have a question about this:
    > ---------------
    > Note If you use DPAPI with the machine store, the encrypted string is
    > specific to a given computer and therefore you must generate the encrypted
    > data on every computer. Do not copy the encrypted data across computers in a
    > farm or cluster.
    > ------------------
    >
    > My scenario is: I have a web-app in .NET 2.0. The user enters some data. I
    > encrypt it. I store the encrypted data in a SQL server table. I can then
    > retrieve the data, decrypt it, and show it to the user.
    >
    > But what happens if my IIS-server crashes and have to be re-installed ? What
    > does "a given computer" mean ? Is my data forever encrypted and can never be
    > decrypted ?
    >
    > /jim
    >
    >
    >
    Kaustav, Apr 10, 2006
    #7
  8. Dominick Baier [DevelopMentor]

    Jim Andersen Guest

    "Kaustav" <> skrev i en meddelelse
    news:...
    > Hi Jim,
    >
    > So as long as
    > the OS is okay, you shouldn't have a problem.


    Yes.

    > Yet another option can be to
    > use an User store instead of the machine store. You can have the user
    > profile
    > backed up and restore it on a different machine if required just in case
    > it
    > gets corrupt on the parent machine where it is being used.


    You have any good links to managing user profiles ? Have never worked with
    that technology before.

    /jim
    Jim Andersen, Apr 11, 2006
    #8
  9. Dominick Baier [DevelopMentor]

    Kaustav Guest

    Hi Jim,

    check out
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/secnetht09.asp

    Kaustav.

    "Jim Andersen" wrote:

    >
    > "Kaustav" <> skrev i en meddelelse
    > news:...
    > > Hi Jim,
    > >
    > > So as long as
    > > the OS is okay, you shouldn't have a problem.

    >
    > Yes.
    >
    > > Yet another option can be to
    > > use an User store instead of the machine store. You can have the user
    > > profile
    > > backed up and restore it on a different machine if required just in case
    > > it
    > > gets corrupt on the parent machine where it is being used.

    >
    > You have any good links to managing user profiles ? Have never worked with
    > that technology before.
    >
    > /jim
    >
    >
    >
    Kaustav, Apr 11, 2006
    #9
  10. user store won't work with IIS - the profile is not loaded for worker process
    accounts.

    the rule of thumb is - if the data stays on the computer where it was encrypted
    - use DPAPI - otherwise you have to do your own key management (and use a
    different encryption algorithm).

    ---------------------------------------
    Dominick Baier - DevelopMentor
    http://www.leastprivilege.com

    > "Kaustav" <> skrev i en meddelelse
    > news:...
    >
    >> Hi Jim,
    >>
    >> So as long as
    >> the OS is okay, you shouldn't have a problem.

    > Yes.
    >
    >> Yet another option can be to
    >> use an User store instead of the machine store. You can have the user
    >> profile
    >> backed up and restore it on a different machine if required just in
    >> case
    >> it
    >> gets corrupt on the parent machine where it is being used.

    > You have any good links to managing user profiles ? Have never worked
    > with that technology before.
    >
    > /jim
    >
    Dominick Baier [DevelopMentor], Apr 11, 2006
    #10
  11. Dominick Baier [DevelopMentor]

    Kaustav Guest

    Yeah exactly. My earlier post has a link which precisely details out the
    steps to be followed in such a scenario.

    Here is the link :

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/secnetht09.asp

    Kaustav.


    "Dominick Baier [DevelopMentor]" wrote:

    > user store won't work with IIS - the profile is not loaded for worker process
    > accounts.
    >
    > the rule of thumb is - if the data stays on the computer where it was encrypted
    > - use DPAPI - otherwise you have to do your own key management (and use a
    > different encryption algorithm).
    >
    > ---------------------------------------
    > Dominick Baier - DevelopMentor
    > http://www.leastprivilege.com
    >
    > > "Kaustav" <> skrev i en meddelelse
    > > news:...
    > >
    > >> Hi Jim,
    > >>
    > >> So as long as
    > >> the OS is okay, you shouldn't have a problem.

    > > Yes.
    > >
    > >> Yet another option can be to
    > >> use an User store instead of the machine store. You can have the user
    > >> profile
    > >> backed up and restore it on a different machine if required just in
    > >> case
    > >> it
    > >> gets corrupt on the parent machine where it is being used.

    > > You have any good links to managing user profiles ? Have never worked
    > > with that technology before.
    > >
    > > /jim
    > >

    >
    >
    >
    Kaustav, Apr 11, 2006
    #11
  12. but still the key is stored locally on the machine...this is just to avoid
    the machine store

    to enable key "backup" you also need to use roaming profiles or backup the
    profile - so you could also backup the whole machine, right??...

    ---------------------------------------
    Dominick Baier - DevelopMentor
    http://www.leastprivilege.com

    > Yeah exactly. My earlier post has a link which precisely details out
    > the steps to be followed in such a scenario.
    >
    > Here is the link :
    >
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnet
    > sec/html/secnetht09.asp
    >
    > Kaustav.
    >
    > "Dominick Baier [DevelopMentor]" wrote:
    >
    >> user store won't work with IIS - the profile is not loaded for worker
    >> process accounts.
    >>
    >> the rule of thumb is - if the data stays on the computer where it was
    >> encrypted - use DPAPI - otherwise you have to do your own key
    >> management (and use a different encryption algorithm).
    >>
    >> ---------------------------------------
    >> Dominick Baier - DevelopMentor
    >> http://www.leastprivilege.com
    >>> "Kaustav" <> skrev i en meddelelse
    >>> news:...
    >>>
    >>>> Hi Jim,
    >>>>
    >>>> So as long as
    >>>> the OS is okay, you shouldn't have a problem.
    >>> Yes.
    >>>
    >>>> Yet another option can be to
    >>>> use an User store instead of the machine store. You can have the
    >>>> user
    >>>> profile
    >>>> backed up and restore it on a different machine if required just in
    >>>> case
    >>>> it
    >>>> gets corrupt on the parent machine where it is being used.
    >>> You have any good links to managing user profiles ? Have never
    >>> worked with that technology before.
    >>>
    >>> /jim
    >>>
    Dominick Baier [DevelopMentor], Apr 11, 2006
    #12
  13. Dominick Baier [DevelopMentor]

    Kaustav Guest

    Absolutely. The profile has to be roaming or backed up.

    "Dominick Baier [DevelopMentor]" wrote:

    > but still the key is stored locally on the machine...this is just to avoid
    > the machine store
    >
    > to enable key "backup" you also need to use roaming profiles or backup the
    > profile - so you could also backup the whole machine, right??...
    >
    > ---------------------------------------
    > Dominick Baier - DevelopMentor
    > http://www.leastprivilege.com
    >
    > > Yeah exactly. My earlier post has a link which precisely details out
    > > the steps to be followed in such a scenario.
    > >
    > > Here is the link :
    > >
    > > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnet
    > > sec/html/secnetht09.asp
    > >
    > > Kaustav.
    > >
    > > "Dominick Baier [DevelopMentor]" wrote:
    > >
    > >> user store won't work with IIS - the profile is not loaded for worker
    > >> process accounts.
    > >>
    > >> the rule of thumb is - if the data stays on the computer where it was
    > >> encrypted - use DPAPI - otherwise you have to do your own key
    > >> management (and use a different encryption algorithm).
    > >>
    > >> ---------------------------------------
    > >> Dominick Baier - DevelopMentor
    > >> http://www.leastprivilege.com
    > >>> "Kaustav" <> skrev i en meddelelse
    > >>> news:...
    > >>>
    > >>>> Hi Jim,
    > >>>>
    > >>>> So as long as
    > >>>> the OS is okay, you shouldn't have a problem.
    > >>> Yes.
    > >>>
    > >>>> Yet another option can be to
    > >>>> use an User store instead of the machine store. You can have the
    > >>>> user
    > >>>> profile
    > >>>> backed up and restore it on a different machine if required just in
    > >>>> case
    > >>>> it
    > >>>> gets corrupt on the parent machine where it is being used.
    > >>> You have any good links to managing user profiles ? Have never
    > >>> worked with that technology before.
    > >>>
    > >>> /jim
    > >>>

    >
    >
    >
    Kaustav, Apr 12, 2006
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BigLuzer
    Replies:
    1
    Views:
    1,412
    Cowboy \(Gregory A. Beamer\)
    Nov 21, 2006
  2. Kevin Cunningham

    DPAPI and connection string

    Kevin Cunningham, Oct 16, 2003, in forum: ASP .Net Security
    Replies:
    1
    Views:
    149
    Steve Jansen
    Oct 16, 2003
  3. afsheen

    error DPAPI

    afsheen, Oct 24, 2003, in forum: ASP .Net Security
    Replies:
    0
    Views:
    123
    afsheen
    Oct 24, 2003
  4. Shaun

    DPAPI Encryption Constraints & implementation

    Shaun, Jan 9, 2004, in forum: ASP .Net Security
    Replies:
    0
    Views:
    116
    Shaun
    Jan 9, 2004
  5. Ron Ifferte

    DPAPI in a Load Balanced Environment

    Ron Ifferte, Feb 26, 2004, in forum: ASP .Net Security
    Replies:
    2
    Views:
    152
    Alek Davis
    Feb 28, 2004
Loading...

Share This Page