Ask Your Question

Aye's profile - activity

2023-05-20 00:31:43 +0200 received badge  Famous Question (source)
2022-04-14 10:57:27 +0200 received badge  Notable Question (source)
2021-05-02 22:04:17 +0200 received badge  Popular Question (source)
2021-02-09 23:39:45 +0200 received badge  Popular Question (source)
2019-06-22 10:14:01 +0200 received badge  Notable Question (source)
2018-04-30 16:42:26 +0200 received badge  Popular Question (source)
2017-05-11 16:23:11 +0200 received badge  Famous Question (source)
2016-05-30 14:20:13 +0200 received badge  Student (source)
2016-05-30 14:14:22 +0200 received badge  Popular Question (source)
2016-05-30 14:14:22 +0200 received badge  Notable Question (source)
2013-05-10 13:24:40 +0200 asked a question The sage example in Crypto (Stallings)

When I doing the exercise of the textbook in Append -C I confront a question, it says " Write a function that takes a bitlength n and generates a modulus N of bitlength n and g less than N and relatively prime to it. "

I am confused about the meaning

does is mean that I have to use the bitlength n and generate the modulus N? ( len(bin(n)) % N)?

2013-05-08 15:54:25 +0200 received badge  Scholar (source)
2013-05-08 15:54:25 +0200 marked best answer Xor the output from SDES

The output of the sdes.encrypt function is a bit strange. The elements of the output can be coerced to the reals but not to integers. So, something like this should work. Replace your return with

return [(ZZ(RR(a)) + ZZ(RR(b))) % 2 for a,b in zip(block1, block2)]
2013-05-08 12:46:35 +0200 commented answer Xor the output from SDES

its amazing!! may I ask where I could find related information of ZZ RR and zip??

2013-05-08 02:06:33 +0200 asked a question Xor the output from SDES

Hi again,

From the Stalling's cryptography textbook I use the xor function a following :

def XorBlock(block1, block2):

   r"""
   Xors two blocks together.
   """
   l = len(block1);
   if (l != len(block2)):
      raise ValueError, "XorBlock arguments must be same length"
   return [(block1[j]+block2[j]) % 2 for j in xrange(l)];

it works well when I use it to xor 2 blocks. But when I wanna xor the output from sDES function as following:

from sage.crypto.block_cipher.sdes import SimplifiedDES
sdes = SimplifiedDES()
T = [0,1,0,1,0,1,0,1]
K = [1, 0, 1, 0, 0, 0, 0, 0, 1, 0]
sdes.encrypt(T,K)
XorBlock(sdes.encrypt(T,K),T)

it will replies TypeError: unsupported operand parent(s) for '+': 'Free binary string monoid' and 'Integer Ring'

So, how could I store the sdes.encrypt(T,K) into a pure list so that the XorBlock could work???

Thank you !!!!

2013-05-07 18:19:59 +0200 asked a question How to design a loop

If I have designed a pseudo random number generator(PRNG), but I just cant output the random number from the first round ( from the input seed), how could I design a loop that could help me run the number of round I need in the PRNG??

2013-05-02 15:51:02 +0200 asked a question How do we get the current time?

I would like to use the current time instead of a password as a key for some encryption. Does anyone know how to obtain the current time with Sage?

Thank you!!!!

2013-04-18 16:43:18 +0200 received badge  Editor (source)
2013-04-18 15:49:56 +0200 asked a question About the output of simpified DES

Here is the Sage code copy from Cryptography and Network Security Principle and Practice 5th by Stallings in Appendix B of SDES Sage code:

P10_data = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6];
P8_data = [6, 3, 7, 4, 8, 5, 10, 9];
LS1_data = [2, 3, 4, 5, 1];
LS2_data = [3, 4, 5, 1, 2];
IP_data = [2, 6, 3, 1, 4, 8, 5, 7];
IPinv_data = [4, 1, 3, 5, 7, 2, 8, 6];
EP_data = [4, 1, 2, 3, 2, 3, 4, 1];
P4_data = [2, 4, 3, 1];
SW_data = [5, 6, 7, 8, 1, 2, 3, 4];

#
# SDES lookup tables
#
S0_data = [[1, 0, 3, 2],
           [3, 2, 1, 0],
           [0, 2, 1, 3],
           [3, 1, 3, 2]];

S1_data = [[0, 1, 2, 3],
           [2, 0, 1, 3],
           [3, 0, 1, 0],
           [2, 1, 0, 3]];

