Search code examples
gocryptographyxtea

Golang decrypt array Byte Xtea ECB algorithm


I have an input byte array:

60614e680e705d0fefcf7ac8102c4452
ecb0c85768f2f2dc52415c43a36712f0
31c9037dafd31f01ecb0c85768f2f2dc
7b00be7e5a15fee1e78c63c58c2c6861
fef9a1c4130a354c846448512e6a97ce
4a9005690d1e3808f065c957538e1bac
87e7228322ab39a6900146786840dc0b
c536ad6afb6e4e3267fb045dd9c7e670
f1c2d2ac1fcc71ad06b7b194de4031f4
046744610aafa7b92fd3f392c3a5eeb1
474ffa60c0587e68ecb0c85768f2f2dc
2a88827461b41c99b2539b6bfdcd4325
be3ced59be7b594addb3366e076f6e47
0cc41df1eb3a8d93c99eb7bdad5a474c
33659653762910d0ecb0c85768f2f2dc
ecb0c85768f2f2dc82e715e7952a79c4
660074ccc50741cab5eabb873ae706b4
c8b008128df0af80fece91741fc5f641
1145aab35ac9f6e0f8a937baed012d00
c3be705a5e8c3440ddc1cd4e0051cccc

The algorithm by which they were encrypted XTEA ECB. I try to use the library for decryption “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,
            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,
            }
    // key
    key := []byte("yuyuyuyuopopopop")
    c, _ := xtea.NewCipher(key)
    myout := make([]byte, len(test))
    // decrypte
    c.Decrypt(myout, test)

    fmt.Println(myout)
}

My results do not coincide with what should be the result. Maybe I'm not using the library correctly? Or was it wrong with the data type? I'm new to the golang. The array that should be output:

09300004100e00000104f47795590210
0000000000000000616161615d5d5d5d
09150000000000000000000000000000
000000000000000d1052545530322e30
312e3030303200000012040000000013
040000000014046161616115045d5d5d
5d1604010e00001704010e0000180401
0e00001904010e00001a04010e00001b
04010e00001c04010e00001d04010e00
001e01001f0100200100210100240100
25113235303032000000000000000000
0000002604ef1400002704930d00002d
01022e02e0012f05ffffffff00300103
3101003301003404050100003d20342e
3132382e323400000000000000000000
00000000000000000000000000004401
184f04960d0000500400000000570460
ea0000580422060000590460ea00005a
04e01500005b01005c01025d01005e01
005f010360010361010262010400011b

Solution

  • Maybe someone will come in handy. The library “golang.org/x/crypto/xtea” does not include the XTEA algorithm with the ECB model because of its insecurity, therefore I wrote my own version:

    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
    }