Menu

Usage of salt for the AES algorithm

Help
2009-05-26
2012-12-11
  • Trond Andersen

    Trond Andersen - 2009-05-26

    One question about the salt usage in the AES algorithm. If I want to control the salt in encryption/decryption with AES_CBC, do I only need to prefix the text to be encrypted with the given salt?

    In other words can I do something like this:

    <code>
    plainText = "Salted__mysecretsalt" + plainText
    var aes = new pidCrypt.AES.CBC();
    return aes.encryptText(plainText, password, {nBits:256});
    </code>

     
    • Pidder

      Pidder - 2009-05-26

      You must specify the salt within the options. If no salt specified a random salt is created.
      The encrypt method add the prefix  to the crypted text  ("Salted__" + salt + cryptedText).

      In other words you should do something like this:

      <code>
      var aes = new pidCrypt.AES.CBC();
      return aes.encryptText(plainText, password, {{nBits:256, salt:"mysecretsalt"});
      </code>

      The salt is combined with the password and this combination goes to three md5 rounds to create the aes-cbc key (like in openssl cbc mode). There is no need for a secret salt btw.

       
    • Trond Andersen

      Trond Andersen - 2009-05-29

      Is it possible to say that salt isn't going to be used at all? It other words avoid the random salt?

       
      • Pidder

        Pidder - 2009-05-29

        If you want to avoid the use of any salt at all you can call

          var aes = new pidCrypt.AES.CBC();
          aes.initByValues(plain, key, iv);
          aes.encryptRaw(); //does not make *any* transformations to in and output

         
        • Trond Andersen

          Trond Andersen - 2009-06-02

          When I look at the aes_cbc.js file, there is a method called decryptRaw and encryptRaw which both take a byte array as a parameter, but the example shown above calls the raw method without any parameter. The initByValues first parameter seems to be the text to decrypt/encrypt and this is a string. What is the proper usage of this library? Should I provide the text as byte array and just send null to the init method or should the text to be decrypted be sent as text to the init method?

           
          • Pidder

            Pidder - 2009-06-02

            There are different level of usage. A non technical user who simply wants to encrypt and decrypt text with a given password will use the highest level

            var cryptedText = aes.encryptText('text to encrypt','password');

            to encrypt and

            var plainText = aes.decryptText('U2FsdGVkX1/YdMuwX73G3pzXn9EVezfY+22kXWrE3/3u5noyBcz2SysMSa6Izrht','password');

            to decrypt.

            A technical expert can take over control and can manipulate many parameters of the encryption/decryption process. E.g. he can call the init method with the option clear=false once and can encrypt many different byte arrays with the same parameters :

            var toEncrypt = [];
            var secrets = [];

            toEncrypt[toEncrypt.length] = [116,101,120,116,49,10,10,10,10,10,10,10,10,10,10,10];
            toEncrypt[toEncrypt.length] = [116,101,120,116,50,10,10,10,10,10,10,10,10,10,10,10];
            toEncrypt[toEncrypt.length] = [116,101,120,116,51,10,10,10,10,10,10,10,10,10,10,10];
            toEncrypt[toEncrypt.length] = [116,101,120,116,52,10,10,10,10,10,10,10,10,10,10,10];

            //this init function has no text input, only password and options!
            aes.init('password',{salt:'mysecretsalt',clear: false});

            for(var i=0;i<toEncrypt.length;i++)
              secrets[i] = aes.enryptRaw(toEncrypt[i]);

            The enryptRaw(byteArray) method will use params,encryptIn if no parameter is given. params,encryptIn is the string input converted to a byte array by the init functions. There is no salt salt added to the result. The user must store the parameters by himself or he will never decrypt the secrets.

            The encrypt(String) method instead uses params.dataIn or a given string parameter. This mehod adds "Salted__" + salt and do the base64 encoding.

            So the anwser to your questions is: "It depends". 

             
    • Trond Andersen

      Trond Andersen - 2009-05-29

      On a related note regarding this. I'm using the CBC encrypt and decrypt methods, but in my case the Key isn't really a string - it is a byte array which could contain "invalid" string characters. Does this implementation support sending in a byte array as a key?

       
      • Pidder

        Pidder - 2009-05-29

        The encrypt method expects the key to be a string of length nBits/8 in its hexadecimal representation. You also need an initialization vector (iv) of length 16 as hex string too.
        You can convert your byte array with pidcrypt's utility methods byteArray2String(array).convertToHex()

        To use your own key and iv you have to call for encryption
                aes.initByValues(plain, key, iv);
                crypted = aes.encrypt();

        and for decryption
                aes.initByValues(crypted, key, iv);
                result = aes.decrypt();

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.