def ApplyPermutation(X, permutation):
    r"""
    This function takes a permutation list (list of bit positions.)
    And outputs a bit list with the bits taken from X. 
    """

    # permute the list X
    l = len(permutation);
    return [X[permutation[j]-1] for j in xrange(l)];

def ApplySBox(X, SBox):
    r"""
    This function Applies the SDES SBox (by table look up
    """

    r = 2*X[0] + X[3];
    c = 2*X[1] + X[2];
    o = SBox[r][c];

    return [o & 2, o & 1];

#
# Each of these functions uses ApplyPermutation
# and a permutation list to perform an SDES
# Expansion/Permutation
#

def P10(X):
    return ApplyPermutation(X, P10_data);

def P8(X):
    return ApplyPermutation(X, P8_data);

def IP(X):
    return ApplyPermutation(X, IP_data);

def IPinv(X):
    return ApplyPermutation(X, IPinv_data);

def EP(X):
    return ApplyPermutation(X, EP_data);

def EPinv(X):
    return ApplyPermutation(X, EPinv_data);

def P4(X):
    return ApplyPermutation(X, P4_data);

def SW(X):
    return ApplyPermutation(X, SW_data);

def LS1(X):
    return ApplyPermutation(X, LS1_data);

def LS2(X):
    return ApplyPermutation(X, LS2_data);

#
# These two functions perform the SBox substitutions
#

def S0(X):
    return ApplySBox(X, S0_data);

def S1(X):
    return ApplySBox(X, S1_data);

def concatenate(left, right):
    r"""
    Joins to bit lists together.
    """
    ret = [left[j] for j in xrange(len(left))];
    ret.extend(right);
    return ret;

def LeftHalfBits(block):
    r"""
    Returns the left half bits from block.
    """
    l = len(block);
    return [block[j] for j in xrange(l/2)];

def RightHalfBits(block):
    r"""
    Returns the right half bits from block.
    """

    l = len(block);
    return [block[j] for j in xrange(l/2, l)];

def XorBlock(block1, block2):
    r"""
    Xors two blocks together.
    """

    l = len(block1);
    if (l != len(block2)):
        raise ValueError, "XorBlock arguments must be same length"
    return [(block1[j]+block2[j]) % 2 for j in xrange(l)];

def SDESKeySchedule(K):
    r"""
    Expands an SDES Key (bit list) into the two round keys.
    """

    temp_K = P10(K);
    left_temp_K = LeftHalfBits(temp_K);
    right_temp_K = RightHalfBits(temp_K);
    K1left = LS1(left_temp_K);
    K1right = LS1(right_temp_K);
    K1temp = concatenate(K1left, K1right);
    K1 = P8(K1temp);
    K2left = LS2(K1left);
    K2right = LS2(K1right);
    K2temp = concatenate(K2left, K2right);
    K2 = P8(K2temp);

    return (K1, K2);

def f_K(block, K):
    r"""
    Performs the f_K function supplied block and K. 
    """

    left_block = LeftHalfBits(block);
    right_block = RightHalfBits(block);
    temp_block1 = EP ...
(more)
2013-04-03 16:48:04 +0200 commented answer pretty dumb question but I don't know

yeah u find the point!!

2013-04-03 16:48:04 +0200 commented answer pretty dumb question but I don't know

yeah u find the point!! I'm really new to python and sage. Don't know that form bring much influence to the result, thank you very much

2013-04-03 16:47:35 +0200 received badge  Supporter (source)
2013-04-03 15:37:54 +0200 asked a question pretty dumb question but I don't know
en_alphabet = "abcdefghijklmnopqrstuvwxyz"
def is_alphabetic_char(c):
    return (c.lower() in en_alphabet)
def char_to_num(c):
    return en_alphabet.index(c.lower())
def num_to_char(x):
    return en_alphabet[x % 26]

def CaesarEncrypt(k, plaintext):
    ciphertext=""
    for j in xrange(len(plaintext)):
        p= plaintext[j]
        if is_alphabetic_char(p):
            x= (k + char_to_num(p))%26
            c= num_to_char(x)
        else:
            c = p
            ciphertext += c
        return ciphertext

(but when I enter

k=16 ;plaintext = 'test.'
CaesarEncrypt(k,plaintext)

)

why does it reply me ' ' but ciphertext......