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 8922247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:47:06+00:00 2026-06-15T06:47:06+00:00

I am trying to implement the Twofish cypher step by step as described in

  • 0

I am trying to implement the Twofish cypher step by step as described in the 1998 paper of Bruce Schneider. Nevertheless, I already fail at the key expansion.

I tried to copy the details of the paper 1-to-1 into python, with the following result:

#! /usr/bin/python3.2

def expandKey256 (key):
    m = [0] * (32)
    for i in range (32):
        m [i] = (key >> (i * 8) ) & 0xff
        #m [31 - i] = (key >> (i * 8) ) & 0xff
    print ('m = {}\n'.format ( [hex (b) for b in m] ) )

    M = [0] * 8
    for i in range (8):
        for j in range (4):
            M [i] += m [4 * i + j] * 2 ** (8 * j)
    print ('M = {}\n'.format ( [hex (b) for b in M] ) )

    Me = [M [0], M [2], M [4], M [6] ]
    Mo = [M [1], M [3], M [5], M [7] ]
    print ('Me = {}\n'.format ( [hex (b) for b in Me] ) )
    print ('Mo = {}\n'.format ( [hex (b) for b in Mo] ) )

    RS = [ [0x01, 0xA4, 0x55, 0x87, 0x5A, 0x58, 0xDB, 0x9E],
        [0xA4, 0x56, 0x82, 0xF3, 0x1E, 0xC6, 0x68, 0xE5],
        [0x02, 0xA1, 0xFC, 0xC1, 0x47, 0xAE, 0x3D, 0x19],
        [0xA4, 0x55, 0x87, 0x5A, 0x58, 0xDB, 0x9E, 0x03] ]

    s = [ [0] * 4] * 4
    S = [0] * 4
    for i in range (4):
        for j in range (4):
            for k in range (8):
                s [i] [j] += m [8 * i + k] * RS [j] [k]
                s [i] [j] &= 0xff
            S [i] += s [i] [j] * 2 ** (8 * j)
    for i in range (4):
        print ('S{} = {}'.format (i, hex (S [i] ) ) )

expandKey256 (0x0123456789ABCDEFFEDCBA987654321000112233445566778899AABBCCDDEEFF)

However, my output isn’t the same as specified in the test vectors. I already tried to read the bytes the other way round (the commented line), but no avail.

These are the results of the test vector:

B89FF6F2
B255BC4B
45661061
8E4447F7

And these are mine:

S0 = 0x612a646d
S1 = 0x527cc87a
S2 = 0x1482c008
S3 = 0xa4d128ce

Can anyone see my error?

  • 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-06-15T06:47:07+00:00Added an answer on June 15, 2026 at 6:47 am

    At least this line

    s = [ [0] * 4] * 4
    

    probably is not doing what you think it is doing. It is not doing the same thing as

    s = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    

    I didn’t go through all of the code however.

    EDIT

    Apparently the OP needs more proof. Here some output from IDLE showing the difference

    >>> s = [ [0] * 4] * 4
    >>> s
    [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    >>> s[0][0] += 1
    >>> s
    [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
    >>> s = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    >>> s
    [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    >>> s[0][0] += 1
    >>> s
    [[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    >>> 
    

    The expression s = [ [0] * 4] * 4 creates a list containing another list of zeros, then makes 3 more copies of the reference to the list. It is equivalent to v = [0]*4; s=[v,v,v,v]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying implement A* Start path finding in my games(which are written with JavaScript,
I am trying implement a most recent widget into my tumblr post. So far,
Trying to implement LoaderManager + CursorLoader. In onFinish method adapter should swap its cursor
Trying to implement google C2DM service. registrationIntent.putExtra(app, PendingIntent.getBroadcast(context,0,new Intent(), 0)); registrationIntent.putExtra(sender,example@gmail.com); context.startService(registrationIntent); Almost every
Trying to implement a search similar to here .This searches properties based on city,locality,property
Trying to implement 3-layer (not: tier, I just want to separate my project logically,
Trying to implement a rating system of users and postings. What is the best
Trying to implement a UITableView of names similar to the built-in Contacts iPhone app
Im trying to implement a destructor for the objects of linked-list Iv created. I
I trying to implement some relatively simple 2D sprite batching in OpenGL ES 2.0

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.