http://stackoverflow.com/questions/32921117/inplement-c-sharp-descryptoserviceprovider-in-go-lang
Thanks in advance
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
my cursory review of RFC-2898 leads me to believe that pbkdf2.Key() with proper keylen and hash function can produce pbkdf1 keys.
package crypt
import (
"golang.org/x/crypto/pbkdf2"
"crypto/sha1"
)
var strDefaultKey string="65EA7902-C2A0-436E-8345-22260DC104C0";
var defaultIV = []byte{43, 56, 32, 32, 32, 23, 22, 32}
var salt = []byte{55, 156, 33, 56, 100, 43, 45, 43 }
func GetDefaultPass() string {
return strDefaultKey
}
func GetDefaultPassByte() []byte {
return []byte(strDefaultKey)
}
func GetKey(password string) []byte {
return pbkdf2.Key([]byte(password), salt, 100, 8, sha1.New)
}
package crypt_testsimport ("erpvietnam/fhc/core""testing""bytes""fmt")
func TestGetDefaultPassByte(t *testing.T) {ok := []byte{54, 53, 69, 65, 55, 57, 48, 50, 45, 67, 50, 65, 48, 45, 52, 51, 54, 69, 45, 56, 51, 52, 53, 45, 50, 50, 50, 54, 48, 68, 67, 49, 48, 52, 67, 48}result := crypt.GetDefaultPassByte()if(!bytes.Equal(ok, result)) {t.Error("crypt.GetDefaultPassByte failed ok:", ok, "vs result:", result)}}
func TestGetKey(t *testing.T) {ok := []byte {200, 144, 191, 9, 93, 72, 42, 233}result := crypt.GetKey(crypt.GetDefaultPass())if(!bytes.Equal(ok, result)) {t.Error("crypt.GetKey failed ok:", ok, "vs result:", result)}}
func DecryptString(encryptStr string, key string) string {
data, err := base64.StdEncoding.DecodeString(encryptStr)
if err != nil {
panic(err)
}
return DecryptByte(data, key)
}
func DecryptByte(encryptByte []byte, key string) string {
c, err := des.NewCipher([]byte {200, 144, 191, 9, 93, 72, 42, 233}) //des.NewCipher(GetKey(key))
if err != nil {
panic(err)
}
stream := cipher.NewCFBDecrypter(c, defaultIV)
// XORKeyStream can work in-place if the two arguments are the same.
stream.XORKeyStream(encryptByte, encryptByte)
return fmt.Sprintf("%s", encryptByte)
}
func TestDecryptString(t *testing.T) {
ok:= "admin123"
result := crypt.DecryptString("W1ajloISkm7aVgxpnoqDvvGdGT44f3ub", "")
if(result != ok) {
t.Error("DecryptString failed ok:", ok, " vs result: ", result);
}
}
=== result of test ====
crypt_test.go:39: DecryptString failed ok: admin123 vs result: s�!h�˫f[�b? ��% ^
I am newbie of golang. Please help me.
Thanks in advance
Hello. How to inplement C# DESCryptoServiceProvider in Go Lang? I have Decrypt in C# and want Encrypt in go lang.