Serpent Sbox transformation - cryptography

Consider I have this sample key
15FC0D48 D7F8199C BE399183 4D96F327 10000000 00000000 00000000 00000000
w-8 w-7 w-6 w-5 w-4 w-3 w-2 w-1
Creating first K0 Key Schedule pair
(k0,k1,k2,k3)=S3(w0,w1,w2,w3)=K0
using this formula for Serpent Key Schdule
wi=(wi-8 xor wi-5 xor wi-3 xor wi-1 xor phi xor i)<<<11
1) i=0
w0=w-8 xor w-5 xor w-3 xor w-1 xor 9e3779b9 xor 0<<<11=15FC0D48 xor 4D96F327 xor 00000000 xor 00000000 xor 9e3779b9 xor 0 <<<11=EC3EB632
2) i=1
w1=w-7 xor w-4 xor w-2 xor w0 xor 9e3779b9 xor 1<<<11=D7F8199C xor 10000000 xor 00000000 xor EC3EB632 xor 9e3779b9 xor 1<<<11=8EB0B5AF
3) i=2
w2=w-6 xor w-3 xor w-1 xor w1 xor 9e3779b9 xor 2<<<11=BE399183 xor 00000000 xor 00000000 xor 8EB0B5AF xor 9e3779b9 xor 2<<<11=F2ECBD75
4) i=3
w3=w-5 xor w-2 xor w0 xor w2 xor 9e3779b9 xor 3<<<11=4D96F327 xor 00000000 xor EC3EB632 xor F2ECBD75 xor 9e3779b9 xor 3<<<11=9C0ED66B
For k0 will be created by Sbox3
S3(w0,w1,w2,w3)
EC3EB632
binary form:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
11|10|11|00|00|11|11|10|10|11|01|10|00|11|00|10|
after Sbox3
11101010001111001110110001101100
EA3CEC6C
8EB0B5AF
binary form:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
10|00|11|10|10|11|00|00|10|11|01|01|10|10|11|11
after Sbox3
10110110101100101000111001001111
B6B28E4F
F2ECBD75
binary form:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
11|11|00|10|11|10|11|00|10|11|11|01|01|11|01|01
after Sbox3
11010110011111101111001111001001
D67EF3C9
9C0ED66B
binary form:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
10|01|11|00|00|00|11|10|11|01|01|10|01|10|10|11
after Sbox3
10111011010111001001110001100010
BB5C9C62
k0={EA3CEC6C B6B28E4F D67EF3C9 BB5C9C62}
I use my solution in C# for SBOX transformation:
String key="10011100000011101101011001101011";
prek="";
Uint32 k;
for (int i = sboxpos; i < sboxpos + 1; i++)
{
for (int i2 = 0; i2 < 16; i2++)
{
prek += key.Substring(sbox[i, i2] * 2, 2); //Multiplying on 2 to move byte on appropriate position, as in my example above I transform hex to binary it have 32bits, but Sbox'es moves only bytes. So e.g. the 15's byte is 15*2=30's position plus 2 characters
}
}
k = Convert.ToUInt32(prek, 2);
My question is am I doing Key schedule of Serpent right, as in other implementation they uses an &, operands in Sbox'es. Will I have same result by applying Sbox transformation in my code above? Thanks!

Related

Breaking a XOR with repeating key and counter

