像http://play.golang.org/p/fD7mx2k4Ycwindow rdp 密码加密 http://www.remkoweijnen.nl/blog/2007/10/18/how-rdp-passwords-are-encrypted/ package main import ( "fmt" "log" "syscall" "unsafe" ) const ( CRYPTPROTECT_UI_FORBIDDEN = 0x1 ) var ( dllcrypt32 = syscall.NewLazyDLL("Crypt32.dll") dllkernel32 = syscall.NewLazyDLL("Kernel32.dll") procEncryptData = dllcrypt32.NewProc("CryptProtectData") procDecryptData = dllcrypt32.NewProc("CryptUnprotectData") procLocalFree = dllkernel32.NewProc("LocalFree") ) type DATA_BLOB struct { cbData uint32 pbData *byte } func NewBlob(d []byte) *DATA_BLOB { if len(d) == 0 { return &DATA_BLOB{} } return &DATA_BLOB{ pbData: &d[0], cbData: uint32(len(d)), } } func (b *DATA_BLOB) ToByteArray() []byte { d := make([]byte, b.cbData) copy(d, (*[1 << 30]byte)(unsafe.Pointer(b.pbData))[:]) return d } func Encrypt(data []byte) ([]byte, error) { var outblob DATA_BLOB r, _, err := procEncryptData.Call(uintptr(unsafe.Pointer(NewBlob(data))), 0, 0, 0, 0, 0, uintptr(unsafe.Pointer(&outblob))) if r == 0 { return nil, err } defer procLocalFree.Call(uintptr(unsafe.Pointer(outblob.pbData))) return outblob.ToByteArray(), nil } func Decrypt(data []byte) ([]byte, error) { var outblob DATA_BLOB r, _, err := procDecryptData.Call(uintptr(unsafe.Pointer(NewBlob(data))), 0, 0, 0, 0, 0, uintptr(unsafe.Pointer(&outblob))) if r == 0 { return nil, err } defer procLocalFree.Call(uintptr(unsafe.Pointer(outblob.pbData))) return outblob.ToByteArray(), nil }
2 回答
紫衣仙女
TA贡献1839条经验 获得超15个赞
对上面不详细的答案做一个补充。根据@Fuqiang 的回答,只需UTF-16LE在加密之前将纯字符串的编码传输到,就可以了。所以它看起来像这样:
....
func convertToUTF16LittleEndianBytes(s string) []byte {
u := utf16.Encode([]rune(s))
b := make([]byte, 2*len(u))
for index, value := range u {
binary.LittleEndian.PutUint16(b[index*2:], value)
}
return b
}
func main() {
const secret = "MYpasswd"
s := convertToUTF16LittleEndianBytes(secret)
enc, err := Encrypt(s)
if err != nil {
log.Fatalf("Encrypt failed: %v", err)
}
...
}
并且解密后,您必须utf-16le在比较或使用它之前对解密的字符串进行解码,不要直接将utf-16le字符转换为字符串string(dec)。
- 2 回答
- 0 关注
- 343 浏览
添加回答
举报
0/150
提交
取消