<s>
In	O
cryptography	O
,	O
ciphertext	B-Algorithm
stealing	I-Algorithm
(	O
CTS	O
)	O
is	O
a	O
general	O
method	O
of	O
using	O
a	O
block	B-Algorithm
cipher	I-Algorithm
mode	I-Algorithm
of	I-Algorithm
operation	I-Algorithm
that	O
allows	O
for	O
processing	O
of	O
messages	O
that	O
are	O
not	O
evenly	O
divisible	O
into	O
blocks	O
without	O
resulting	O
in	O
any	O
expansion	O
of	O
the	O
ciphertext	O
,	O
at	O
the	O
cost	O
of	O
slightly	O
increased	O
complexity	O
.	O
</s>
<s>
Ciphertext	B-Algorithm
stealing	I-Algorithm
is	O
a	O
technique	O
for	O
encrypting	O
plaintext	O
using	O
a	O
block	O
cipher	O
,	O
without	O
padding	B-Algorithm
the	O
message	O
to	O
a	O
multiple	O
of	O
the	O
block	O
size	O
,	O
so	O
the	O
ciphertext	O
is	O
the	O
same	O
size	O
as	O
the	O
plaintext	O
.	O
</s>
<s>
In	O
principle	O
any	O
block-oriented	O
block	B-Algorithm
cipher	I-Algorithm
mode	I-Algorithm
of	I-Algorithm
operation	I-Algorithm
can	O
be	O
used	O
,	O
but	O
stream-cipher-like	O
modes	O
can	O
already	O
be	O
applied	O
to	O
messages	O
of	O
arbitrary	O
length	O
without	O
padding	B-Algorithm
,	O
so	O
they	O
do	O
not	O
benefit	O
from	O
this	O
technique	O
.	O
</s>
<s>
The	O
common	O
modes	B-Algorithm
of	I-Algorithm
operation	I-Algorithm
that	O
are	O
coupled	O
with	O
ciphertext	B-Algorithm
stealing	I-Algorithm
are	O
Electronic	O
Codebook	O
(	O
ECB	O
)	O
and	O
Cipher	O
Block	O
Chaining	O
(	O
CBC	O
)	O
.	O
</s>
<s>
Ciphertext	B-Algorithm
stealing	I-Algorithm
for	O
ECB	O
mode	O
requires	O
the	O
plaintext	O
to	O
be	O
longer	O
than	O
one	O
block	O
.	O
</s>
<s>
A	O
possible	O
workaround	B-Application
is	O
to	O
use	O
a	O
stream	O
cipher-like	O
block	B-Algorithm
cipher	I-Algorithm
mode	I-Algorithm
of	I-Algorithm
operation	I-Algorithm
when	O
the	O
plaintext	O
length	O
is	O
one	O
block	O
or	O
less	O
,	O
such	O
as	O
the	O
CTR	O
,	O
CFB	O
or	O
OFB	O
modes	O
.	O
</s>
<s>
Ciphertext	B-Algorithm
stealing	I-Algorithm
for	O
CBC	O
mode	O
does	O
n't	O
necessarily	O
require	O
the	O
plaintext	O
to	O
be	O
longer	O
than	O
one	O
block	O
.	O
</s>
<s>
In	O
the	O
case	O
where	O
the	O
plaintext	O
is	O
one	O
block	O
long	O
or	O
less	O
,	O
the	O
Initialization	B-Algorithm
vector	I-Algorithm
(	O
IV	O
)	O
can	O
act	O
as	O
the	O
prior	O
block	O
of	O
ciphertext	O
.	O
</s>
<s>
This	O
may	O
not	O
be	O
possible	O
in	O
situations	O
where	O
the	O
IV	O
can	O
not	O
be	O
freely	O
chosen	O
by	O
the	O
sender	O
when	O
the	O
ciphertext	O
is	O
sent	O
(	O
e.g.	O
,	O
when	O
the	O
IV	O
is	O
a	O
derived	O
or	O
pre-established	O
value	O
)	O
,	O
and	O
in	O
this	O
case	O
ciphertext	B-Algorithm
stealing	I-Algorithm
for	O
CBC	O
mode	O
can	O
only	O
occur	O
in	O
plaintexts	O
longer	O
than	O
one	O
block	O
.	O
</s>
<s>
This	O
results	O
in	O
the	O
final	O
block	O
not	O
being	O
aligned	B-Application
on	O
a	O
natural	O
boundary	O
,	O
complicating	O
hardware	O
implementations	O
.	O
</s>
<s>
This	O
does	O
have	O
the	O
advantage	O
that	O
,	O
if	O
the	O
final	O
plaintext	O
block	O
happens	O
to	O
be	O
a	O
multiple	O
of	O
the	O
block	O
size	O
,	O
the	O
ciphertext	O
is	O
identical	O
to	O
that	O
of	O
the	O
original	O
mode	B-Algorithm
of	I-Algorithm
operation	I-Algorithm
without	O
ciphertext	B-Algorithm
stealing	I-Algorithm
.	O
</s>
<s>
This	O
results	O
in	O
naturally	O
aligned	B-Application
ciphertext	O
blocks	O
.	O
</s>
<s>
This	O
maintains	O
natural	O
alignment	O
,	O
and	O
compatibility	O
with	O
the	O
non-stealing	O
modes	O
,	O
but	O
requires	O
treating	O
the	O
cases	O
of	O
aligned	B-Application
and	O
unaligned	O
message	O
size	O
differently	O
.	O
</s>
<s>
In	O
order	O
to	O
encrypt	O
or	O
decrypt	O
data	O
,	O
use	O
the	O
standard	O
block	B-Algorithm
cipher	I-Algorithm
mode	I-Algorithm
of	I-Algorithm
operation	I-Algorithm
on	O
all	O
but	O
the	O
last	O
two	O
blocks	O
of	O
data	O
.	O
</s>
<s>
XOR	B-Application
:	O
Bitwise	O
Exclusive-OR	O
.	O
</s>
<s>
Ciphertext	B-Algorithm
stealing	I-Algorithm
in	O
ECB	O
mode	O
introduces	O
an	O
inter-block	O
dependency	O
within	O
the	O
last	O
two	O
blocks	O
,	O
resulting	O
in	O
altered	O
error	O
propagation	O
behavior	O
for	O
the	O
last	O
two	O
blocks	O
.	O
</s>
<s>
Xn−1	O
=	O
Pn−1	O
XOR	B-Application
Cn−2	O
.	O
</s>
<s>
The	O
zero	O
padding	B-Algorithm
in	O
this	O
step	O
is	O
important	O
for	O
step	O
5	O
.	O
</s>
<s>
Dn	O
=	O
En−1	O
XOR	B-Application
P	O
.	O
Exclusive-OR	O
En−1	O
with	O
P	O
to	O
create	O
Dn	O
.	O
</s>
<s>
The	O
zero	O
padding	B-Algorithm
of	O
P	O
in	O
step	O
4	O
was	O
important	O
,	O
because	O
it	O
makes	O
the	O
XOR	B-Application
operation	O
's	O
effect	O
on	O
the	O
last	O
B−M	O
bits	O
equivalent	O
to	O
copying	O
the	O
last	O
B−M	O
bits	O
of	O
En−1	O
to	O
the	O
end	O
of	O
Dn	O
.	O
</s>
<s>
We	O
are	O
padding	B-Algorithm
Cn	O
with	O
zeros	O
to	O
help	O
in	O
step	O
3	O
.	O
</s>
<s>
Xn	O
=	O
Dn	O
XOR	B-Application
C	O
.	O
Exclusive-OR	O
Dn	O
with	O
C	O
to	O
create	O
Xn	O
.	O
</s>
<s>
Looking	O
at	O
the	O
first	O
M	O
bits	O
,	O
this	O
step	O
has	O
the	O
result	O
of	O
XORing	O
Cn	O
(	O
the	O
first	O
M	O
bits	O
of	O
the	O
encryption	O
process	O
 '	O
En−1	O
)	O
with	O
the	O
(	O
now	O
decrypted	O
)	O
Pn	O
XOR	B-Application
Head	O
(	O
En−1	O
,	O
M	O
)	O
(	O
see	O
steps	O
4-5	O
of	O
the	O
encryption	O
process	O
)	O
.	O
</s>
<s>
Pn−1	O
=	O
Xn−1	O
XOR	B-Application
Cn−2	O
.	O
</s>
<s>
Finally	O
,	O
we	O
reverse	O
the	O
XOR	B-Application
step	O
from	O
step	O
1	O
of	O
the	O
encryption	O
process	O
.	O
</s>
<s>
For	O
CBC	O
ciphertext	B-Algorithm
stealing	I-Algorithm
,	O
there	O
is	O
a	O
clever	O
(	O
but	O
opaque	O
)	O
method	O
of	O
implementing	O
the	O
described	O
ciphertext	B-Algorithm
stealing	I-Algorithm
process	O
using	O
a	O
standard	O
CBC	O
interface	O
.	O
</s>