I was given an exercise where I have to break a xor with repeating key and counter.
I don't know the key nor its length, nor the value of the counter.
I have:
PT1 xor K = C1
PT2 xor K = C2
PT3 xor K = C3
So:
C1 xor C2 = PT1 xor PT2 xor K xor K = PT1 xor PT2 xor 0
So:
0 xor C1 xor C2 = PT1 xor PT2
0 xor C1 xor C3 = PT1 xor PT3
0 Xor C2 xor C3 = PT2 xor PT3
Where K is key, C is cypher and PT is Plain Text.
I don't know what to do with that. Could you please give me a hint? :)
Here is a solution that satisfies your conditions from with your question
PT1(5) K(9) C1(12)
PT2(7) K(9) C2(14)
PT3(9) K(9) C3(0)
0 xor C1 xor C2 = PT1 xor PT2
0 xor C1 xor C3 = PT1 xor PT3
0 Xor C2 xor C3 = PT2 xor PT3
So in the case i built:
0 xor C1(12) xor C2(14) = PT1(5) xor PT2(7)
0 xor C1(12) xor C3(0) = PT1(5) xor PT3(9)
0 Xor C2(14) xor C3(0) = PT2(7) xor PT3(9)
In weak cases you can get K like this:
You can get K like this:
In [519]: 1^5
Out[519]: 4
In [520]: 2^5
Out[520]: 7
In [521]: 3^5
Out[521]: 6
In [522]: 6^7^4
Out[522]: 5
So you can crack the key by xoring the ciphers together if a weak key to xor cipher is used ( Does not work in all cases only very rare one ). But does that help any for helping you solve the problem?
PT1 xor K = C1 if c1 is 4
PT2 xor K = C2 if c2 is 7
PT3 xor K = C3 if c2 is 6
You know that K is 5. From there you can get PT by working the XOR backwards:
c1 (4) ^ K (5) = PT1
c2 (7) ^ K (5) = PT2
c3 (6) ^ K (5) = PT3
So to recap, XOR c1^c2^c3 to get K, and from there you can get the corresponding PT(1-3). But this only works in some rare cases, but does it help?
If you have any of the values, let me know. I'll try to recreate something from scratch
Here's a case where i XORED the C1, C2, C3 together to get a key, and then PT1-3 just fill in to work
PT1(10) K(12) C1(6)
PT2(4) K(12) C2(8)
PT3(14) K(12) C3(2)
0 xor C1(6) xor C2(8) = PT1(10) xor PT2(4)
0 xor C1(6) xor C3(2) = PT1(10) xor PT3(14)
0 Xor C2(8) xor C3(2) = PT2(4) xor PT3(14)
It really depends on how your assignment is expecting you to get the key. I hope all of this helps, encryption is fun. I provided a few paths, hopefully one will help.

numericupdown shows wrong value

I use a numericupdown set min = 0 maximum = 59 and increment = 1
When i check the debugger at
Private Sub Numericsec_ValueChanged(sender As Object, e As System.EventArgs) Handles Numericsec.ValueChanged
unitsec = Convert.ToInt32(DirectCast(sender, NumericUpDown).Value Mod 10)
tensec = Convert.ToInt32(DirectCast(sender, NumericUpDown).Value / 10)
when value is 5 then
unitsec = 5
tensec = 0
but when value is 6
unitsec = 5
tensec = 1 ????
Thanks
There's nothing special about NumericUpDown.Value; it's just a Decimal. Test this code and confirm that you get the same results. It might just be an issue with understanding the various arithmetic and conversions involved.
Test code:
Console.WriteLine($"Convert.ToInt32(5D Mod 10): {Convert.ToInt32(5D Mod 10)}")
Console.WriteLine($"Convert.ToInt32(5D / 10): {Convert.ToInt32(5D / 10)}")
Console.WriteLine($"Convert.ToInt32(6D Mod 10): {Convert.ToInt32(6D Mod 10)}")
Console.WriteLine($"Convert.ToInt32(6D / 10): {Convert.ToInt32(6D / 10)}")
Console.WriteLine($"Convert.ToInt32(6D) \ 10: {Convert.ToInt32(6D) \ 10}")
Console.WriteLine($"Convert.ToInt32(5D) \ 10: {Convert.ToInt32(5D) \ 10}")
Output:
Convert.ToInt32(5D Mod 10): 5
Convert.ToInt32(5D / 10): 0
Convert.ToInt32(6D Mod 10): 6
Convert.ToInt32(6D / 10): 1
Convert.ToInt32(6D) \ 10: 0
Convert.ToInt32(5D) \ 10: 0
Explanation:
5D Mod 10: No secret here. 5 Mod 10 is 5. Conversion does nothing
5D / 10: Convert.ToInt32 uses banker's rounding i.e. midpoint rounding goes to the nearest even number
6D Mod 10: Again, no secret here
6D / 10: Here it might be confusing because now you have 0.6 and it is rounded to the nearest integer. Midpoint rounding down to 0 doesn't apply because it is > the midpoint.
5D \ 10, 6D \ 10: Integer division discards decimals. Both result in 0.

