Why do Rfc2898DeriveBytes in C# and pbkdf2 in go lang generate different keys?
my C# code
using System; using System.Security.Cryptography; using System.Text; public class Test { private static byte[] passBytes = new byte[] {164,176,124,62,244,154,226,211,177,90,202,180,12,142,25,225}; private static byte[] saltBytes = new byte[] {173,205,190,172,239,190,242,63,219,205,173,196,218,171,142,214}; public static byte[] GetKey() { var key = new Rfc2898DeriveBytes(Encoding.UTF8.GetString(passBytes, 0, 16), saltBytes).GetBytes(16); return key; } public static void Main() { System.Console.WriteLine(Convert.ToBase64String(GetKey())); } } output: 77U85CphtSEwPP9a2T/jaQ==
golang code
package main import ( b64 "encoding/base64" "golang.org/x/crypto/pbkdf2" "crypto/sha1" ) var ( pass[]byte = []byte{164,176,124,62,244,154,226,211,177,90,202,180,12,142,25,225} salt[]byte = []byte{173,205,190,172,239,190,242,63,219,205,173,196,218,171,142,214} ) func getKey() (key[]byte){ key = pbkdf2.Key(pass,salt,1000,16,sha1.New) return } func main() { print(b64.StdEncoding.EncodeToString(getKey())) } output: hnuuu+he4aF7vAzA8rfQtw==
Is there something different i must do?
UTF8onpassBytesseem suspect because thepassBytesmay not be a valid UTF-8 string.Encoding.UTF8.GetString(passBytes, 0, 16)not the right encoding supposed to be used onpassBytes?