Skip to content

Instantly share code, notes, and snippets.

@seraphy
Created July 8, 2012 17:47
Show Gist options
  • Save seraphy/3072028 to your computer and use it in GitHub Desktop.
Save seraphy/3072028 to your computer and use it in GitHub Desktop.
C#とJAVA間で同じキーとなるパスワード ベースのキー派生機能 (PBKDF2) を実装する方法
public static void main(String[] args) throws Exception {
char[] password = "Hello, World!!".toCharArray();
byte[] salt = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(password, salt, 65536, 128); // キーは128Bit
SecretKey tmp = factory.generateSecret(spec);
byte[] digest = tmp.getEncoded();
for (byte d : digest) {
System.out.print(String.format("%02x", d));
}
System.out.println();
// 2adfe63d125ae48eb056893dd18bc643
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace Rfc2898DeriveBytesSample
{
class Program
{
static void Main(string[] args)
{
var password = "Hello, World!!";
byte[] salt = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
var kf = new Rfc2898DeriveBytes(password, salt, 65536);
byte[] key = kf.GetBytes(128 / 8);
var buf = new StringBuilder();
foreach (byte b in key)
{
buf.Append(b.ToString("x2"));
}
System.Diagnostics.Debug.WriteLine(buf);
// 2adfe63d125ae48eb056893dd18bc643
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment