You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
609 B
26 lines
609 B
package TestUnit
|
|
|
|
import (
|
|
"crypto/rc4"
|
|
"log"
|
|
"testing"
|
|
)
|
|
|
|
func TestRC4(t *testing.T) {
|
|
rc4Key := []byte("topic")
|
|
// 要加密的源数据
|
|
str := []byte("this is test RC4")
|
|
// 加密操作
|
|
dest1 := make([]byte, len(str))
|
|
rc4.NewCipher(rc4Key)
|
|
cipher1, _ := rc4.NewCipher(rc4Key)
|
|
cipher1.XORKeyStream(dest1, str)
|
|
log.Printf("方法1加密后:%s \n", dest1)
|
|
|
|
// 解密操作
|
|
dest2 := make([]byte, len(dest1))
|
|
cipher2, _ := rc4.NewCipher(rc4Key) // 切记:这里不能重用cipher1,必须重新生成新的
|
|
cipher2.XORKeyStream(dest2, dest1)
|
|
log.Printf("方法1解密后:%s \n\n", dest2)
|
|
|
|
}
|
|
|