Breaking a XOR with repeating key and counter - cryptography

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.

Related

Extract colors from a gradient [VBA]

I´m currently working on a small tool for powerpoint to make certain processes easier and one of them is to create a gradient with 2 stops and then extract the colors inbetween so that I can simply create a gradient in powerpoint on a shape, select it, choose how many colors should be generated out of the gradient and then create a group of shapes with the individual colors.
However creating the shapes with the colors is not an issue so I would simply like to know whether or not there is any way to extract the colors as described and perhaps how I would achieve it.
Thanks in advance
(Edit : I was able to resolve the issue myself. The code is attached in my answer)
Alright, I was able to find a solution myself by breaking down both colors into the respective values and then increase/decrease each value by a percentage of the difference between both colors. I attached the code for anyone interested.
Sub extractGradient()
Dim sld As Slide
Dim nShape As shape
Dim c1, c2, r1, g1, b1, r2, g2, b2, rDiff, gDiff, bDiff, cR, cG, cB As Long
Dim range As Integer
Dim colors As Collection
Set sld = Application.ActiveWindow.View.Slide
Set colors = New Collection
range = 1000
With ActiveWindow.Selection.ShapeRange(1).Fill.ForeColor
c1 = .RGB
r1 = .RGB Mod 256
g1 = .RGB \ 256 Mod 256
b1 = .RGB \ 65536 Mod 256
End With
With ActiveWindow.Selection.ShapeRange(2).Fill.ForeColor
c2 = .RGB
r2 = .RGB Mod 256
g2 = .RGB \ 256 Mod 256
b2 = .RGB \ 65536 Mod 256
End With
rDiff = Abs(r2 - r1)
gDiff = Abs(g2 - g1)
bDiff = Abs(b2 - b1)
colors.Add c1
For i = 1 To range - 1
cR = IIf(r1 > r2, r1 - (rDiff / range * i), r1 + (rDiff / range * i))
cG = IIf(g1 > g2, g1 - (gDiff / range * i), g1 + (gDiff / range * i))
cB = IIf(b1 > b2, b1 - (bDiff / range * i), b1 + (bDiff / range * i))
colors.Add (RGB(cR, cG, cB))
Next i
colors.Add c2
count = 0
For Each c In colors
Set nShape = sld.Shapes.AddShape(Type:=msoShapeRectangle, left:=(1 * count), top:=50, width:=1, height:=50)
nShape.Fill.ForeColor.RGB = c
nShape.Line.Visible = msoFalse
count = count + 1
Next c
End Sub

Why on my code that checks for permutations, Characters also permute with themselves?

So my code searches for permutations from a random given String, and checks some .txt dictionary files (which are loaded in arrays) to see the words that can be made with the random given letters. But my code also makes them permute with themselves. For instance if i put "ab" it should make these permutations "ab" and "ba". INstead it makes "aa", "ab", "ba" and "bb". Any ideas? (the given code is for words until length 3)
If TextBox1.Text.Length > 1 Then
For Each c0 As Char In chars
For Each c1 As Char In chars
For i As Integer = 0 To Rank2.Length - 1
test = Rank2(i)
If InStr(Rank2(i), c0 & c1) Then
RankBox2.Items.Add(test)
End If
Next
Next
Next
End If
If TextBox1.Text.Length > 2 Then
For Each c0 As Char In chars
For Each c1 As Char In chars
For Each c2 As Char In chars
For i As Integer = 0 To Rank3.Length - 1
test = Rank3(i)
If InStr(Rank3(i), c0 & c1 & c2) Then
RankBox3.Items.Add(test)
End If
Next
Next
Next
Next
End If
If TextBox1.Text.Length > 3 Then
For Each c0 As Char In chars
For Each c1 As Char In chars
For Each c2 As Char In chars
For Each c3 As Char In chars
For i As Integer = 0 To Rank4.Length - 1
test = Rank4(i)
If InStr(Rank4(i), c0 & c1 & c2 & c3) Then
RankBox4.Items.Add(test)
End If
Next
Next
Next
Next
Next
End If
If the goal is exclude the arrangement of characters that repeat themselves, then you would need to add a check before processing the code inside the for loops.
For example..
If TextBox1.Text.Length > 1 Then
For Each c0 As Char In chars
For Each c1 As Char In chars
If c0 <> c1 Then
For i As Integer = 0 To Rank2.Length - 1
test = Rank2(i)
If InStr(Rank2(i), c0 & c1) Then
RankBox2.Items.Add(test)
End If
Next
End If
Next
Next
End If
Same solution as #RyanRoos, but with indexes. So the compare is with the indexes.
Dim c0 As Char
Dim c1 As Char
If TextBox1.Text.Length > 1 Then
For i = 0 to chars.Length - 1
For j = 0 to chars.Length - 1
If i <> j Then
For k As Integer = 0 To Rank2.Length - 1
test = Rank2(k)
If InStr(Rank2(k), c0 & c1) Then
RankBox2.Items.Add(test)
End If
Next
End If
Next
Next
End If
So if you have ab, it will produce ab and ba
If you have aba, it will produce aab, aba, baa twice each, because the a at first pos has been permuted with a at third pos.