Display commend in ampl

I have a 2 dimension variable in ampl and I want to display it. I want to change the order of the indices but I do not know how to do that! I put my code , data and out put I described what kind of out put I want to have.
Here is my code:
param n;
param t;
param w;
param p;
set Var, default{1..n};
set Ind, default{1..t};
set mode, default{1..w};
var E{mode, Ind};
var B{mode,Var};
var C{mode,Ind};
param X{mode,Var,Ind};
var H{Ind};
minimize obj: sum{m in mode,i in Ind}E[m,i];
s.t. a1{m in mode, i in Ind}: sum{j in Var} X[m,j,i]*B[m,j] -C[m,i] <=E[m,i];
solve;
display C;
data;
param w:=4;
param n:=9;
param t:=2;
param X:=
[*,*,1]: 1 2 3 4 5 6 7 8 9 :=
1 69 59 100 70 35 1 1 0 0
2 34 31 372 71 35 1 0 1 0
3 35 25 417 70 35 1 0 0 1
4 0 10 180 30 35 1 0 0 0
[*,*,2]: 1 2 3 4 5 6 7 8 9 :=
1 64 58 68 68 30 2 1 0 0
2 44 31 354 84 30 2 0 1 0
3 53 25 399 85 30 2 0 0 1
4 0 11 255 50 30 2 0 0 0
The output of this code using glpksol is like tis:
C[1,1].val = -1.11111111111111
C[1,2].val = -1.11111111111111
C[2,1].val = -0.858585858585859
C[2,2].val = -1.11111111111111
C[3,1].val = -0.915032679738562
C[3,2].val = -1.11111111111111
C[4,1].val = 0.141414141414141
C[4,2].val = 0.2003367003367
but I want the result to be like this:
C[1,1].val = -1.11111111111111
C[2,1].val = -0.858585858585859
C[3,1].val = -0.915032679738562
C[4,1].val = 0.141414141414141
C[1,2].val = -1.11111111111111
C[2,2].val = -1.11111111111111
C[3,2].val = -1.11111111111111
C[4,2].val = 0.2003367003367
any idea?
You can use for loops and printf commands in your .run file:
for {i in Ind}
for {m in mode}
printf "C[%d,%d] = %.4f\n", m, i, C[m,i];
or even:
printf {i in Ind, m in mode} "C[%d,%d] = %.4f\n", m, i, C[m,i];
I don't get the same numerical results as you, but anyway the output works:
C[1,1] = 0.0000
C[2,1] = 0.0000
C[3,1] = 0.0000
C[4,1] = 0.0000
C[1,2] = 0.0000
C[2,2] = 0.0000
C[3,2] = 0.0000
C[4,2] = 0.0000

correlations across columns AWK

