TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Francesco DC
NA
1
2k
Decrypting string with Rijndael
May 16 2012 11:32 PM
Good evening everybody !
I'm trying to decrypt a string crypted with Rijndael algorythm. This type of cryptation apply a padding with "#" on the right of the Key and IV, if they are more than 16 characters long. The string to decrypt is received from a Webservice that sends it, and the Key, to me in XML SOAP Format. The IV is the Mac Address of my machine (that the server use as IV to encrypt the string). When i try to decrypt the received string, my program crash at this instruction:
while
((
num5
=
stream3
.
ReadByte
())
!=
-
1
)
and it give to me this errore "The padding is not valid and it cannot be removed".
I've searched this error on MSDN, and it says that it happen when the IV used to encrypt is different from the IV used to decrypt, but, i repeat, the IV is the MacAddress and it is the same everytime.
This is the sourcecode of Encrypt and Decrypt functions:
public
static
string
Decrypt
(
string
strInputString
,
string
strKeyString
,
string
myIV
)
{
if
((
strInputString
==
null
)
||
(
strInputString
.
Length
==
0
))
{
return
strInputString
;
}
try
{
int
num5
;
int
keySize
=
0x100
;
int
blockSize
=
0x100
;
int
length
=
keySize
/
0x10
;
if
(
strKeyString
.
Length
>
length
)
{
strKeyString
=
strKeyString
.
Substring
(
0
,
length
);
}
if
(
strKeyString
.
Length
<
length
)
{
strKeyString
=
strKeyString
.
PadRight
(
length
,
'#'
);
}
Encoding
.
Unicode
.
GetBytes
(
strKeyString
);
if
(
myIV
.
Length
>
length
)
{
myIV
=
myIV
.
Substring
(
0
,
length
);
}
if
(
myIV
.
Length
<
length
)
{
myIV
=
myIV
.
PadRight
(
length
,
'#'
);
}
Encoding
.
Unicode
.
GetBytes
(
myIV
);
byte
[]
bytes
=
Encoding
.
Unicode
.
GetBytes
(
strKeyString
);
byte
[]
rgbIV
=
Encoding
.
Unicode
.
GetBytes
(
myIV
);
RijndaelManaged
managed
=
new
RijndaelManaged
{
BlockSize
=
blockSize
,
KeySize
=
keySize
};
MemoryStream
stream
=
new
MemoryStream
();
for
(
int
i
=
0
;
i
<
strInputString
.
Length
;
i
+=
2
)
{
stream
.
WriteByte
(
byte
.
Parse
(
strInputString
.
Substring
(
i
,
2
),
NumberStyles
.
AllowHexSpecifier
));
}
stream
.
Position
=
0L
;
MemoryStream
stream2
=
new
MemoryStream
();
CryptoStream
stream3
=
new
CryptoStream
(
stream
,
managed
.
CreateDecryptor
(
bytes
,
rgbIV
),
CryptoStreamMode
.
Read
);
while
((
num5
=
stream3
.
ReadByte
())
!=
-
1
)
{
stream2
.
WriteByte
((
byte
)
num5
);
}
stream3
.
Close
();
stream2
.
Close
();
stream
.
Close
();
byte
[]
buffer3
=
stream2
.
ToArray
();
return
Encoding
.
Unicode
.
GetString
(
buffer3
);
}
catch
(
Exception
exception
)
{
Log
.
Error
(
exception
.
Message
);
}
}
public
static
string
Encrypt
(
string
strInputString
,
string
strKeyString
,
string
myIV
)
{
if
((
strInputString
==
null
)
||
(
strInputString
.
Length
==
0
))
{
return
strInputString
;
}
try
{
int
num4
;
int
keySize
=
0x100
;
int
blockSize
=
0x100
;
int
length
=
keySize
/
0x10
;
if
(
strKeyString
.
Length
>
length
)
{
strKeyString
=
strKeyString
.
Substring
(
0
,
length
);
}
if
(
strKeyString
.
Length
<
length
)
{
strKeyString
=
strKeyString
.
PadRight
(
length
,
'#'
);
}
Encoding
.
Unicode
.
GetBytes
(
strKeyString
);
if
(
myIV
.
Length
>
length
)
{
myIV
=
myIV
.
Substring
(
0
,
length
);
}
if
(
myIV
.
Length
<
length
)
{
myIV
=
myIV
.
PadRight
(
length
,
'#'
);
}
Encoding
.
Unicode
.
GetBytes
(
myIV
);
byte
[]
bytes
=
Encoding
.
Unicode
.
GetBytes
(
strKeyString
);
byte
[]
rgbIV
=
Encoding
.
Unicode
.
GetBytes
(
myIV
);
string
str
=
""
;
RijndaelManaged
managed
=
new
RijndaelManaged
{
BlockSize
=
blockSize
,
KeySize
=
keySize
};
MemoryStream
stream
=
new
MemoryStream
(
Encoding
.
Unicode
.
GetBytes
(
strInputString
));
MemoryStream
stream2
=
new
MemoryStream
();
CryptoStream
stream3
=
new
CryptoStream
(
stream2
,
managed
.
CreateEncryptor
(
bytes
,
rgbIV
),
CryptoStreamMode
.
Write
);
while
((
num4
=
stream
.
ReadByte
())
!=
-
1
)
{
stream3
.
WriteByte
((
byte
)
num4
);
}
stream3
.
Close
();
stream2
.
Close
();
stream
.
Close
();
foreach
(
byte
num5
in
stream2
.
ToArray
())
{
str
=
str
+
num5
.
ToString
(
"X2"
);
}
return
str
;
}
catch
(
Exception
exception
)
{
Log
.
Error
(
exception
.
Message
);
}
}
}
i've also uploaded it.
Can anybody help me ?
P.S.: sorry for my bad english... :-P
Attachment:
encryptdecrypt.zip
Reply
Answers (
0
)
Features of C#
How to add button in email body