22 มิถุนายน 2565

Golang decrypt RC4 sample

 Sample Golang code below is the RC4 decrypt function that accept key and encrypted hex inputs.

Sample Golang Code

import (
"crypto/cipher"
"crypto/rc4"
"encoding/hex"
"strings"
"unicode/utf8"
)

func DecryptRC4(keyHex string, valueHex string) (string, error) {
keyBs, err := hex.DecodeString(keyHex)
if err != nil {
return "", err
}

valueBs, err := hex.DecodeString(valueHex)
if err != nil {
return "", err
}

c, err := rc4.NewCipher(keyBs)
if err != nil {
return "", err
}

c.XORKeyStream(valueBs, valueBs)
if utf8.Valid(valueBs) {
return bsToString(valueBs), nil
}

return "", nil

}

func bsToString(bs []byte) string {
str := string(bs)
str = strings.Replace(str, "\u0000", "", -1)
str = strings.Replace(str, "\x05", "", -1)
return str
}

Try to play and compare result with RC4 encrypt / decrypt online tool Link

Sample Input

got, gotErr := DecryptRC4("6d796b6579", "3b1fa50a0129d01faaa686f42969")
// raw key: "mykey"
// got:"my dummy value"
// gotErr: nil




ไม่มีความคิดเห็น:

แสดงความคิดเห็น

บทความยอดนิยม (ล่าสุด)

บทความยอดนิยม (1 ปีย้อนหลัง)