I need to calculate correlations across columns.
The code below works when calculating correlations across rows.
What is needed to modify to calculate across columns?
Input file:
Name C1 C2 C3 C4 C5 C6
R1 1 2 3 4 5 6
R2 2 1 1 0 1 0
R3 1 3 1 1 2 1
R4 1 1 0 2 0 1
R5 1 2 2 2 0 2
R6 1 1 0 1 2 0
Desired Output:
C1 C1 1.00
C1 C2 -0.4
C1 C3 -0.069
C1 C4 -0.597
C1 C5 -0.175
C1 C5 -0.362
C2 C2 1.00
C2 C3 0.4889
etc.
Code:
awk '{
a = 0; for (i = 2; i <= NF; ++i) a += $i; a /= NF-1
b = 0; for (i = 2; i <= NF; ++i) b += ($i - a) ^ 2; b = sqrt(b)
if (b <= 0) next
for (i = 2; i <= NF; ++i) x[NR, i] = ($i - a) / b
n[NR] = $1
for (i = 2; i <= NR; ++i) {
if (!(i in n)) continue
a = 0
for (k = 2; k <= NF; ++k)
a += x[NR, k] * x[i, k]
print n[NR], n[i], a
}}'
Don't know if looking for this kind solution, but how about to transpose first with following awk:
awk '
{ for (i=1;i<=NF;i++) arr[i","NR]=$i; }
END {
for (i=1;i<=NF;i++) {
for (j=1;j<=NR;j++) printf("%s%s",arr[i","j],FS);
printf("%s",RS);
}
}
'
Output:
Name R1 R2 R3 R4 R5 R6
C1 1 2 1 1 1 1
C2 2 1 3 1 2 1
C3 3 1 1 0 2 0
C4 4 0 1 2 2 1
C5 5 1 2 0 0 2
C6 6 0 1 1 2 0
Then just combine with Your script to calculate column-column correlations:
awk '
{ for (i=1;i<=NF;i++) arr[i","NR]=$i; }
END {
for (i=1;i<=NF;i++) {
for (j=1;j<=NR;j++) printf("%s%s",arr[i","j],FS);
printf("%s",RS);
}
}
' roddy.txt | awk '{
a = 0; for (i = 2; i <= NF; ++i) a += $i; a /= NF-1
b = 0; for (i = 2; i <= NF; ++i) b += ($i - a) ^ 2; b = sqrt(b)
if (b <= 0) next
for (i = 2; i <= NF; ++i) x[NR, i] = ($i - a) / b
n[NR] = $1
for (i = 2; i <= NR; ++i) {
if (!(i in n)) continue
a = 0
for (k = 2; k <= NF; ++k)
a += x[NR, k] * x[i, k]
print n[NR], n[i], a
}}'
Output:
C1 C1 1
C2 C1 -0.4
C2 C2 1
C3 C1 -0.069843
C3 C2 0.488901
C3 C3 1
C4 C1 -0.597614
C4 C2 0.239046
C4 C3 0.667827
C4 C4 1
C5 C1 -0.175412
C5 C2 0.30697
C5 C3 0.581936
C5 C4 0.576557
C5 C5 1
C6 C1 -0.362738
C6 C2 0.362738
C6 C3 0.861381
C6 C4 0.932143
C6 C5 0.731727
C6 C6 1

Reading 2 numbers in assembly and storing it in a variable

just to put it out there -I really did search and try to find an easy to understand tutorial but couldn't :/ .. I'm still kind of new to it.
But what I'm struggling with is to input 2 numbers from the user and to store it in a variable . And then later compare it to a Maximum value.
output db 10,13, "Enter a number: $"
asq dw 10,13, "$" ; ascii values for a new line
stilltoimplement db 10,13, "Do calculations $"
.code
jmp start
number db ?
max db 14
start:
mov ax,#data
mov ds,ax
mov ah,09 ;prints new line
mov dx, offset output
int 21h
mov ah, 01 ;checks for key
mov al, 01
int 21h
mov number, al
cmp number,14
jg start
jl part2
part2:
mov ah,09
mov dx,offset stilltoimplement
int 21h
ending:
mov ah,4ch
mov al,00
int 21h ;End the program
END
If you want to add multiple-digit numbers, you could store the digits
in a register by multiplying the current number by 10 on a digit
input.
I would guess that you are on x86 so you have 6 general purpose
registers.
In assembler-like pseudocode:
register a = 0
register b = 0
register c = 0
num1:
c = getDigit()
if c is a delimiter/sentinel value jump to num2
a = a * 10
a = a + c
jump to num1
num2:
c = getDigit()
if c is a delimiter/sentinel value jump to sum
b = b * 10
b = b + c
jump to num2
sum:
a = a + b
and your sum is now in a