Jennifer  H

Jennifer H

  • NA
  • 2
  • 0

How do you set the right padding key for DES Algorithm?

Oct 20 2008 11:05 PM

Hello,

Can someone shows me how to set he right padding key for the DES Algorithm? I have 15 alpha-numerics key to decrypt the encrypted value but when I get to the legal key, it extracts the secret key to 8bytes to meet the DES Algorithm requirement and the return value for decrypted is "null", I wonder why is it and how do I set the right padding key for decrypting the encrypted value with the right secret key?  Thanks

public virtual byte[] GetLegalKey()

{

// Adjust key if necessary, and return a valid key

if (mCryptoService.LegalKeySizes.Length > 0)

{

// Key sizes in bits

int keySize = mKey.Length * 8;

int minSize = mCryptoService.LegalKeySizes[0].MinSize;

int maxSize = mCryptoService.LegalKeySizes[0].MaxSize;

int skipSize = mCryptoService.LegalKeySizes[0].SkipSize;

 

if (keySize > maxSize)

{

// Extract maximum size allowed

mKey = mKey.Substring(0, maxSize / 8);

}

else if (keySize < maxSize)

{

// Set valid size

int validSize = (keySize <= minSize) ? minSize : (keySize - keySize % skipSize) + skipSize;

if (keySize < validSize)

{

// Pad the key with asterisk to make up the size

mKey = mKey.PadRight(validSize / 8, '*');

}

}

}

PasswordDeriveBytes key = new PasswordDeriveBytes(mKey, ASCIIEncoding.ASCII.GetBytes(mSalt));

return key.GetBytes(mKey.Length);

}