前言
拿去直接用,直接 Ctrl+C/V
。
代码
package utils import ( "crypto/rc4" "encoding/base64" ) // 加密 func EncryptionRc4(k, query string) string { key := []byte(k) plaintext := []byte(query) // encryption ciphertext := make([]byte, len(plaintext)) cipher1, _ := rc4.NewCipher(key) cipher1.XORKeyStream(ciphertext, plaintext) return base64.StdEncoding.EncodeToString(ciphertext) } // 解密 func DecryptionRc4(k, query string) string { param, err := base64.StdEncoding.DecodeString(query) if err != nil { return "" } key := []byte(k) ciphertext := param plaintextDec := make([]byte, len(ciphertext)) cipher2, _ := rc4.NewCipher(key) cipher2.XORKeyStream(plaintextDec, ciphertext) return string(plaintextDec) }
func TestRc4(t *testing.T) { // 密钥 & 待加密字符串 rc4 := utils.EncryptionRc4("javaPub_api_key", "我要被加密啦,好害怕!!!") fmt.Println("这是加密后的👇:") fmt.Println(rc4) decryptionRc4 := utils.DecryptionRc4("javaPub_api_key", rc4) fmt.Println("这是解密后的👇:") fmt.Println(decryptionRc4) }
祝:工作顺利,得心应手。我是 JavaPub。