本文整理汇总了Golang中crypto/cipher.NewOFB函数的典型用法代码示例。如果您正苦于以下问题:Golang NewOFB函数的具体用法?Golang NewOFB怎么用?Golang NewOFB使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewOFB函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestOFB
func TestOFB(t *testing.T) {
for _, tt := range ofbTests {
test := tt.name
c, err := aes.NewCipher(tt.key)
if err != nil {
t.Errorf("%s: NewCipher(%d bytes) = %s", test, len(tt.key), err)
continue
}
for j := 0; j <= 5; j += 5 {
plaintext := tt.in[0 : len(tt.in)-j]
ofb := cipher.NewOFB(c, tt.iv)
ciphertext := make([]byte, len(plaintext))
ofb.XORKeyStream(ciphertext, plaintext)
if !bytes.Equal(ciphertext, tt.out[:len(plaintext)]) {
t.Errorf("%s/%d: encrypting\ninput % x\nhave % x\nwant % x", test, len(plaintext), plaintext, ciphertext, tt.out)
}
}
for j := 0; j <= 5; j += 5 {
ciphertext := tt.out[0 : len(tt.in)-j]
ofb := cipher.NewOFB(c, tt.iv)
plaintext := make([]byte, len(ciphertext))
ofb.XORKeyStream(plaintext, ciphertext)
if !bytes.Equal(plaintext, tt.in[:len(ciphertext)]) {
t.Errorf("%s/%d: decrypting\nhave % x\nwant % x", test, len(ciphertext), plaintext, tt.in)
}
}
if t.Failed() {
break
}
}
}
示例2: CreateExchangedCipher
func CreateExchangedCipher(peerPub, priv []byte) (Cipher, Cipher, error) {
x, y := elliptic.Unmarshal(curve, peerPub)
sx, _ := curve.ScalarMult(x, y, priv)
secret := cryptohash(sx.Bytes())
aesKey1 := secret[0:aes.BlockSize]
aesKey2 := secret[aes.BlockSize : 2*aes.BlockSize]
vector1 := secret[2*aes.BlockSize : 3*aes.BlockSize]
vector2 := secret[3*aes.BlockSize : 4*aes.BlockSize]
block1, err := aes.NewCipher(aesKey1)
if err != nil {
return nil, nil, err
}
block2, err := aes.NewCipher(aesKey2)
if err != nil {
return nil, nil, err
}
stream1 := cipher.NewOFB(block1, vector1)
stream2 := cipher.NewOFB(block2, vector2)
return stream1, stream2, nil
}
示例3: ExampleNewOFB
func ExampleNewOFB() {
key := []byte("example key 1234")
plaintext := []byte("some plaintext")
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewOFB(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
// It's important to remember that ciphertexts must be authenticated
// (i.e. by using crypto/hmac) as well as being encrypted in order to
// be secure.
// OFB mode is the same for both encryption and decryption, so we can
// also decrypt that ciphertext with NewOFB.
plaintext2 := make([]byte, len(plaintext))
stream = cipher.NewOFB(block, iv)
stream.XORKeyStream(plaintext2, ciphertext[aes.BlockSize:])
fmt.Printf("%s\n", plaintext2)
// Output: some plaintext
}
示例4: main
func main() {
plainText := []byte("Bob loves Alice.")
key := []byte("passw0rdpassw0rdpassw0rdpassw0rd")
// Create new DES cipher block
block, err := aes.NewCipher(key)
if err != nil {
fmt.Printf("err: %s\n", err)
}
// The IV (Initialization Vector) need to be unique, but not secure.
// Therefore, it's common to include it at the beginning of the cipher text.
cipherText := make([]byte, aes.BlockSize+len(plainText))
// Create IV
iv := cipherText[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
fmt.Printf("err: %s\n", err)
}
// Encrypt
encryptStream := cipher.NewOFB(block, iv)
encryptStream.XORKeyStream(cipherText[aes.BlockSize:], plainText)
fmt.Printf("Cipher text: %v \n", cipherText)
// Decrpt
decryptedText := make([]byte, len(cipherText[aes.BlockSize:]))
decryptStream := cipher.NewOFB(block, cipherText[:aes.BlockSize])
decryptStream.XORKeyStream(decryptedText, cipherText[aes.BlockSize:])
fmt.Printf("Decrypted text: %s\n", string(decryptedText))
}
示例5: DecryptIO
func DecryptIO(reader io.Reader, writer io.Writer, passphrase string) (mac []byte, e error) {
key := PassphraseToKey(passphrase)
hashMac := hmac.New(sha256.New, key)
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
iv := make([]byte, aes.BlockSize)
if _, err = io.ReadFull(reader, iv); err != nil {
return nil, err
}
stream := cipher.NewOFB(block, iv)
cipherReader := &cipher.StreamReader{S: stream, R: reader}
pp := make([]byte, len([]byte(passphrase)))
if _, err = io.ReadFull(cipherReader, pp); err != nil {
return nil, err
}
if passphrase != string(pp) {
return nil, errors.New("Incorrect passphrase")
}
mw := io.MultiWriter(writer, hashMac)
if _, err = io.Copy(mw, cipherReader); err != nil {
return nil, err
}
return hashMac.Sum(nil), nil
}
示例6: EncryptIO
func EncryptIO(reader io.Reader, writer io.Writer, passphrase string) (mac []byte, e error) {
key := PassphraseToKey(passphrase)
hashMac := hmac.New(sha256.New, key)
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
iv := make([]byte, aes.BlockSize)
if _, err = io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
stream := cipher.NewOFB(block, iv)
_, err = writer.Write(iv)
if err != nil {
return nil, err
}
cipherWriter := &cipher.StreamWriter{S: stream, W: writer}
_, err = cipherWriter.Write([]byte(passphrase))
if err != nil {
return nil, err
}
mw := io.MultiWriter(cipherWriter, hashMac)
if _, err = io.Copy(mw, reader); err != nil {
return nil, err
}
return hashMac.Sum(nil), nil
}
示例7: AesEncryptData
func AesEncryptData(data, key []byte, ctp string) []byte {
block, err := aes.NewCipher(key)
if err != nil {
return nil
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
data_enc := make([]byte, aes.BlockSize+len(data))
iv := data_enc[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil
}
var stream cipher.Stream
switch ctp {
case "cfb":
stream = cipher.NewCFBEncrypter(block, iv)
case "ctr":
stream = cipher.NewCTR(block, iv)
default:
stream = cipher.NewOFB(block, iv)
}
stream.XORKeyStream(data_enc[aes.BlockSize:], data)
// It's important to remember that ciphertexts must be authenticated
// (i.e. by using crypto/hmac) as well as being encrypted in order to
// be secure.
return data_enc
}
示例8: writeBytes
// WriteBytes writes an encrypted/compressed stream to an io.Writer
func writeBytes(out io.Writer, key string, data []byte) error {
var gzWriter *gzip.Writer // compressed writer
var iv [aes.BlockSize]byte // initialization vector
var cb cipher.Block // cipher block interface
var err error // general error holder
// init cipher block
if cb, err = aes.NewCipher(hashKey(key)); err != nil {
return err
}
// init encrypted writer
encWriter := &cipher.StreamWriter{
S: cipher.NewOFB(cb, iv[:]),
W: out,
}
// close when done
defer encWriter.Close()
// wrap encrypted writer
gzWriter = gzip.NewWriter(encWriter)
// close when done
defer gzWriter.Close()
// copy data to destination file compressing and encrypting along the way
_, err = io.Copy(gzWriter, bytes.NewReader(data))
// return copy error
return err
}
示例9: DecryptFile
// decrypt file
func (c *Crypt) DecryptFile(encrypt, decrypt string) error {
inFile, err := os.Open(encrypt)
if err != nil {
return err
}
defer inFile.Close()
block, err := aes.NewCipher(c.key)
if err != nil {
return err
}
// If the key is unique for each ciphertext, then it's ok to use a zero
// IV.
var iv [aes.BlockSize]byte
stream := cipher.NewOFB(block, iv[:])
outFile, err := os.OpenFile(decrypt, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer outFile.Close()
reader := &cipher.StreamReader{S: stream, R: inFile}
// Copy the input file to the output file, decrypting as we go.
if _, err := io.Copy(outFile, reader); err != nil {
return err
}
return nil
}
示例10: decryptResult
func (a *BuyActivity) decryptResult() error {
block, err := aes.NewCipher(a.encResultKey[:])
if err != nil {
return err
}
temp := a.manager.GetStorage().Create(fmt.Sprintf("Buy #%v: result", a.GetKey()))
defer temp.Dispose()
encrypted := a.encResultFile.Open()
defer encrypted.Close()
// Create OFB stream with null initialization vector (ok for one-time key)
var iv [aes.BlockSize]byte
stream := cipher.NewOFB(block, iv[:])
reader := &cipher.StreamReader{S: stream, R: encrypted}
_, err = io.Copy(temp, reader)
if err != nil {
return err
}
if err := temp.Close(); err != nil {
return err
}
a.resultFile = temp.File()
return nil
}
示例11: NewReader
// NewReader returns a reader that encrypts or decrypts its input stream.
func (key TwofishKey) NewReader(r io.Reader) io.Reader {
// OK to use a zero IV if the key is unique for each ciphertext.
iv := make([]byte, twofish.BlockSize)
stream := cipher.NewOFB(key.NewCipher(), iv)
return &cipher.StreamReader{S: stream, R: r}
}
示例12: readBytes
// readBytes reads an encrypted/compressed steam from an io.Reader
// and returns a decoded byte slice
func readBytes(in io.Reader, key string) ([]byte, error) {
var gzReader *gzip.Reader // compressed reader
var iv [aes.BlockSize]byte // initialization vector
var cb cipher.Block // cipher block interface
var outBytes *bytes.Buffer // output buffer
var err error // general error handler
// init cipher block
if cb, err = aes.NewCipher(hashKey(key)); err != nil {
return nil, err
}
// init encrypted reader
encReader := &cipher.StreamReader{
S: cipher.NewOFB(cb, iv[:]),
R: in,
}
// wrap encrypted reader
if gzReader, err = gzip.NewReader(encReader); err != nil {
return nil, err
}
// close when done
defer gzReader.Close()
// init output
outBytes = new(bytes.Buffer)
// read data into output buffer decompressing and decrypting along the way
_, err = io.Copy(outBytes, gzReader)
// return bytes and last error state
return outBytes.Bytes(), err
}
示例13: AesDecryptFd
func AesDecryptFd(inFile, outFile *os.File, key, iv []byte, ctp int) error {
block, err := aes.NewCipher(key)
if err != nil {
return err
}
var stream cipher.Stream
switch ctp {
case 1:
stream = cipher.NewCFBDecrypter(block, iv[:])
case 2:
stream = cipher.NewCTR(block, iv[:])
default:
stream = cipher.NewOFB(block, iv[:])
}
reader := &cipher.StreamReader{S: stream, R: inFile}
// Copy the input file to the output file, decrypting as we go.
if _, err := io.Copy(outFile, reader); err != nil {
return err
}
// Note that this example is simplistic in that it omits any
// authentication of the encrypted data. If you were actually to use
// StreamReader in this manner, an attacker could flip arbitrary bits in
// the output.
return nil
}
示例14: decryptAES
// decryptAES derypts ciphertext input with passed key and mode (IV is contained in input)
// in AES block cipher; and returns plaintext output
func decryptAES(input []byte, output []byte, key []byte, mode Mode) error {
block, err := aes.NewCipher(key)
if err != nil {
return errors.New("Couldn't create block cipher.")
}
if len(input) < aes.BlockSize {
return errors.New("Ciphertext too short.")
}
iv := input[:aes.BlockSize]
ciphertext := input[aes.BlockSize:]
switch mode {
case CBC:
if len(input)%aes.BlockSize != 0 {
return errors.New("Ciphertext doesn't satisfy CBC-mode requirements.")
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(output, ciphertext)
case CFB:
mode := cipher.NewCFBDecrypter(block, iv)
mode.XORKeyStream(output, ciphertext)
case CTR:
mode := cipher.NewCTR(block, iv)
mode.XORKeyStream(output, ciphertext)
case OFB:
mode := cipher.NewOFB(block, iv)
mode.XORKeyStream(output, ciphertext)
}
return nil
}
示例15: encryptAES
// encryptAES enrypts plaintext input with passed key, IV and mode in AES block cipher;
// and returns ciphertext output
func encryptAES(input []byte, output []byte, key, iv []byte, mode Mode) error {
block, err := aes.NewCipher(key)
if err != nil {
return errors.New("Couldn't create block cipher.")
}
// Prepend IV to ciphertext.
// Generate IV randomly if it is not passed
if iv == nil {
if iv = generateIV(aes.BlockSize); iv == nil {
return errors.New("Couldn't create random initialization vector (IV).")
}
}
copy(output, iv)
switch mode {
case CBC:
if len(input)%aes.BlockSize != 0 {
input = addPadding(input, aes.BlockSize)
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(output[aes.BlockSize:], input)
case CFB:
mode := cipher.NewCFBEncrypter(block, iv)
mode.XORKeyStream(output[aes.BlockSize:], input)
case CTR:
mode := cipher.NewCTR(block, iv)
mode.XORKeyStream(output[aes.BlockSize:], input)
case OFB:
mode := cipher.NewOFB(block, iv)
mode.XORKeyStream(output[aes.BlockSize:], input)
}
return nil
}