42

golang中crypto/rc4包

 5 years ago
source link: https://studygolang.com/articles/16794?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

rc4包实现了RC4加密算法,参见Bruce Schneier's Applied Cryptography。

type KeySizeError int

func (KeySizeError) Error() string

type Cipher struct{...}

func NewCipher(key []byte) (*Cipher, error)

NewCipher创建并返回一个新的Cipher。参数key时RC4密钥,至少1字节,最多256字节。

func (c *Cipher) Reset()

Reset方法会清空密钥数据,以便将其数据从程序内存中清除(以免被破解)

func (c *Cipher) XORKeyStream(dst, src []byte)

XORKeyStream方法将src的数据与密钥生成的伪随机流取XOR并写入dst。dst和src可指向同一内存地址;但如果指向不同则其底层内存不可重叠。

Bugs:RC4被广泛使用,但设计上的缺陷使它很少用于较新的协议中。

package main

import (
    "crypto/rc4"
    "fmt"
    "log"
)

func main() {
    c, err := rc4.NewCipher([]byte("dsadsad"))
    if err != nil {
        log.Fatalln(err)
    }
    src := []byte("asdsad")
    dst := make([]byte, len(src))
    c.XORKeyStream(dst, src)
    fmt.Println(string(src), string(dst))
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK