Does UDP checksum detect all errors? - udp

I just learned about the udp checksum calculation.
But I am confused does the algorithm detect all errors.

Of course not. No checksum can detect all errors.

The UDP checksum cannot detect all errors, but it does detect many. It will detect any single bit flip, but if the packet is altered such that the sum of all the data as 16 bit values remains constant, the checksum will not detect the error.

Usually checksums can detect only the most common errors, but not all of them. Actually, the UDP checksum is optional in combination with IPv4 as UDP is designed as an unreliable service.

No, It can not detect all errors.
Suppose we have two 16-bit numbers
A = 0101 1001 1010 0010 and
B = 1010 0100 0110 0101 then Their sum is :
S = 1111 1110 0000 0111 and 1's(Ones) Complement of this checksum is
C = 0000 0001 1111 1000
and this checksum will be filled in the checksum field of UDP Segment.
So Sender will send these 3 16-bit numbers and at receiver sum of A and B will again be calculated at receiver and added to UDP Checksum received .
If we have Sum 1111 1111 1111 1111 then we will think there is no error in recieved segment.
But here is the catch that if
Last two bits in A i.e 10 Get flipped to 01 and similarly
Last two bits in B i.e 01 Get flipped to 10 . Then again S will remain same and S + C will be again 1111 1111 1111 1111. Which is definately a problem as this will not catch error if More than one bit gets flipped.
So, UDP Checksum cannot detect errors of all kinds.

Related

How do you calculate the key space of an English monoalphabetic cipher where no letters are in the original position?

I've just started learning about cryptography and learned that the keyspace of a monoalphabetic cipher is the result of a permutation 26x25x24...x3x2x1. But that takes into account keys where cipher letters can match the original plaintext letter. If I don't want any letters to represent the original letter, how do I calculate the new keyspace? Maybe I'm naive, but is it as simple as starting the permutation from 25 instead of 26 or is there some other method needed to calculate the total possible keys? I've tried to find the answer on Google and Stackoverflow but I couldn't find it. I apologize if this is a stupid question.
This is called a derangement:
In combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no element appears in its original position. In other words, a derangement is a permutation that has no fixed points.
The operation is written !n and is given by the equation !n=(n-1)(!(n-1)+!(n-2)), where !0 is 1 and !1 is 0. For example, you could write the following Ruby code to do it:
#!/usr/bin/env ruby
def derangements(n)
case n
when 0
1
when 1
0
else
(n - 1) * (derangements(n - 1) + derangements(n - 2))
end
end
puts derangements(ARGV[0].to_i)
Running this code with 26 gives us 148 362 637 348 470 135 821 287 825. Such a cipher has a little more than 86 bits of entropy, but would quickly fall to letter frequency cryptanalysis.

Verifying Checksum value through Wireshark

