本文整理汇总了Golang中crypto/des.NewCipher函数的典型用法代码示例。如果您正苦于以下问题:Golang NewCipher函数的具体用法?Golang NewCipher怎么用?Golang NewCipher使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewCipher函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Sign
func (s *descbc) Sign(algo, usage int, data ...[]byte) ([]byte, error) {
var h hash.Hash
switch algo {
case signGssDes:
sz := 0
for _, d := range data {
sz += len(d)
}
sz = (sz + 7) &^ 7
u := make([]byte, sz)
v := u[:0]
for _, d := range data {
v = append(v, d...)
}
iv := [8]byte{}
b, _ := des.NewCipher(s.key)
c := cipher.NewCBCEncrypter(b, iv[:])
c.CryptBlocks(u, u)
return u[len(u)-8:], nil
case signGssMd5Des:
h = md5.New()
for _, d := range data {
h.Write(d)
}
return s.Sign(signGssDes, usage, h.Sum(nil))
case signMd5Des:
h = md5.New()
case signMd4Des:
h = md4.New()
default:
return unkeyedSign(algo, usage, data...)
}
var key [8]byte
for i := 0; i < 8; i++ {
key[i] = s.key[i] ^ 0xF0
}
chk := make([]byte, 24)
io.ReadFull(rand.Reader, chk[:8])
h.Write(chk[:8])
for _, d := range data {
h.Write(d)
}
h.Sum(chk[8:])
iv := [8]byte{}
b, _ := des.NewCipher(s.key)
c := cipher.NewCBCEncrypter(b, iv[:])
c.CryptBlocks(chk, chk)
return chk, nil
}
示例2: NewBlock
func NewBlock(method string, keyfile string) (c cipher.Block, err error) {
logger.Debugf("Crypt Wrapper with %s preparing.", method)
file, err := os.Open(keyfile)
if err != nil {
return
}
defer file.Close()
key := make([]byte, KEYSIZE)
_, err = io.ReadFull(file, key)
if err != nil {
return
}
switch method {
case "aes":
c, err = aes.NewCipher(key)
case "des":
c, err = des.NewCipher(key)
case "tripledes":
c, err = des.NewTripleDESCipher(key)
}
return
}
示例3: encryptDESCBC
func encryptDESCBC(content []byte) ([]byte, *encryptedContentInfo, error) {
// Create DES key & CBC IV
key := make([]byte, 8)
iv := make([]byte, des.BlockSize)
_, err := rand.Read(key)
if err != nil {
return nil, nil, err
}
_, err = rand.Read(iv)
if err != nil {
return nil, nil, err
}
// Encrypt padded content
block, err := des.NewCipher(key)
if err != nil {
return nil, nil, err
}
mode := cipher.NewCBCEncrypter(block, iv)
plaintext, err := pad(content, mode.BlockSize())
cyphertext := make([]byte, len(plaintext))
mode.CryptBlocks(cyphertext, plaintext)
// Prepare ASN.1 Encrypted Content Info
eci := encryptedContentInfo{
ContentType: oidData,
ContentEncryptionAlgorithm: pkix.AlgorithmIdentifier{
Algorithm: oidEncryptionAlgorithmDESCBC,
Parameters: asn1.RawValue{Tag: 4, Bytes: iv},
},
EncryptedContent: marshalEncryptedContent(cyphertext),
}
return key, &eci, nil
}
示例4: NewDES
/*
介绍:创建默认DESCipher,使用ECB工作模式、pkcs57填充,算法秘钥长度64位 , 使用秘钥作为初始向量
作者:Alex
版本:release-1.1
*/
func NewDES(key []byte) (Cipher, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
return NewECBMode().Cipher(block, key[:block.BlockSize()]), nil
}
示例5: main
func main() {
plainText := []byte("Bob loves Alice.")
// Key must be 8 bytes
key := []byte("passw0rd")
// Create Cipher block for DES
block, err := des.NewCipher(key)
if err != nil {
fmt.Printf("err: %s", err)
}
// Create initialization vector from rand.reader
iv := make([]byte, des.BlockSize)
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
fmt.Printf("err: %s", err)
return
}
// Encrypt with CBC mode
cipherText := make([]byte, len(plainText))
encryptMode := cipher.NewCBCEncrypter(block, iv)
encryptMode.CryptBlocks(cipherText, plainText)
fmt.Printf("Cipher text: %v\n", cipherText)
// Decrypt with CBC mode
decryptedText := make([]byte, len(cipherText))
decryptMode := cipher.NewCBCDecrypter(block, iv)
decryptMode.CryptBlocks(decryptedText, cipherText)
fmt.Printf("Decrypted text: %s\n", string(decryptedText))
}
示例6: NewDESWith
/*
介绍:根据指定的工作模式,创建DESCipher,算法秘钥长度64位,使用秘钥作为初始向量
作者:Alex
版本:release-1.1
*/
func NewDESWith(key []byte, mode CipherMode) (Cipher, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
return mode.Cipher(block, key[:block.BlockSize()]), nil
}
示例7: DesEncrypt
func DesEncrypt(src, key []byte) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
log.Printf("[des encrypt] NewCipher error, %v", err)
return nil, err
}
bs := block.BlockSize()
src = ZeroPadding(src, bs)
// src = PKCS5Padding(src, bs)
if len(src)%bs != 0 {
return nil, errors.New("Need a multiple of the blocksize")
}
out := make([]byte, len(src))
dst := out
for len(src) > 0 {
block.Encrypt(dst, src[:bs])
src = src[bs:]
dst = dst[bs:]
}
return out, nil
}
示例8: check
func check(key []byte, original uint64) {
cipher, err := des.NewCipher(key)
if err != nil {
panic(err)
}
i2b := func(i uint64) (b []byte) {
b = make([]byte, 8)
binary.BigEndian.PutUint64(b, i)
return b
}
originalPlainText := i2b(original)
originalCipherText := make([]byte, 8)
cipher.Encrypt(originalCipherText, originalPlainText)
var (
updated uint64
updatedPlainText []byte
)
updatedCipherText := make([]byte, 8)
sum := 0.0
for i := 0; i < 64; i++ {
updated = original ^ (1 << uint64(i))
updatedPlainText = i2b(updated)
cipher.Encrypt(updatedCipherText, updatedPlainText)
diffed := diff(originalCipherText, updatedCipherText)
fmt.Printf("%d -> %d\n", i+1, diffed)
sum += float64(diffed)
}
fmt.Printf("original: %d\n", original)
fmt.Printf("mean: %f\n", sum/64)
}
示例9: cipherDES
func cipherDES(key, iv []byte, isRead bool) interface{} {
block, _ := des.NewCipher(key)
if isRead {
return cipher.NewCBCDecrypter(block, iv)
}
return cipher.NewCBCEncrypter(block, iv)
}
示例10: Decrypt
// Decrypt decrypts ciphertext in CBC mode and returns plaintext.
// ciphertext's length must be a multiple of 8. key is a 24 byte des key.
// iv is an 8 byte initialization vector. If iv is nil, zeros
// will be used as the initialization vector.
func Decrypt(ciphertext, key, iv []byte) ([]byte, error) {
switch {
case len(ciphertext)%8 != 0:
return nil, errors.New("invalid ciphertext length")
case len(key) != 24:
return nil, errors.New("invalid key length")
case iv == nil:
iv = defaultIV
case len(iv) != 8:
return nil, errors.New("invalid iv length")
}
out := make([]byte, 0, len(ciphertext))
prevXor := make([]byte, 8)
temp := make([]byte, 8)
preWhiten := key[8:16]
postWhiten := key[16:24]
cipher, err := des.NewCipher(key[:8])
if err != nil {
panic(err)
}
copy(prevXor, iv)
for i := 0; i < len(ciphertext)/8; i++ {
curCipher := ciphertext[i*8 : i*8+8]
cipher.Decrypt(temp, xorBlock2(postWhiten, curCipher))
curPlain := xorBlock3(prevXor, preWhiten, temp)
out = append(out, curPlain...)
copy(prevXor, curCipher)
}
return out, nil
}
示例11: main
func main() {
//需要去加密的字符串
plaintext := []byte("My name is Astaxie")
//如果传入加密串的话,plaint就是传入的字符串
if len(os.Args) > 1 {
plaintext = []byte(os.Args[1])
}
//des的加密字符串
key_text := "12345678"
if len(os.Args) > 2 {
key_text = os.Args[2]
}
fmt.Println(len(key_text))
// 创建加密算法des
c, err := des.NewCipher([]byte(key_text))
if err != nil {
fmt.Printf("Error: NewCipher(%d bytes) = %s", len(key_text), err)
os.Exit(-1)
}
//加密字符串
cfb := cipher.NewCFBEncrypter(c, commonIV)
ciphertext := make([]byte, len(plaintext))
cfb.XORKeyStream(ciphertext, plaintext)
fmt.Printf("%s=>%x\n", plaintext, ciphertext)
// 解密字符串
cfbdec := cipher.NewCFBDecrypter(c, commonIV)
plaintextCopy := make([]byte, len(plaintext))
cfbdec.XORKeyStream(plaintextCopy, ciphertext)
fmt.Printf("%x=>%s\n", ciphertext, plaintextCopy)
}
示例12: main
func main() {
key, _ := hex.DecodeString("0000000000000000")
block, _ := des.NewCipher(key)
data := make([]byte, 8)
block.Encrypt(data, key)
fmt.Println(hex.EncodeToString(data))
}
示例13: Decrypt
func (s *descbc) Decrypt(salt []byte, algo, usage int, data []byte) ([]byte, error) {
var h hash.Hash
switch algo {
case cryptDesCbcMd5:
h = md5.New()
case cryptDesCbcMd4:
h = md4.New()
default:
return nil, ErrProtocol
}
if (len(data) & 7) != 0 {
return nil, ErrProtocol
}
iv := [8]byte{}
b, _ := des.NewCipher(s.key)
c := cipher.NewCBCDecrypter(b, iv[:])
c.CryptBlocks(data, data)
chk := make([]byte, h.Size())
h.Write(data[:8])
h.Write(chk) // Just need h.Size() zero bytes instead of the checksum
h.Write(data[8+len(chk):])
h.Sum(chk[:0])
if subtle.ConstantTimeCompare(chk, data[8:8+len(chk)]) != 1 {
return nil, ErrProtocol
}
return data[8+len(chk):], nil
}
示例14: TripleEcbDesEncrypt
//[golang ECB 3DES Encrypt]
func TripleEcbDesEncrypt(origData, key []byte) ([]byte, error) {
tkey := make([]byte, 24, 24)
copy(tkey, key)
k1 := tkey[:8]
k2 := tkey[8:16]
k3 := tkey[16:]
block, err := des.NewCipher(k1)
if err != nil {
return nil, err
}
bs := block.BlockSize()
origData = PKCS5Padding(origData, bs)
buf1, err := encrypt(origData, k1)
if err != nil {
return nil, err
}
buf2, err := decrypt(buf1, k2)
if err != nil {
return nil, err
}
out, err := encrypt(buf2, k3)
if err != nil {
return nil, err
}
return out, nil
}
示例15: decryptDES
func decryptDES(src, key, privParam []byte) (dst []byte, err error) {
if len(src)%des.BlockSize != 0 {
err = ArgumentError{
Value: len(src),
Message: "Invalid DES cipher length",
}
return
}
if len(privParam) != 8 {
err = ArgumentError{
Value: len(privParam),
Message: "Invalid DES PrivParameter length",
}
return
}
block, err := des.NewCipher(key[:8])
if err != nil {
return
}
iv := xor(key[8:16], privParam)
dst = make([]byte, len(src))
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(dst, src)
return
}