为了账号安全,请及时绑定邮箱和手机立即绑定

Golang 解密数组 Byte Xtea ECB 算法

Golang 解密数组 Byte Xtea ECB 算法

Go
拉莫斯之舞 2022-06-01 10:37:32
我有一个输入字节数组:60614e680e705d0fefcf7ac8102c4452ecb0c85768f2f2dc52415c43a36712f031c9037dafd31f01ecb0c85768f2f2dc7b00be7e5a15fee1e78c63c58c2c6861fef9a1c4130a354c846448512e6a97ce4a9005690d1e3808f065c957538e1bac87e7228322ab39a6900146786840dc0bc536ad6afb6e4e3267fb045dd9c7e670f1c2d2ac1fcc71ad06b7b194de4031f4046744610aafa7b92fd3f392c3a5eeb1474ffa60c0587e68ecb0c85768f2f2dc2a88827461b41c99b2539b6bfdcd4325be3ced59be7b594addb3366e076f6e470cc41df1eb3a8d93c99eb7bdad5a474c33659653762910d0ecb0c85768f2f2dcecb0c85768f2f2dc82e715e7952a79c4660074ccc50741cab5eabb873ae706b4c8b008128df0af80fece91741fc5f6411145aab35ac9f6e0f8a937baed012d00c3be705a5e8c3440ddc1cd4e0051cccc它们被加密的算法 XTEA ECB。我尝试使用该库进行解密“golang.org/x/crypto/xtea”。package mainimport (    "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的新手。
查看完整描述

1 回答

?
www说

TA贡献1775条经验 获得超8个赞

也许有人会派上用场。库“golang.org/x/crypto/xtea”由于其不安全性,不包含带有 ECB 模型的 XTEA 算法,因此我编写了自己的版本:


package main


import (

    "fmt"

    "encoding/hex"

    "encoding/binary"

)


func main() {

    original := []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,

            0x87, 0xe7, 0x22, 0x83, 0x22, 0xab, 0x39, 0xa6, 0x90, 0x01, 0x46, 0x78, 0x68, 0x40, 0xdc, 0x0b,

            0xc5, 0x36, 0xad, 0x6a, 0xfb, 0x6e, 0x4e, 0x32, 0x67, 0xfb, 0x04, 0x5d, 0xd9, 0xc7, 0xe6, 0x70,

            0xf1, 0xc2, 0xd2, 0xac, 0x1f, 0xcc, 0x71, 0xad, 0x06, 0xb7, 0xb1, 0x94, 0xde, 0x40, 0x31, 0xf4,

            0x04, 0x67, 0x44, 0x61, 0x0a, 0xaf, 0xa7, 0xb9, 0x2f, 0xd3, 0xf3, 0x92, 0xc3, 0xa5, 0xee, 0xb1,

            0x47, 0x4f, 0xfa, 0x60, 0xc0, 0x58, 0x7e, 0x68, 0xec, 0xb0, 0xc8, 0x57, 0x68, 0xf2, 0xf2, 0xdc,

            0x2a, 0x88, 0x82, 0x74, 0x61, 0xb4, 0x1c, 0x99, 0xb2, 0x53, 0x9b, 0x6b, 0xfd, 0xcd, 0x43, 0x25,

            0xbe, 0x3c, 0xed, 0x59, 0xbe, 0x7b, 0x59, 0x4a, 0xdd, 0xb3, 0x36, 0x6e, 0x07, 0x6f, 0x6e, 0x47,

            0x0c, 0xc4, 0x1d, 0xf1, 0xeb, 0x3a, 0x8d, 0x93, 0xc9, 0x9e, 0xb7, 0xbd, 0xad, 0x5a, 0x47, 0x4c,

            0x33, 0x65, 0x96, 0x53, 0x76, 0x29, 0x10, 0xd0, 0xec, 0xb0, 0xc8, 0x57, 0x68, 0xf2, 0xf2, 0xdc,

            0xec, 0xb0, 0xc8, 0x57, 0x68, 0xf2, 0xf2, 0xdc, 0x82, 0xe7, 0x15, 0xe7, 0x95, 0x2a, 0x79, 0xc4,

            0x66, 0x00, 0x74, 0xcc, 0xc5, 0x07, 0x41, 0xca, 0xb5, 0xea, 0xbb, 0x87, 0x3a, 0xe7, 0x06, 0xb4,

            0xc8, 0xb0, 0x08, 0x12, 0x8d, 0xf0, 0xaf, 0x80, 0xfe, 0xce, 0x91, 0x74, 0x1f, 0xc5, 0xf6, 0x41,

            0x11, 0x45, 0xaa, 0xb3, 0x5a, 0xc9, 0xf6, 0xe0, 0xf8, 0xa9, 0x37, 0xba, 0xed, 0x01, 0x2d, 0x00,

            0xc3, 0xbe, 0x70, 0x5a, 0x5e, 0x8c, 0x34, 0x40, 0xdd, 0xc1, 0xcd, 0x4e, 0x00, 0x51, 0xcc, 0xcc}


    testKey := []byte("yuyuyuyuopopopop")

    block := make([]byte, 8)

    for i := 0; i< len(original)/8; i++ {

        block = decrypt(testKey , original[(i*8):((i+1)*8)], 32)

        fmt.Println(hex.Dump(block))

    }

}


func decrypt(key []byte, block []byte, rounds uint32) ([]byte){

    var k [4]uint32

    var i uint32

    end := make([]byte, 8)

    v0 := binary.LittleEndian.Uint32(block[:4])

    v1 := binary.LittleEndian.Uint32(block[4:])


    k[0] = binary.LittleEndian.Uint32(key[:4])

    k[1] = binary.LittleEndian.Uint32(key[4:8])

    k[2] = binary.LittleEndian.Uint32(key[8:12])

    k[3] = binary.LittleEndian.Uint32(key[12:])


    delta := binary.LittleEndian.Uint32([]byte{0xb9, 0x79, 0x37, 0x9e})

    mask := binary.LittleEndian.Uint32([]byte{0xff, 0xff, 0xff, 0xff})


    sum := (delta * rounds) & mask


    for i=0; i<rounds; i++ {

        v1 = (v1 - (((v0<<4 ^ v0>>5) + v0) ^ (sum + k[sum>>11 & 3]))) & mask

            sum = (sum - delta) & mask

            v0 = (v0 - (((v1<<4 ^ v1>>5) + v1) ^ (sum + k[sum & 3]))) & mask

    }


    binary.LittleEndian.PutUint32(end[:4], v0)

    binary.LittleEndian.PutUint32(end[4:], v1)


    return end

}



查看完整回答
反对 回复 2022-06-01
  • 1 回答
  • 0 关注
  • 123 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信