
问题在于,
PasswordDeriveBytes仅为前20个字节定义了-在这种情况下,它是PBKDF1( 而不是2
,因为您当前在Java代码中正在使用)。
getBytes多次调用也可能会更改结果。一个或多个调用
getBytes或超过20个字节的算法是Microsoft专有的,似乎在任何地方都没有描述。在Mono中,由于它可能不安全,甚至被描述为非修复。
我强烈建议使用RFC2898DeriveBytes那
确实 实现PBKDF2。注意仅将其用于ASCII输入,否则可能与Java实现不兼容。
唯一的其他选择是弄清楚Microsoft
PasswordDeriveBytes对PBKDF1
的专有扩展(它仅定义最大20个字节的哈希大小的输出)。我在下面重新实现了Mono的版本。
向Microsoft反复请求更新此功能的API描述没有产生任何结果。如果结果不同,您可能需要阅读此错误报告。
这是专有的Microsoft扩展。基本上,它首先计算PBKDF-1,但不包括最后的哈希迭代,称为HX。对于前20个字节,它仅执行另一个哈希,因此它符合PBKDF1。下一个哈希值覆盖从1开始的计数器的ASCII表示形式(因此首先将其转换为
"1",然后转换为
0x31),然后是HX的字节。
以下是从Mono代码进行的一种简单而直接的转换:
public class PasswordDeriveBytes { private final MessageDigest hash; private final byte[] initial; private final int iterations; private byte[] output; private int hashnumber = 0; private int position = 0; public PasswordDeriveBytes(String password, byte[] salt) { try { this.hash = MessageDigest.getInstance("SHA-1"); this.initial = new byte[hash.getDigestLength()]; this.hash.update(password.getBytes(UTF_8)); this.hash.update(salt); this.hash.digest(this.initial, 0, this.initial.length); this.iterations = 100; } catch (NoSuchAlgorithmException | DigestException e) { throw new IllegalStateException(e); } } public byte[] getBytes(int cb) { if (cb < 1) throw new IndexOutOfBoundsException("cb"); byte[] result = new byte[cb]; int cpos = 0; // the initial hash (in reset) + at least one iteration int iter = Math.max(1, iterations - 1); // start with the PKCS5 key if (output == null) { // calculate the PKCS5 key output = initial; // generate new key material for (int i = 0; i < iter - 1; i++) output = hash.digest(output); } while (cpos < cb) { byte[] output2 = null; if (hashnumber == 0) { // last iteration on output output2 = hash.digest(output); } else if (hashnumber < 1000) { String n = String.valueOf(hashnumber); output2 = new byte[output.length + n.length()]; for (int j = 0; j < n.length(); j++) output2[j] = (byte) (n.charAt(j)); System.arraycopy(output, 0, output2, n.length(), output.length); // don't update output output2 = hash.digest(output2); } else { throw new SecurityException(); } int rem = output2.length - position; int l = Math.min(cb - cpos, rem); System.arraycopy(output2, position, result, cpos, l); cpos += l; position += l; while (position >= output2.length) { position -= output2.length; hashnumber++; } } return result; }}或者,更优化,更易读,只需要在两次调用之间更改输出缓冲区和位置即可:
public class PasswordDeriveBytes { private final MessageDigest hash; private final byte[] firstToLastDigest; private final byte[] outputBuffer; private int position = 0; public PasswordDeriveBytes(String password, byte[] salt) { try { this.hash = MessageDigest.getInstance("SHA-1"); this.hash.update(password.getBytes(UTF_8)); this.hash.update(salt); this.firstToLastDigest = this.hash.digest(); final int iterations = 100; for (int i = 1; i < iterations - 1; i++) { hash.update(firstToLastDigest); hash.digest(firstToLastDigest, 0, firstToLastDigest.length); } this.outputBuffer = hash.digest(firstToLastDigest); } catch (NoSuchAlgorithmException | DigestException e) { throw new IllegalStateException("SHA-1 digest should always be available", e); } } public byte[] getBytes(int requested) { if (requested < 1) { throw new IllegalArgumentException( "You should at least request 1 byte"); } byte[] result = new byte[requested]; int generated = 0; try { while (generated < requested) { final int outputOffset = position % outputBuffer.length; if (outputOffset == 0 && position != 0) { final String counter = String.valueOf(position / outputBuffer.length); hash.update(counter.getBytes(US_ASCII)); hash.update(firstToLastDigest); hash.digest(outputBuffer, 0, outputBuffer.length); } final int left = outputBuffer.length - outputOffset; final int required = requested - generated; final int copy = Math.min(left, required); System.arraycopy(outputBuffer, outputOffset, result, generated, copy); generated += copy; position += copy; } } catch (final DigestException e) { throw new IllegalStateException(e); } return result; }}实际上,安全性还不是那么糟糕,因为字节之间通过摘要彼此分开。因此,密钥拉伸相对还可以。请注意,那里有Microsoft
PasswordDeriveBytes实现,其中包含一个错误和重复的字节(请参见上面的错误报告)。这里不转载。
用法:
private static final String PASSWORD = "46dkaKLKKJLjdkdk;akdjafj";private static final byte[] SALT = { 0x26, 0x19, (byte) 0x81, 0x4E, (byte) 0xA0, 0x6D, (byte) 0x95, 0x34 };public static void main(String[] args) throws Exception { final Cipher desEDE = Cipher.getInstance("DESede/CBC/PKCS5Padding"); final PasswordDeriveBytes myPass = new PasswordDeriveBytes(PASSWORD, SALT); final SecretKeyFactory kf = SecretKeyFactory.getInstance("DESede"); final byte[] key = myPass.getBytes(192 / Byte.SIZE); final SecretKey desEDEKey = kf.generateSecret(new DESedeKeySpec(key)); final byte[] iv = myPass.getBytes(desEDE.getBlockSize()); desEDE.init(Cipher.ENCRYPT_MODE, desEDEKey, new IvParameterSpec(iv)); final byte[] ct = desEDE.doFinal("owlstead".getBytes(US_ASCII));}有关Java实现的旁注:
- 迭代次数太低,请检查当前日期需要哪种迭代次数
- 密钥大小不正确,应创建3 * 64 = 192位而不是196位的密钥
- 3DES越来越老了,请改用AES
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)