Exemple #1
0
func new_captchaserv() *t_captchaserv {
	var (
		e error
	)
	defer func() {
		if e != nil {
			panic(e)
		}
	}()

	t := new(t_captchaserv)
	t.counter = counter{limit: lendb}
	t.iv[0].val = make([]byte, 16)
	t.iv[1].val = make([]byte, 16)
	t.iv[0].id = byte(0)
	//t.iv[1].id = byte(0)

	key := make([]byte, cipherlen)
	io.ReadFull(rand.Reader, key)
	t.bcipher, e = i_cipher.NewCipher(key)

	t.iochan = make(chan *rezult_json, 1000)

	go t.chang_state()

	go t.gofabric()
	go t.gofabric()

	return t
}
Exemple #2
0
func decrypt(blob []byte) {
	c, err := twofish.NewCipher(key)
	if err != nil {
		panic(err)
	}
	s := cipher.NewCFBDecrypter(c, iv)
	s.XORKeyStream(blob, blob)
}
Exemple #3
0
func (g *Generator) generateBlocks(k int) (r []byte, err error) {
	r = make([]byte, 0, k*twofish.BlockSize)

	c, err := twofish.NewCipher(g.key[:])
	if err != nil {
		return
	}

	for i := 0; i < k; i++ {
		block := make([]byte, twofish.BlockSize)
		c.Encrypt(block, g.ctr[:])
		r = append(r, block...)
		incCounter(g.ctr)
	}
	return
}
Exemple #4
0
func encrypt(key, in []byte) ([]byte, error) {
	if len(key) != twofishKeyLen {
		return nil, errors.New("secretbox: invalid key size")
	}
	c, err := twofish.NewCipher(key)
	if err != nil {
		return nil, err
	}

	ctext := make([]byte, len(in)+twofish.BlockSize)
	iv, err := generateIV()
	if err != nil {
		return nil, err
	}
	copy(ctext, iv)

	ctr := cipher.NewCTR(c, iv)
	ctr.XORKeyStream(ctext[twofish.BlockSize:], in)
	return ctext, nil
}
Exemple #5
0
func decrypt(key, ctext []byte) ([]byte, error) {
	if len(key) != twofishKeyLen {
		return nil, errors.New("secretbox: invalid key size")
	}
	if len(ctext) < twofish.BlockSize {
		return nil, errors.New("secretbox: invalid ciphertext")
	}

	iv := ctext[:twofish.BlockSize]
	ct := ctext[twofish.BlockSize:]

	c, err := twofish.NewCipher(key)
	if err != nil {
		return nil, err
	}

	ptext := make([]byte, len(ct))
	ctr := cipher.NewCTR(c, iv)
	ctr.XORKeyStream(ptext, ct)
	return ptext, nil
}
Exemple #6
0
func NewCipherCfg(algo string, key []byte, iv []byte) *CipherCfg {
	cfg := new(CipherCfg)
	cfg.Key = key
	cfg.IV = iv

	switch algo {
	case "twofish":
		block, err := twofish.NewCipher(key)
		if err != nil {
			panic("Twofish: " + err.Error())
		}
		cfg.Algo = block
	case "aes":
		block, err := aes.NewCipher(key)
		if err != nil {
			panic("AES: " + err.Error())
		}
		cfg.Algo = block
	default:
		panic("Bad cipher name: " + algo)
	}

	return cfg
}