2015-12-07 25 views
12

Sono nuovo alla tecnologia JWT e ne sto leggendo molto a riguardo.JWT crittografare informazioni sul carico utile

So JWT ha 3 parti:

  1. INTESTAZIONE: ALGORITMO & GETTONE TIPO
  2. Carico utile: DATI
  3. FIRMA da verificare CON IL SEGRETO CHIAVE

E 'possibile crittografare le informazioni sul payload? Voglio dire, diciamo per esempio ho questa informazione payload nel mio token:

{ 
"iss": "joe", 
"exp": "1300819380", 
"data": { 
    "id": "12", 
    "userName": "PH", 
    "qntRed": "7", 
    "qntGrad": { 
     "1": "800", 
     "2": "858", 
     "3": "950", 
     "4": "745", 
     "5": "981" 
    } 
} 

E diciamo "qntGrad" è informazioni sensibili. È possibile crittografarlo anche con la chiave segreta? È ancora un token JWT?

risposta

10

Infatti non v'è solo firmato JWT, ma diverse tecnologie descritto da RFC:

Nel tuo caso, leggere la RFC7516 (JWE). Questi JWE hanno 5 parti:

  • Header Protetta
  • chiave crittografata
  • vettore di inizializzazione
  • testo cifrato
  • autenticazione Tag

a seconda della piattaforma, è possibile trovare una libreria che ti aiuterà a creare tale JWT crittografato. Per quanto riguarda PHP, sto scrivendo a library che è già in grado di caricare e creare questi jose.

+0

Ciao, @ florent-morselli, leggo su RFC7516! Si adatta davvero alle mie esigenze. E a proposito, sto usando 'PHP' nel mio backend. Grazie per l'aiuto! –

+1

Prego. Lasciami ora se decidi di usare la mia biblioteca. Poiché la documentazione non è stata ancora scritta, potresti aver bisogno di aiuto. –

-1

Di seguito è un metodo molto semplice ed efficace per la crittografia utilizzando AES. Nota che dovrai procurarti la tua chiave (link incluso nei commenti).

Si noti che quando si cripta, verrà impostata una IV per ogni chiamata di crittografia. Ne avrai bisogno per decifrare.

public class CustomEncryption 
{ 
    public static string Encrypt256(string text, byte[] AesKey256, out byte[] iv) 
    { 
     // AesCryptoServiceProvider 
     AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); 
     aes.BlockSize = 128; 
     aes.KeySize = 256; 
     aes.Key = aesKey256(); 
     aes.Mode = CipherMode.CBC; 
     aes.Padding = PaddingMode.PKCS7; 
     iv = aes.IV; 

     byte[] src = Encoding.Unicode.GetBytes(text); 

     using (ICryptoTransform encrypt = aes.CreateEncryptor()) 
     { 
      byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length); 

      return Convert.ToBase64String(dest); 
     } 
    } 

    public static string Decrypt256(string text, byte[] AesKey256, byte[] iv) 
    { 
     AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); 
     aes.BlockSize = 128; 
     aes.KeySize = 256; 
     aes.IV = iv; 
     aes.Key = aesKey256(); 
     aes.Mode = CipherMode.CBC; 
     aes.Padding = PaddingMode.PKCS7; 

     byte[] src = System.Convert.FromBase64String(text); 

     using (ICryptoTransform decrypt = aes.CreateDecryptor()) 
     { 
      byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length); 
      return Encoding.Unicode.GetString(dest); 
     } 
    } 

    private static byte[] aesKey256() 
    { 
     //you will need to get your own aesKey 
     //for testing you can generate one from 
     //https://asecuritysite.com/encryption/keygen 

     return new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5 }; 
    } 
} 

}

0

Non cifrare il token significa che altri servizi esterni in grado di leggere e verificare che il token è in realtà autentica senza avere accesso alla propria chiave privata.(Avrebbero solo bisogno della chiave pubblica)

Problemi correlati