Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 713149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:55:44+00:00 2026-05-14T04:55:44+00:00

Anybody knows proper python implementation of TEA (Tiny Encryption Algorithm)? I tried the one

  • 0

Anybody knows proper python implementation of TEA (Tiny Encryption Algorithm)? I tried the one I’ve found here: http://sysadminco.com/code/python-tea/ – but it does not seem to work properly.

It returns different results than other implementations in C or Java. I guess it’s caused by completely different data types in python (or no data types in fact).

Here’s the code and an example:

def encipher(v, k):
    y=v[0];z=v[1];sum=0;delta=0x9E3779B9;n=32
    w=[0,0]
    while(n>0):
        y += (z << 4 ^ z >> 5) + z ^ sum + k[sum & 3]
        y &= 4294967295L # maxsize of 32-bit integer
        sum += delta
        z += (y << 4 ^ y >> 5) + y ^ sum + k[sum>>11 & 3]
        z &= 4294967295L
        n -= 1

    w[0]=y; w[1]=z
    return w

def decipher(v, k):
    y=v[0]
    z=v[1]
    sum=0xC6EF3720
    delta=0x9E3779B9
    n=32
    w=[0,0]
    # sum = delta<<5, in general sum = delta * n

    while(n>0):
        z -= (y << 4 ^ y >> 5) + y ^ sum + k[sum>>11 & 3]
        z &= 4294967295L
        sum -= delta
        y -= (z << 4 ^ z >> 5) + z ^ sum + k[sum&3]
        y &= 4294967295L
        n -= 1

    w[0]=y; w[1]=z
    return w

Python example:

>>> import tea
>>> key = [0xbe168aa1, 0x16c498a3, 0x5e87b018, 0x56de7805]
>>> v = [0xe15034c8, 0x260fd6d5]
>>> res = tea.encipher(v, key)
>>> "%X %X" % (res[0], res[1])
**'70D16811 F935148F'**

C example:

#include <unistd.h>
#include <stdio.h>

void encipher(unsigned long *const v,unsigned long *const w,
   const unsigned long *const k)
{
   register unsigned long       y=v[0],z=v[1],sum=0,delta=0x9E3779B9,
                                a=k[0],b=k[1],c=k[2],d=k[3],n=32;

   while(n-->0)
      {
      sum += delta;
      y += (z << 4)+a ^ z+sum ^ (z >> 5)+b;
      z += (y << 4)+c ^ y+sum ^ (y >> 5)+d;
      }

   w[0]=y; w[1]=z;
}

int main()
{
        unsigned long v[] = {0xe15034c8, 0x260fd6d5};
        unsigned long key[] = {0xbe168aa1, 0x16c498a3, 0x5e87b018, 0x56de7805};

        unsigned long res[2];

        encipher(v, res, key);

        printf("%X %X\n", res[0], res[1]);

        return 0;
}

$ ./tea
**D6942D68 6F87870D**

Please note, that both examples were run with the same input data (v and key), but results were different. I’m pretty sure C implementation is correct – it comes from a site referenced by wikipedia (I couldn’t post a link to it because I don’t have enough reputation points yet – some antispam thing)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:55:44+00:00Added an answer on May 14, 2026 at 4:55 am

    I fixed it. Here is working TEA implementation in python:

    #!/usr/bin/env python
    #-*- coding: utf-8 -*-
    
    import sys
    from ctypes import *
    
    def encipher(v, k):
        y = c_uint32(v[0])
        z = c_uint32(v[1])
        sum = c_uint32(0)
        delta = 0x9e3779b9
        n = 32
        w = [0,0]
    
        while(n>0):
            sum.value += delta
            y.value += ( z.value << 4 ) + k[0] ^ z.value + sum.value ^ ( z.value >> 5 ) + k[1]
            z.value += ( y.value << 4 ) + k[2] ^ y.value + sum.value ^ ( y.value >> 5 ) + k[3]
            n -= 1
    
        w[0] = y.value
        w[1] = z.value
        return w
    
    def decipher(v, k):
        y = c_uint32(v[0])
        z = c_uint32(v[1])
        sum = c_uint32(0xc6ef3720)
        delta = 0x9e3779b9
        n = 32
        w = [0,0]
    
        while(n>0):
            z.value -= ( y.value << 4 ) + k[2] ^ y.value + sum.value ^ ( y.value >> 5 ) + k[3]
            y.value -= ( z.value << 4 ) + k[0] ^ z.value + sum.value ^ ( z.value >> 5 ) + k[1]
            sum.value -= delta
            n -= 1
    
        w[0] = y.value
        w[1] = z.value
        return w
    
    if __name__ == "__main__":
        key = [1,2,3,4]
        v = [1385482522,639876499]
        enc = encipher(v,key)
        print enc
        print decipher(enc,key)
    

    And a small sample:

    >>> v
    [1385482522, 639876499]
    >>> tea.decipher(tea.encipher(v,key),key)
    [1385482522L, 639876499L]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anybody knows how to print single-linked list in reverse order (one pass and a
Does anybody know a proper, cross-browser way to empty an IFrame? Is about:blank recognized
Anybody knows if it is possible to get a javascript for/in loop from coffeescript?
Anybody knows how to get nic card name when I do ipconfig/all I can
Anybody knows where to copy the AjaxControlToolkit.DLL so that it appears in the Toolbox
Anybody knows where the :// or the // comes from in most URIs syntaxes?
anybody knows what can be the reason for when i start a service from
Anybody knows a good book or two (or resources) for JVM Tuning? I am
Anybody knows how MySql allocates memory for text and blob in case i'm inserting
Could anybody help me make a proper regular expression from a bunch of text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.