c# java algorithm encryption rfc2898

Java equivalente de C#''s Rfc2898DerivedBytes



algorithm encryption (4)

Encontré esta implementación mediante una búsqueda en Google, pero nunca la he usado.

Una implementación gratuita de Java de RFC 2898 / PKCS # 5 PBKDF2

Parece que no hay disponible una implementación Java pequeña y de libre disponibilidad de RFC 2898 / PKCS # 5. Pequeño como en algunos pocos archivos fuente, con compilación trivial y sin dependencias, libre como en LGPL.

Dada la disponibilidad de HMacSHA1 en el proveedor criptográfico SUN JCE estándar, dicha implementación es bastante simple y se puede derivar de la descripción RFC literalmente. Mi código es una implementación de sala limpia con solo el RFC como base.

Me preguntaba si alguien ha tratado de hacer un equivalente de

Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(secret, saltValueBytes); byte[] secretKey = key.GetBytes(16);

en Java. Donde secreto es una cadena (contraseña), y saltValueBytes es, bueno, una sal en matriz de bytes.

He intentado cosas, pero parece que no puedo entenderlo.


El código de Cheeso ligeramente mejorado para trabajar con HMacSHA256 o HMacSHA512 al agregar deriveKey sobrecargado (). Con este cambio, el código se ejecutó con los vectores de prueba PKDF2-HMAC-SHA512 de
PHP Crypt lib que dio como resultado 6 fallas de 100 casos de prueba.

// PBKDF2.java // ------------------------------------------------------------------ // // RFC2898 PBKDF2 in Java. The RFC2898 defines a standard algorithm for // deriving key bytes from a text password. This is also called // "PBKDF2", for Password-based key derivation function #2. // // There''s no RFC2898-compliant PBKDF2 function in the JRE, as far as I // know, but it is available in many J2EE runtimes, including those from // JBoss, IBM, and Oracle. // // It''s fairly simple to implement, so here it is. // // Author: Admin // built on host: DINOCH-2 // Created Sun Aug 09 01:06:57 2009 // // last saved: // Time-stamp: <2009-August-09 11:11:47> // ------------------------------------------------------------------ // // code from Matthias Gartner // // ------------------------------------------------------------------ package cheeso.examples; import java.security.NoSuchAlgorithmException; import java.security.InvalidKeyException; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; public class PBKDF2 { public static byte[] deriveKey(byte[] password, byte[] salt, int iterationCount, int dkLen) throws NoSuchAlgorithmException, InvalidKeyException { return deriveKey("HmacSHA1", password, salt, iterationCount, dkLen); } public static byte[] deriveKey(String hmacAlgo, byte[] password, byte[] salt, int iterationCount, int dkLen) throws NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec keyspec = new SecretKeySpec(password, hmacAlgo); Mac prf = Mac.getInstance(hmacAlgo); prf.init( keyspec ); // Note: hLen, dkLen, l, r, T, F, etc. are horrible names for // variables and functions in this day and age, but they // reflect the terse symbols used in RFC 2898 to describe // the PBKDF2 algorithm, which improves validation of the // code vs. the RFC. // // dklen is expressed in bytes. (16 for a 128-bit key, 32 for 256) int hLen = prf.getMacLength(); // 20 for SHA1 int l = Math.max( dkLen, hLen); // 1 for 128bit (16-byte) keys int r = dkLen - (l-1)*hLen; // 16 for 128bit (16-byte) keys byte T[] = new byte[l * hLen]; int ti_offset = 0; for (int i = 1; i <= l; i++) { F( T, ti_offset, prf, salt, iterationCount, i ); ti_offset += hLen; } if (r < hLen) { // Incomplete last block byte DK[] = new byte[dkLen]; System.arraycopy(T, 0, DK, 0, dkLen); return DK; } return T; } private static void F( byte[] dest, int offset, Mac prf, byte[] S, int c, int blockIndex ) { final int hLen = prf.getMacLength(); byte U_r[] = new byte[ hLen ]; // U0 = S || INT (i); byte U_i[] = new byte[S.length + 4]; System.arraycopy( S, 0, U_i, 0, S.length ); INT( U_i, S.length, blockIndex ); for( int i = 0; i < c; i++ ) { U_i = prf.doFinal( U_i ); xor( U_r, U_i ); } System.arraycopy( U_r, 0, dest, offset, hLen ); } private static void xor( byte[] dest, byte[] src ) { for( int i = 0; i < dest.length; i++ ) { dest[i] ^= src[i]; } } private static void INT( byte[] dest, int offset, int i ) { dest[offset + 0] = (byte) (i / (256 * 256 * 256)); dest[offset + 1] = (byte) (i / (256 * 256)); dest[offset + 2] = (byte) (i / (256)); dest[offset + 3] = (byte) (i); } // ctor private PBKDF2 () {} }