Serpent Sbox transformation

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!

Swap two integers without using a third variable

I have an assignment in which I need to swap two integers without using third variable.
I'm not sure how to do this. How would I code this?
Yes, it's possible:
Dim var1 = 1
Dim var2 = 2
var1 = var1 + var2
var2 = var1 - var2
var1 = var1 - var2
But why do you need it? The code becomes abstruse.
Lets assume
a = 10;
b = 20;
a = a + b; // a = 30
b = a - b; // b = 10
a = a - b; // a = 20
Values swapped.
Read up on the "xor swap algorithm."
You can find an answer here:
http://www.java2s.com/Tutorial/VB/0040__Data-Type/Swaptwointegerswithoutusingathird.htm
firstValue = firstValue Xor secondValue
secondValue = firstValue Xor secondValue
firstValue = firstValue Xor secondValue
Dim a As Integer
Dim b As Integer
a= 1
b= 2
a = a Xor b
b = a Xor b
a = a Xor b
To swap two numeric variables do like this
a = a + b;
b = a - b;
a = a - b;
OR
a = a xor b;
b = a xor b;
a = a xor b;
where a and b are variables to be swapped
theoretically 3 ways
a = 4 , b = 5
1. Using XOR
a = a XOR b = 4 XOR 5 = 9
b = a XOR b = 9 XOR 5 = 4
a = a XOR b = 9 XOR 4 = 5
2. Using +,-
a = a+b = 4+5 = 9 // should not overflow
b = a-b = 9-5 = 4
a = a-b = 9-4 = 5
3. Using *,/
a = a*b = 4*5 = 20 // should not overflow
b = a/b = 20/5 = 4 // should not overflow and should not be irrational number
a = a/b = 20/4 = 5 // should not overflow and should not be irrational number
The Xor or a+b algorithms above work and are the best way to do this, but just an example of a weird way to do it. Still not sure why you would want to do this. Just build a function that you supply two values ByRef and have it do the standard swap method.
Dim newList as New List(Of Integer)
newList.Add firstvalue
newList.Add secondValue
newList.Reverse
secondValue = newList.Item(0)
firstValue = newList.Item(1)
Take two text boxes and a command box.In command box type this code.
text1.text=val(text1.text) + val(text2.text)
text2.text=val(text1.text) - val(text2.text)
text1.text=val(text1.text) - val(text2.text)
Check link written for you
Approach#1.
Addition and Subtraction Method
Integer a, b
read a and b
a= a+b;
b=a-b;
a=a-b;
Problem:
Incorrect result when sum of numbers will exceed the Integer range.
Approach#2.
Multiplication and Division Method
Integer a, b
read a and b
a=a*b;
b=a/b;
a=a/b;
Problems:
If the value of a*b exceeds the range of integer.
If the value of a or b is zero then it will give wrong results.
Approach#3.
XOR Method
Integer a , b
read a and b
a=a^b;
b=a^b;
a=a^b;
Best approach to solve this problem without any pitfalls.

Need Help Vb6 Xor Large Numbers

Dim Key As Long
Packet = txtSend.Text
PacketLength = Len(txtSend.Text)
key = &H82381AC + PacketLength * "17"
For ix = 1 To PacketLength - 1
OneCharacter = Mid$(Packet, ix, 1)
NewCharacter = Asc(OneCharacter) Xor key And &H1F
key = key * "13" Xor &H43B
Next ix
The loop will run about 3 times then overflow. I guess no matter what u do xor will result in Byte,Integer or Long. CDec(Key) will not work I need a way to bypass this so i can Xor large numbers.
You can only XOR integer numbers, not strings. If you want to encrypt a string using XOR, do so by applying XOR character by character to the character codes
Public Function Encrypt(ByVal s As String) As String
Const key As String = "r5^245ADh3%^ywftGY53Gsdr245^Tsfdgw45^fGqw4%6243TefgH563&ot7y"
Dim i As Integer
For i = 1 To Len(s)
Mid(s, i, 1) = Chr(Asc(Mid(s, i, 1)) Xor Asc(Mid(key, i, 1)))
Next i
Return s
End Function