Google 検索の多くでここにたどり着いたことに気づいたので、おそらくあなたの適切な心を借りることができると思いました :)
私は、3 年次の学位論文の一環として、モバイル デバイス (およびログイン用の Web サイト) 用のワンタイム パスワード ジェネレーターに取り組んでいます。
org.bouncycastle.crypto.digests.MD5Digest ライブラリを使用して、(文字列ユーザー入力から) バイト配列を取得し、それを X 回ハッシュしています。これは、デイジー チェーン ハッシュ文字列またはランポート方式の暗号化とも呼ばれます。
私の問題は、文字列が一度ハッシュされると正しくハッシュされますが、新しいハッシュが再度ハッシュされると結果が正しくないということです。
以下のコードを参照してください。
private String generateHash(String OTP, int loopNum)
{
byte[] secretBytes = OTP.getBytes();
for (int x = 0; x < loopNum; x++)
{
byte[] tempStore = new byte[16];
tempStore = hash(secretBytes);
secretBytes = tempStore;
}
return convertToHex(secretBytes);
}
public byte[] hash(byte[] secretBytes)
{
org.bouncycastle.crypto.digests.MD5Digest digest = new org.bouncycastle.crypto.digests.MD5Digest();
digest.reset();
// Update MD5 digest with user secret in byte format
digest.update(secretBytes, 0, secretBytes.length);
// get length of digest to initialise new md5 byte array
int length = digest.getDigestSize();
// create md5 byte array using length
byte[] md5 = new byte[length];
// calculate MD5 hash, using md5 byte array, 0 for buffer offset
digest.doFinal(md5, 0);
return md5;
}
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
String Hex;
String formattedHex;
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] <<< 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a'+ (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
} while(two_halfs++ < 1);
}
Hex = buf.toString();
formattedHex = "\n" + Hex.substring(0, 4) + " " + Hex.substring(4, 8) + " " + Hex.substring(8, 12) + " "
+ Hex.substring(12, 16) + " " + Hex.substring(16, 20) + " " +Hex.substring(20, 24) + " "
+ Hex.substring(24, 28) + " " + Hex.substring(28, 32);
return formattedHex;
}
どちらかだと思います。
- ダイジェストが正しいバイト配列を返さない
- Hex コンバーターはこれを誤って変換します
次の MD5 出力を持つ A のシークレットを使用してテストしています。
- 7fc56270e7a70fa81a5935b72eacbe29
- 8f28f2e7231860115d2a8cacba019dbe (これは 4cbd6d53280de25e04712c7434a70642 である必要があります)
事前にご協力いただきありがとうございます:)
ps PHP md5に対してチェックしていますが、これも問題になる可能性がありますか?