Esta funciona para mí. Todavía no estoy seguro de que una implementación estándar de un PBKDF2 compatible con RFC2898 no esté presente en el JRE. Creo que debo estar mirando en los lugares equivocados. El nombre confusión (RFC2898 PKCS5 PBKDF2) no ayuda.

// PBKDF2.java // ------------------------------------------------------------------ // // RFC2898 PBKDF2 in Java. The RFC2898 defines a standard algorithm for // deriving key bytes from a text password. This is also called // "PBKDF2", for Password-based key derivation function #2. // // There''s no RFC2898-compliant PBKDF2 function in the JRE, as far as I // know, but it is available in many J2EE runtimes, including those from // JBoss, IBM, and Oracle. // // It''s fairly simple to implement, so here it is. // // Author: Admin // built on host: DINOCH-2 // Created Sun Aug 09 01:06:57 2009 // // last saved: // Time-stamp: <2009-August-09 11:11:47> // ------------------------------------------------------------------ // // code from Matthias Gartner // // ------------------------------------------------------------------ package cheeso.examples; import java.security.NoSuchAlgorithmException; import java.security.InvalidKeyException; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; public class PBKDF2 { public static byte[] deriveKey( byte[] password, byte[] salt, int iterationCount, int dkLen ) throws java.security.NoSuchAlgorithmException, java.security.InvalidKeyException { SecretKeySpec keyspec = new SecretKeySpec( password, "HmacSHA1" ); Mac prf = Mac.getInstance( "HmacSHA1" ); prf.init( keyspec ); // Note: hLen, dkLen, l, r, T, F, etc. are horrible names for // variables and functions in this day and age, but they // reflect the terse symbols used in RFC 2898 to describe // the PBKDF2 algorithm, which improves validation of the // code vs. the RFC. // // hLen denotes the length in octets of the pseudorandom function output // dklen the length in octets (bytes) of the derived key. int hLen = prf.getMacLength(); // 20 for SHA1 int l = Math.ceil( dkLen/hLen ); // 1 for 128bit (16-byte) keys int r = dkLen - (l-1)*hLen; // 16 for 128bit (16-byte) keys byte T[] = new byte[l * hLen]; int ti_offset = 0; for (int i = 1; i <= l; i++) { F( T, ti_offset, prf, salt, iterationCount, i ); ti_offset += hLen; } if (r < hLen) { // Incomplete last block byte DK[] = new byte[dkLen]; System.arraycopy(T, 0, DK, 0, dkLen); return DK; } return T; } private static void F( byte[] dest, int offset, Mac prf, byte[] S, int c, int blockIndex ) { final int hLen = prf.getMacLength(); byte U_r[] = new byte[ hLen ]; // U0 = S || INT (i); byte U_i[] = new byte[S.length + 4]; System.arraycopy( S, 0, U_i, 0, S.length ); INT( U_i, S.length, blockIndex ); for( int i = 0; i < c; i++ ) { U_i = prf.doFinal( U_i ); xor( U_r, U_i ); } System.arraycopy( U_r, 0, dest, offset, hLen ); } private static void xor( byte[] dest, byte[] src ) { for( int i = 0; i < dest.length; i++ ) { dest[i] ^= src[i]; } } private static void INT( byte[] dest, int offset, int i ) { dest[offset + 0] = (byte) (i / (256 * 256 * 256)); dest[offset + 1] = (byte) (i / (256 * 256)); dest[offset + 2] = (byte) (i / (256)); dest[offset + 3] = (byte) (i); } // ctor private PBKDF2 () {} }


Sé que es tarde para el juego, pero Java 6 y posteriores tienen una implementación PBKDF2 incorporada.

int dkLen = 64; int rounds = 1000; PBEKeySpec keySpec = new PBEKeySpec("Some password".toCharArray(), "SomeSalt".getBytes(), rounds, dkLen * 8); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); byte[] out = factory.generateSecret(keySpec).getEncoded();

La lista de mejoras de seguridad de Java 6 compara la PKCS n. ° 5 y, a través de mis propias pruebas (superficiales), parece producir claves PBKDF2 correctas.