Golang 解密数组 Byte Xtea ECB 算法

我有一个输入字节数组:


60614e680e705d0fefcf7ac8102c4452

ecb0c85768f2f2dc52415c43a36712f0

31c9037dafd31f01ecb0c85768f2f2dc

7b00be7e5a15fee1e78c63c58c2c6861

fef9a1c4130a354c846448512e6a97ce

4a9005690d1e3808f065c957538e1bac

87e7228322ab39a6900146786840dc0b

c536ad6afb6e4e3267fb045dd9c7e670

f1c2d2ac1fcc71ad06b7b194de4031f4

046744610aafa7b92fd3f392c3a5eeb1

474ffa60c0587e68ecb0c85768f2f2dc

2a88827461b41c99b2539b6bfdcd4325

be3ced59be7b594addb3366e076f6e47

0cc41df1eb3a8d93c99eb7bdad5a474c

33659653762910d0ecb0c85768f2f2dc

ecb0c85768f2f2dc82e715e7952a79c4

660074ccc50741cab5eabb873ae706b4

c8b008128df0af80fece91741fc5f641

1145aab35ac9f6e0f8a937baed012d00

c3be705a5e8c3440ddc1cd4e0051cccc

它们被加密的算法 XTEA ECB。我尝试使用该库进行解密“golang.org/x/crypto/xtea”。


package main


import (

    "golang.org/x/crypto/xtea"

    "fmt"

)



func main() {

    // encripte data

    test :=[]byte{

            0x60, 0x61, 0x4e, 0x68, 0x0e, 0x70, 0x5d, 0x0f, 0xef, 0xcf, 0x7a, 0xc8, 0x10, 0x2c, 0x44, 0x52,

            0xec, 0xb0, 0xc8, 0x57, 0x68, 0xf2, 0xf2, 0xdc, 0x52, 0x41, 0x5c, 0x43, 0xa3, 0x67, 0x12, 0xf0,

            0x31, 0xc9, 0x03, 0x7d, 0xaf, 0xd3, 0x1f, 0x01, 0xec, 0xb0, 0xc8, 0x57, 0x68, 0xf2, 0xf2, 0xdc,

            0x7b, 0x00, 0xbe, 0x7e, 0x5a, 0x15, 0xfe, 0xe1, 0xe7, 0x8c, 0x63, 0xc5, 0x8c, 0x2c, 0x68, 0x61,

            0xfe, 0xf9, 0xa1, 0xc4, 0x13, 0x0a, 0x35, 0x4c, 0x84, 0x64, 0x48, 0x51, 0x2e, 0x6a, 0x97, 0xce,

            0x4a, 0x90, 0x05, 0x69, 0x0d, 0x1e, 0x38, 0x08, 0xf0, 0x65, 0xc9, 0x57, 0x53, 0x8e, 0x1b, 0xac,

            }

    // key

    key := []byte("yuyuyuyuopopopop")

    c, _ := xtea.NewCipher(key)

    myout := make([]byte, len(test))

    // decrypte

    c.Decrypt(myout, test)


    fmt.Println(myout)

}

我的结果与应有的结果不一致。也许我没有正确使用图书馆?还是数据类型有问题?我是golang的新手。


拉莫斯之舞
浏览 129回答 1
1回答

www说

