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
andy bily
NA
9
2.1k
AES encryption C# cipher text length
Mar 6 2019 5:02 PM
From:
https://crypto.stackexchange.com/questions/54017/can-we-calculate-aes-ciphertext-length-based-on-the-length-of-the-plaintext?newreg=1ce930f3fa664ef3a5c17da4d32462b4
I see: output_size = input_size + (16 - (input_size % 16))
But this does not seem to correspond with what I see when running tests using C# and the AesCng with PKCS7 padding and CBC CipherMode.
Thoughts on how I calculate the length of the encrypted value?
At the end of the code, I have the "actual" length (that seen when running the code) -vs- the theoretical (per the above equation) and it shows how they differ.
Here's the code for sample program:
Public
Shared
Function
Encrypt(
ByVal
plainText
As
String
,
ByVal
aes256BitKeyValue
As
String
,
ByVal
initialVector
As
String
)
As
String
If
String
.IsNullOrEmpty(plainText)
Then
Return
String
.Empty
End
If
Dim
initialVectorBytes
As
Byte
() = System.Text.Encoding.ASCII.GetBytes(initialVector)
Dim
plainTextBytes
As
Byte
() = System.Text.Encoding.UTF8.GetBytes(plainText)
Dim
cipherTextBytes
As
Byte
() =
Nothing
Using symmetricKey
As
New
System.Security.Cryptography.AesCng()
symmetricKey.Padding = Security.Cryptography.PaddingMode.PKCS7
symmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC
Dim
keyBytes
As
Byte
() = StringToByteArray(aes256BitKeyValue)
Try
Using cryptographyTransform
As
System.Security.Cryptography.ICryptoTransform = symmetricKey.CreateEncryptor(keyBytes, initialVectorBytes)
Using memoryStream
As
New
System.IO.MemoryStream()
Using cryptographyStream
As
New
System.Security.Cryptography.CryptoStream(memoryStream, cryptographyTransform, System.Security.Cryptography.CryptoStreamMode.Write)
cryptographyStream.Write(plainTextBytes, 0, plainTextBytes.Length)
cryptographyStream.FlushFinalBlock()
cipherTextBytes = memoryStream.ToArray()
memoryStream.Close()
cryptographyStream.Close()
End
Using
End
Using
End
Using
Catch
ex
As
Exception
Throw
New
Exceptions.EncryptionException(
String
.Format(System.Globalization.CultureInfo.InvariantCulture, Resources.ExceptionMessages.EncryptionFailed1, ex))
Finally
symmetricKey.Clear()
End
Try
End
Using
Return
Convert.ToBase64String(cipherTextBytes)
End
Function
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(
string
[] args)
{
string
aes256Key =
"1234567890123456789012345678901234567890123456789012345678901234"
;
string
initialVector =
"66sDeG*3xsS334dE"
;
string
unencrypted20Text =
"12345678901234567890"
;
string
unencrypted40Text =
"1234567890123456789012345678901234567890"
;
string
unencrypted64Text =
"1234567890123456789012345678901234567890123456789012345678901234"
;
string
unencrypted128Text =
"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678"
;
string
unencrypted256Text =
"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456"
;
string
unencrypted257Text =
"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567"
;
string
unencrypted512Text = unencrypted256Text + unencrypted256Text;
string
unencrypted1024Text = unencrypted512Text + unencrypted512Text;
string
unencrypted2048Text = unencrypted1024Text + unencrypted1024Text;
string
unencrypted4096Text = unencrypted2048Text + unencrypted2048Text;
string
unencrypted8192Text = unencrypted4096Text + unencrypted4096Text;
string
encrypted20Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted20Text, aes256Key, initialVector);
string
encrypted40Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted40Text, aes256Key, initialVector);
string
encrypted64Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted64Text, aes256Key, initialVector);
string
encrypted128Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted128Text, aes256Key, initialVector);
string
encrypted256Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted256Text, aes256Key, initialVector);
string
encrypted257Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted257Text, aes256Key, initialVector);
string
encrypted512Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted512Text, aes256Key, initialVector);
string
encrypted1024Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted1024Text, aes256Key, initialVector);
string
encrypted2048Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted2048Text, aes256Key, initialVector);
string
encrypted4096Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted4096Text, aes256Key, initialVector);
string
encrypted8192Text = Grb.Core.Cryptography.AesEncryption.Encrypt(unencrypted8192Text, aes256Key, initialVector);
int
encrypted20TextLength = encrypted20Text.Length;
// actual: 44, expected 20 + (16 - 4) = 32
int
encrypted40TextLength = encrypted40Text.Length;
// actual: 64, expected 40 + (116 - 8) = 48
int
encrypted64TextLength = encrypted64Text.Length;
// actual: 108, expected 64 + (16 - 0) = 80
int
encrypted128TextLength = encrypted128Text.Length;
// actual: 192, expected 128 + (16 - 0) = 144
int
encrypted256TextLength = encrypted256Text.Length;
// actual: 364
int
encrypted257TextLength = encrypted257Text.Length;
// actual: 364
int
encrypted512TextLength = encrypted512Text.Length;
// actual: 704
int
encrypted1024TextLength = encrypted1024Text.Length;
// actual: 1388
int
encrypted2048TextLength = encrypted2048Text.Length;
// actual: 2752
int
encrypted4096TextLength = encrypted4096Text.Length;
// actual: 5484
int
encrypted8192TextLength = encrypted8192Text.Length;
// actual: 10944
}
}
}
Reply
Answers (
0
)
What is the problem of increasing time encryption Algorithm?
How to read .cs file in C# ?