感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

[Golang] RC4加解密

JavaPub
关注TA
已关注
手记 59
粉丝 16
获赞 37

@[toc]

前言

拿去直接用,直接 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。


本文由博客一文多发平台 OpenWrite 发布!

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP