栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Go中正确编写JVM AES / CFB8加密

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Go中正确编写JVM AES / CFB8加密

解决方案是手动实施CFB8,因为内置实施默认为CFB128。

由kostya创建并由IlmariKaronen修复的实现。

如果有人正在寻找分段大小= 8的Go实现CFB模式,则可以使用以下方法:

import "crypto/cipher"// CFB stream with 8 bit segment size// See http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdftype cfb8 struct {    b         cipher.Block    blockSize int    in        []byte    out       []byte    decrypt bool}func (x *cfb8) XORKeyStream(dst, src []byte) {    for i := range src {        x.b.Encrypt(x.out, x.in)        copy(x.in[:x.blockSize-1], x.in[1:])        if x.decrypt { x.in[x.blockSize-1] = src[i]        }        dst[i] = src[i] ^ x.out[0]        if !x.decrypt { x.in[x.blockSize-1] = dst[i]        }    }}// NewCFB8Encrypter returns a Stream which encrypts with cipher feedback mode// (segment size = 8), using the given Block. The iv must be the same length as// the Block's block size.func newCFB8Encrypter(block cipher.Block, iv []byte) cipher.Stream {    return newCFB8(block, iv, false)}// NewCFB8Decrypter returns a Stream which decrypts with cipher feedback mode// (segment size = 8), using the given Block. The iv must be the same length as// the Block's block size.func newCFB8Decrypter(block cipher.Block, iv []byte) cipher.Stream {    return newCFB8(block, iv, true)}func newCFB8(block cipher.Block, iv []byte, decrypt bool) cipher.Stream {    blockSize := block.BlockSize()    if len(iv) != blockSize {        // stack trace will indicate whether it was de or encryption        panic("cipher.newCFB: IV length must equal block size")    }    x := &cfb8{        b:         block,        blockSize: blockSize,        out:       make([]byte, blockSize),        in:        make([]byte, blockSize),        decrypt:   decrypt,    }    copy(x.in, iv)    return x}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/426829.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号