Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 790817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:44:17+00:00 2026-05-14T21:44:17+00:00

I have an AES encryption being made on two columns: one of these columns

  • 0

I have an AES encryption being made on two columns: one of these columns is stored at a SQL Server 2000 database; the other is stored at a SQL Server 2008 database.

As the first column’s database (2000) doesn’t have native functionality for encryption / decryption, we’ve decided to do the cryptography logic at application level, with .NET classes, for both.

But as the second column’s database (2008) allow this kind of functionality, we’d like to make the data migration using the database functions to be faster, since the data migration in SQL 2k is much smaller than this second and it will last more than 50 hours because of being made at application level.

My problem started at this point: using the same key, I didn’t achieve the same result when encrypting a value, neither the same result size.

Below we have the full logic in both sides.. Of course I’m not showing the key, but everything else is the same:

private byte[] RijndaelEncrypt(byte[] clearData, byte[] Key)
{
    var memoryStream = new MemoryStream();

    Rijndael algorithm = Rijndael.Create();

    algorithm.Key = Key;
    algorithm.IV = InitializationVector;

    var criptoStream = new CryptoStream(memoryStream, algorithm.CreateEncryptor(), CryptoStreamMode.Write);
    criptoStream.Write(clearData, 0, clearData.Length);
    criptoStream.Close();

    byte[] encryptedData = memoryStream.ToArray();
    return encryptedData;
}

private byte[] RijndaelDecrypt(byte[] cipherData, byte[] Key)
{
    var memoryStream = new MemoryStream();

    Rijndael algorithm = Rijndael.Create();

    algorithm.Key = Key;
    algorithm.IV = InitializationVector;

    var criptoStream = new CryptoStream(memoryStream, algorithm.CreateDecryptor(), CryptoStreamMode.Write);

    criptoStream.Write(cipherData, 0, cipherData.Length);

    criptoStream.Close();

    byte[] decryptedData = memoryStream.ToArray();

    return decryptedData;
}

This is the SQL Code sample:

open symmetric key columnKey decryption by password = N'{pwd!!i_ll_not_show_it_here}'

declare @enc varchar(max)

set @enc = dbo.VarBinarytoBase64(EncryptByKey(Key_GUID('columnKey'), 'blablabla'))

select LEN(@enc), @enc

This varbinaryToBase64 is a tested sql function we use to convert varbinary to the same format we use to store strings in the .net application.

The result in C# is: eg0wgTeR3noWYgvdmpzTKijkdtTsdvnvKzh+uhyN3Lo=

The same result in SQL2k8 is: AI0zI7D77EmqgTQrdgMBHAEAAACyACXb+P3HvctA0yBduAuwPS4Ah3AB4Dbdj2KBGC1Dk4b8GEbtXs5fINzvusp8FRBknF15Br2xI1CqP0Qb/M4w

I just didn’t get yet what I’m doing wrong.

Do you have any ideas?

EDIT: One point I think is crucial: I have one Initialization Vector at my C# code, 16 bytes. This IV is not set at SQL symmetric key, could I do this?

But even not filling the IV in C#, I get very different results, both in content and length.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:44:18+00:00Added an answer on May 14, 2026 at 9:44 pm

    There are a couple of things I’d look at:

    1. Make absolutely sure that the plaintext is identical in content and encoding. IIRC, streams default to UTF-8 whereas if your VarBinaryToBase64 function take a nvarchar parameter, it will be Unicode.

    2. Make sure both encryption algorithms use the same block size. In SQL, you determine the algorithm when you call CREATE SYMMETRIC KEY. If you do not specify an algorithm, it uses AES256. In .NET using RijndaelManaged, I believe the default block size is 128 but you can set it to 256 (you cannot if you use the Aes class).

    3. The last thing I’d look for is how SQL Server deals with Initialization Vectors as you mentioned in your amended post. I want to say that it uses the authenticator parameter for this, but that’s a wild guess.

    EDIT

    I was way off. Given what I have discovered, you cannot use a .NET class to decrypt text encrypted by SQL Server’s built-in encryption because SQL Server adds a bunch of goo to what gets encrypted, including a random initialization vector. From Michael Cole’s book “Pro T-SQL 2005 Programmer’s Guide” (although 2008 does this the same way):

    When SQL Server encrypts by symmetric
    key, it adds metadata to the encrypted
    result, as well as padding, making the
    encrypted result larger (sometimes
    significantly larger) than the
    unencrypted plain text. The format for
    the encrypted result with metadata
    follows this format:

    • The first 16 bytes of the encrypted result represent the GUID of the
      symmetric key used to encrypt the data
    • The next 4 bytes represent the version number, currently hard-coded
      as “01000000”.
    • The next 8 bytes for DES encryption (16 bytes for AES encryption)
      represent the randomly generated
      initialization vector.
    • The next 8 bytes are header information representing the options
      used to encrypt the data. If the
      authenticator option is used, this
      header information includes a 20-byte
      SHA1 hash of the authenticator, making
      the header information 28 bytes in
      length.
    • The last part of the encrypted data is the actual data and padding itself.
      For DES algorithms, the length of this
      encrypted data will be a multiple of 8
      bytes. For AES algorithms, the length
      will be a multiple of 16 bytes.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code for AES encryption, can some one verify that this code
I have a problem with AES encryption and the customer showed me their PHP
I need a CORE that will perform AES-128 Encryption/Decryption. I have searched online but
Given two classes: First class performs AES encryption / decryption and returns encrypted /
We are adding AES 256 bit encryption to our server and client applications for
I have a char* string that I have encoded using AES encryption. This string
per my question Aes Encryption... missing an important piece , I have now learned
i have a java program . this is a AES encryption - decryption program.
I have written some (functional) AES encryption code using Java's built in encryption libraries,
I have written a BlackBerry app that uses AES encryption. I am trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.