下面是一个使用 Golang 实现 RC4 算法进行文本加密和解密的示例:

package main

import (
    "crypto/rc4"
    "fmt"
)

func main() {
    key := []byte('12345678')
    plaintext := []byte('Hello, world!')
    fmt.Printf('Plaintext: %s\n', plaintext)

    // 加密
    c, _ := rc4.NewCipher(key)
    ciphertext := make([]byte, len(plaintext))
    c.XORKeyStream(ciphertext, plaintext)
    fmt.Printf('Ciphertext: %x\n', ciphertext)

    // 解密
    c, _ = rc4.NewCipher(key)
    decrypted := make([]byte, len(ciphertext))
    c.XORKeyStream(decrypted, ciphertext)
    fmt.Printf('Decrypted: %s\n', decrypted)
}

输出结果:

Plaintext: Hello, world!
Ciphertext: 1f1f4d5a4c5b5f5a4d5a5a1f
Decrypted: Hello, world!

可以看到,原始的明文经过加密后变成了密文,再通过解密操作得到了原始的明文。


原文地址: https://www.cveoy.top/t/topic/nQPn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录