也许有人会派上用场。库“golang.org/x/crypto/xtea”由于其不安全性,不包含带有 ECB 模型的 XTEA 算法,因此我编写了自己的版本:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "encoding/hex"&nbsp; &nbsp; "encoding/binary")func main() {&nbsp; &nbsp; original := []byte{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x60, 0x61, 0x4e, 0x68, 0x0e, 0x70, 0x5d, 0x0f, 0xef, 0xcf, 0x7a, 0xc8, 0x10, 0x2c, 0x44, 0x52,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0xec, 0xb0, 0xc8, 0x57, 0x68, 0xf2, 0xf2, 0xdc, 0x52, 0x41, 0x5c, 0x43, 0xa3, 0x67, 0x12, 0xf0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x31, 0xc9, 0x03, 0x7d, 0xaf, 0xd3, 0x1f, 0x01, 0xec, 0xb0, 0xc8, 0x57, 0x68, 0xf2, 0xf2, 0xdc,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x7b, 0x00, 0xbe, 0x7e, 0x5a, 0x15, 0xfe, 0xe1, 0xe7, 0x8c, 0x63, 0xc5, 0x8c, 0x2c, 0x68, 0x61,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0xfe, 0xf9, 0xa1, 0xc4, 0x13, 0x0a, 0x35, 0x4c, 0x84, 0x64, 0x48, 0x51, 0x2e, 0x6a, 0x97, 0xce,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x4a, 0x90, 0x05, 0x69, 0x0d, 0x1e, 0x38, 0x08, 0xf0, 0x65, 0xc9, 0x57, 0x53, 0x8e, 0x1b, 0xac,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x87, 0xe7, 0x22, 0x83, 0x22, 0xab, 0x39, 0xa6, 0x90, 0x01, 0x46, 0x78, 0x68, 0x40, 0xdc, 0x0b,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0xc5, 0x36, 0xad, 0x6a, 0xfb, 0x6e, 0x4e, 0x32, 0x67, 0xfb, 0x04, 0x5d, 0xd9, 0xc7, 0xe6, 0x70,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0xf1, 0xc2, 0xd2, 0xac, 0x1f, 0xcc, 0x71, 0xad, 0x06, 0xb7, 0xb1, 0x94, 0xde, 0x40, 0x31, 0xf4,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x04, 0x67, 0x44, 0x61, 0x0a, 0xaf, 0xa7, 0xb9, 0x2f, 0xd3, 0xf3, 0x92, 0xc3, 0xa5, 0xee, 0xb1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x47, 0x4f, 0xfa, 0x60, 0xc0, 0x58, 0x7e, 0x68, 0xec, 0xb0, 0xc8, 0x57, 0x68, 0xf2, 0xf2, 0xdc,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x2a, 0x88, 0x82, 0x74, 0x61, 0xb4, 0x1c, 0x99, 0xb2, 0x53, 0x9b, 0x6b, 0xfd, 0xcd, 0x43, 0x25,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0xbe, 0x3c, 0xed, 0x59, 0xbe, 0x7b, 0x59, 0x4a, 0xdd, 0xb3, 0x36, 0x6e, 0x07, 0x6f, 0x6e, 0x47,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x0c, 0xc4, 0x1d, 0xf1, 0xeb, 0x3a, 0x8d, 0x93, 0xc9, 0x9e, 0xb7, 0xbd, 0xad, 0x5a, 0x47, 0x4c,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x33, 0x65, 0x96, 0x53, 0x76, 0x29, 0x10, 0xd0, 0xec, 0xb0, 0xc8, 0x57, 0x68, 0xf2, 0xf2, 0xdc,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0xec, 0xb0, 0xc8, 0x57, 0x68, 0xf2, 0xf2, 0xdc, 0x82, 0xe7, 0x15, 0xe7, 0x95, 0x2a, 0x79, 0xc4,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x66, 0x00, 0x74, 0xcc, 0xc5, 0x07, 0x41, 0xca, 0xb5, 0xea, 0xbb, 0x87, 0x3a, 0xe7, 0x06, 0xb4,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0xc8, 0xb0, 0x08, 0x12, 0x8d, 0xf0, 0xaf, 0x80, 0xfe, 0xce, 0x91, 0x74, 0x1f, 0xc5, 0xf6, 0x41,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0x11, 0x45, 0xaa, 0xb3, 0x5a, 0xc9, 0xf6, 0xe0, 0xf8, 0xa9, 0x37, 0xba, 0xed, 0x01, 0x2d, 0x00,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0xc3, 0xbe, 0x70, 0x5a, 0x5e, 0x8c, 0x34, 0x40, 0xdd, 0xc1, 0xcd, 0x4e, 0x00, 0x51, 0xcc, 0xcc}&nbsp; &nbsp; testKey := []byte("yuyuyuyuopopopop")&nbsp; &nbsp; block := make([]byte, 8)&nbsp; &nbsp; for i := 0; i< len(original)/8; i++ {&nbsp; &nbsp; &nbsp; &nbsp; block = decrypt(testKey , original[(i*8):((i+1)*8)], 32)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(hex.Dump(block))&nbsp; &nbsp; }}func decrypt(key []byte, block []byte, rounds uint32) ([]byte){&nbsp; &nbsp; var k [4]uint32&nbsp; &nbsp; var i uint32&nbsp; &nbsp; end := make([]byte, 8)&nbsp; &nbsp; v0 := binary.LittleEndian.Uint32(block[:4])&nbsp; &nbsp; v1 := binary.LittleEndian.Uint32(block[4:])&nbsp; &nbsp; k[0] = binary.LittleEndian.Uint32(key[:4])&nbsp; &nbsp; k[1] = binary.LittleEndian.Uint32(key[4:8])&nbsp; &nbsp; k[2] = binary.LittleEndian.Uint32(key[8:12])&nbsp; &nbsp; k[3] = binary.LittleEndian.Uint32(key[12:])&nbsp; &nbsp; delta := binary.LittleEndian.Uint32([]byte{0xb9, 0x79, 0x37, 0x9e})&nbsp; &nbsp; mask := binary.LittleEndian.Uint32([]byte{0xff, 0xff, 0xff, 0xff})&nbsp; &nbsp; sum := (delta * rounds) & mask&nbsp; &nbsp; for i=0; i<rounds; i++ {&nbsp; &nbsp; &nbsp; &nbsp; v1 = (v1 - (((v0<<4 ^ v0>>5) + v0) ^ (sum + k[sum>>11 & 3]))) & mask&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = (sum - delta) & mask&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v0 = (v0 - (((v1<<4 ^ v1>>5) + v1) ^ (sum + k[sum & 3]))) & mask&nbsp; &nbsp; }&nbsp; &nbsp; binary.LittleEndian.PutUint32(end[:4], v0)&nbsp; &nbsp; binary.LittleEndian.PutUint32(end[4:], v1)&nbsp; &nbsp; return end}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go