I'm trying to verify the validity of a checksum value of a UDP packet by checking the packet with Wireshark.
In this specific packet I'm looking at, the values of the UDP headers are as follows:
Source port: 53 (0000 0000 0011 0101)
Destination port: 64992 (1111 1101 1110 0000)
Length: 64 (0000 0000 0100 0000)
Now if these values are added, the sum is 65109 (1111 1110 0101 0101)
So I expect the checksum value to be 426 (0001 1010 1010) which is 1's complement of the sum.
But in Wireshark, the checksum value is 0x63c7, and it says that this checksum is correct.
I'd like to know where I'm mistaken.
Any help or push in the right direction would be greatly appreciated.
Thanks in advance.
If you reference RFC 768, you will find the details you need to properly compute the checksum:
Checksum is the 16-bit one's complement of the one's complement sum of a
pseudo header of information from the IP header, the UDP header, and the
data, padded with zero octets at the end (if necessary) to make a
multiple of two octets.
The pseudo header conceptually prefixed to the UDP header contains the
source address, the destination address, the protocol, and the UDP
length. This information gives protection against misrouted datagrams.
This checksum procedure is the same as is used in TCP.
0 7 8 15 16 23 24 31
+--------+--------+--------+--------+
| source address |
+--------+--------+--------+--------+
| destination address |
+--------+--------+--------+--------+
| zero |protocol| UDP length |
+--------+--------+--------+--------+
If the computed checksum is zero, it is transmitted as all ones (the
equivalent in one's complement arithmetic). An all zero transmitted
checksum value means that the transmitter generated no checksum (for
debugging or for higher level protocols that don't care).
If you want to see how Wireshark's UDP dissector handles it, you can look at the source code for packet-udp.c. Basically, after setting up the data inputs properly, it essentially just calls the in_cksum() function in the in_cksum.c file to compute it.
You might also want to take a look at RFC 1071, "Computing the Internet Checksum".

How to calculate dPermissiions parameter in the Ghostscript command line?

I am looking for an online calculator, a tool or at least an understandable article, which lets me define the value of dPermissiions parameter of Ghostscript command line.
Please, advice!
Its documented in the VectorDevices.htm, where it says its a bit field and directs you to the PDF Reference Manual. The actual values are defined by Adobe.
The various access permissions are described under the Standard Security Handler (on p121 of the 1.7 PDF Reference) and the individual bits are described in Table 3.20 (p124 and 124 in the 1.7 PDF Reference Manual).
Bits 1 and 2 (the lowest 2 bits) are always defined as 0, as (currently) are bits 13-32. Bits 7 & 8, annoyingly are reserved and must be 1.
So lets say you want to grant permission to print the document, to do that you need to set bit 3. So bits 1-2 are 0 and bits 4-32 are also 0, bits 7 and 8 must be 1. In binary that corresponds to:
00000000 00000000 00000000 11000100
Which in hex is 00 00 00 C4 which in decimal is 196. So you would set -dPermissions=196
To take a more complex example, we might also want to set bit 12 to allow a high quality print (for revision 3 or better of the security handler). Now we want to set bits 3 and 12, in binary:
00000000 00000000 00001000 11000100
in hex 00 00 08 C4 which is decimal 2244 so you would set -dPermissions=2244
The Windows calculator, when set to programmer mode, has a binary entry configuration. If you enter the bitfield in binary, and then switch to decimal it'll convert it for you. Alternatively there's an online conversion tool here.
Just write out the bits you want set as binary, set bits 7 & 8, then convert to decimal, simples!
--EDIT--
So as Vsevolod Azovsky pointed out, the bits 12-32 should be 1. Using the same tool I pointed at above you can get the decimal signed 2's complement of the binary representation, which you can use as the value for Permissions.
However, if you do that, then Ghostscript's pdfwrite device will produce a warning. The reason is that some of the bits I've set above (anything above bit 8) are only compatible with the revision 3 (or better) security handler, and the default for pdfwrite is to use the revision 2 security encryption.
So if you want to use the bits marked in the Adobe documentation as 'revision 3' then you (obviously) need to set the revision to 3 using -dEncryptionR=3. This requires that the output PDF file be a 1.4 or greater file, you can't use revision 3 with a PDF 1.3 file.
Note that for the revision 2 security handler all the bits 1-2 and 7-32 must be 1.
Hopefully that also answers the questions in the last comment.

How to set different bits in VBA

I need to use bit operations in VBA. For example I want to set first bit in 0 second bit in 1 and so on. How can I do it?
Thanks a lot!
You could use masks.
If you want to set* the n-th bit, you can perform an Or operation of your original value and a mask filled with 0 except for the n-th position.
That way, 1100 Or 0001 will set the first bit resulting in 1101. 1100 Or 0010 will set the second bit, resulting in 1110.
If you want to unset* the n-th position, you can do the opposite. Perform an And operation on your original value with a mask filled with 1 except the n-th position.
That way, 0011 And 1110 will unset the first bit resulting in 0010, 0011 And 1101 will unset the second bit, resulting in 0001.
* Set means to turn the bit into 1. Unset means to turn the bit into 0.

Convert 2 word16 to doubleword

A little background: I am writing a Visual Basic application. It will connect to an Omron Programmable Logic Controller (PLC).
When I read data from the PLC, they come as WORDs (16bits). However the PLC programmer needs to have a double-word (32bits) for a big number (bigger than what 16bits can hold). I need to be able to show that number on the screen. As mentioned, I read from the PLC as WORDs. So I can make 2 reads to get the 2 words. However they are separate.
Here's what it is: I need to show 120,000 on the screen (VB app). on the PLC, I read 2 words (in bit form):
Word#1: 1101 0100 1100 0000 (Unsigned, this equals 54464)
Word#2: 0000 0000 0000 0001
I need to put these together like this: 0000 0000 0000 0001 1101 0100 1100 0000 = 120,000
Are there any built in functions in Visual Basic that will combine these two 16bit words into into 1 Double-word? If so what is it?
Or do I have to write a function to put these two values together?
Or has someone done something like this and can provide some info for me?
I found the << and >> operands. They shift bits left and right. so I used the formula (Word2 << 16) to generate the value of Word2. then added the value of Word1.
In VBA if you want to concatenate two strings together all you need to do is use the & operator. For example:
Dim TempCombinedWord as String
TempCombinedWord = FirstWord & LastWord
If FirstWord contained Happy and LastWord contained TreeFriends. Then TempCombinedWord would then contain HappyTreeFriends.