Click here to Skip to main content
15,886,422 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: one class in tow namespace Pin
A_Fa20-Dec-06 19:12
A_Fa20-Dec-06 19:12 
QuestionListCtrl dynamic icon changing.. Pin
Arun M S20-Dec-06 0:51
Arun M S20-Dec-06 0:51 
AnswerRe: ListCtrl dynamic icon changing.. Pin
Monty220-Dec-06 2:13
Monty220-Dec-06 2:13 
GeneralRe: ListCtrl dynamic icon changing.. Pin
Arun M S20-Dec-06 2:26
Arun M S20-Dec-06 2:26 
QuestionText with Toolbar Pin
Arun M S20-Dec-06 0:47
Arun M S20-Dec-06 0:47 
QuestionHow to detect a mouse event of other applications? Pin
tempi19-Dec-06 23:39
tempi19-Dec-06 23:39 
AnswerRe: How to detect a mouse event of other applications? Pin
Rajesh R Subramanian20-Dec-06 0:02
professionalRajesh R Subramanian20-Dec-06 0:02 
QuestionPadding error for cryptograhpy with hmacsha256 verification [modified] Pin
Malcolm Chu19-Dec-06 22:54
Malcolm Chu19-Dec-06 22:54 
I am trying to build a cryptography program using HMACSHA256 as the verification technique. I came across this article by Nathan Blomquist and here's the link:

File Encryption/Decryption with Hash Verification in C#
http://www.codeproject.com/csharp/fileencryptdecrypt.asp

I edited the code to use HMACSHA256 instead as well as C++. Upon decryption using the incorrect password, I will encounter this error.


An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll

Additional information: Padding is invalid and cannot be removed.


Below is my code and the error occurs at this line
while ( (bytesInCurrentBlock = cin->Read(buffer, 0, buffer->Length)) != 0 )
in the DecryptFile function. I would be grateful if someone could point out my mistake here.

PS: I am suppose to be expecting the hmacsha256 mismatch error to throw instead



<code>void CryptoData::EncryptFile(String^ inFile, String^ outFile, String^ password, int keySize,
CryptoProgressCallBack^ callback)
{
FileStream^ fin = gcnew FileStream(inFile, FileMode::Open, FileAccess::Read);
FileStream^ fout = gcnew FileStream(outFile, FileMode::OpenOrCreate, FileAccess::Write);

fout->SetLength(0);

array<Byte>^ buffer = gcnew array<Byte>(BUFFER_SIZE);
int bytesInCurrentBlock;
long long bytesProcessed = 0;
long long fileLength = fin->Length;
long progress;

// generate IV and Salt
array<Byte>^ IV = GenerateRandomBytes(16);
array<Byte>^ salt = GenerateRandomBytes(16);

// create the crypting object
SymmetricAlgorithm^ sma = CreateRijndaelManaged(password, keySize, IV, salt);

// write the IV and salt to the beginning of the file
fout->Write(IV, 0, IV->Length);
fout->Write(salt, 0, salt->Length);

// Initialize the keyed hash object.
HashAlgorithm^ hmacsha256 = gcnew HMACSHA256(sma->Key);
// Compute the hash of the input file.
array<Byte>^ hashValue = hmacsha256->ComputeHash(fin);
// Reset fin to the beginning of the file.
fin->Position = 0;
// Write the computed hash value to the output file.
fout->Write(hashValue, 0, hashValue->Length);

// create the cryptostream
CryptoStream^ cout = gcnew CryptoStream(fout, sma->CreateEncryptor(), CryptoStreamMode::Write);

// read and write the bytes to the crypto stream in BUFFER_SIZE chunks
while ( (bytesInCurrentBlock = fin->Read(buffer, 0, buffer->Length)) != 0 )
{
cout->Write(buffer, 0, bytesInCurrentBlock);
bytesProcessed += bytesInCurrentBlock;
progress = (int)((bytesProcessed / fileLength) * 100);
callback(0, 100, progress);
}

// clear the hashing object
hmacsha256->Clear();

// flush and close the cryptostream
cout->Flush();
cout->Close();

// close the input file
fin->Close();
}

void CryptoData::DecryptFile(String^ inFile, String^ outFile, String^ password, int keySize,
CryptoProgressCallBack^ callback)
{
FileStream^ fin = gcnew FileStream(inFile, FileMode::Open, FileAccess::Read);
FileStream^ fout = gcnew FileStream(outFile, FileMode::OpenOrCreate, FileAccess::Write);

fout->SetLength(0);

array<Byte>^ buffer = gcnew array<Byte>(BUFFER_SIZE);
int bytesInCurrentBlock;
long long bytesProcessed = 0;
long long fileLength = fin->Length;
int progress;

// read the IV and Salt
array<Byte>^ IV = gcnew array<Byte>(16);
fin->Read(IV, 0, 16);
array<Byte>^ salt = gcnew array<Byte>(16);
fin->Read(salt, 0, 16);

// create the crypting object
SymmetricAlgorithm^ sma = CreateRijndaelManaged(password, keySize, IV, salt);

// Initialize the keyed hash object.
HashAlgorithm^ hmacsha256 = gcnew HMACSHA256(sma->Key);
// Create an array to hold the keyed hash value read from the file.
array<Byte>^ storedHash = gcnew array<Byte>(hmacsha256->HashSize / 8);
// Read in the storedHash.
fin->Read(storedHash, 0, storedHash->Length);
bytesProcessed = 64;

// create the cryptostream that will process the file
CryptoStream^ cin = gcnew CryptoStream(fin, sma->CreateDecryptor(), CryptoStreamMode::Read);

// read the BUFFER_SIZE chunks
while ( (bytesInCurrentBlock = cin->Read(buffer, 0, buffer->Length)) != 0 )
{
fout->Write(buffer, 0, bytesInCurrentBlock);
bytesProcessed += bytesInCurrentBlock;
progress = (int)((bytesProcessed / fileLength) * 100);
callback(0, 100, progress);
}

// Reposition the file to compute the hash of the output file.
fout->Position = 0;

array<Byte>^ computedHash = hmacsha256->ComputeHash(fout);

if (!ByteArrayEqual(computedHash, storedHash)) {
throw gcnew CryptoDataException("hmacsha256 mismatch");
}

// flush and close the cryptostream
cin->Flush();
cin->Close();

// close the output file
fout->Close();
}
</code>








-- modified at 5:09 Wednesday 20th December, 2006
Question2-dimensional array of strings Pin
namy119-Dec-06 22:52
namy119-Dec-06 22:52 
AnswerRe: 2-dimensional array of strings Pin
sunit519-Dec-06 23:13
sunit519-Dec-06 23:13 
AnswerRe: 2-dimensional array of strings Pin
toxcct19-Dec-06 23:18
toxcct19-Dec-06 23:18 
AnswerRe: 2-dimensional array of strings Pin
sunit520-Dec-06 0:51
sunit520-Dec-06 0:51 
AnswerRe: 2-dimensional array of strings Pin
Sam Hobbs20-Dec-06 18:05
Sam Hobbs20-Dec-06 18:05 
Questionaccess violation Pin
neha.agarwal2719-Dec-06 22:42
neha.agarwal2719-Dec-06 22:42 
AnswerRe: access violation Pin
Naveen20-Dec-06 0:32
Naveen20-Dec-06 0:32 
QuestionMemory Leak in SQLExecuteDirect() Pin
apoorva_raje19-Dec-06 22:37
apoorva_raje19-Dec-06 22:37 
AnswerRe: Memory Leak in SQLExecuteDirect() Pin
James R. Twine20-Dec-06 4:04
James R. Twine20-Dec-06 4:04 
GeneralRe: Memory Leak in SQLExecuteDirect() Pin
apoorva_raje20-Dec-06 22:52
apoorva_raje20-Dec-06 22:52 
QuestionImage segmentation Pin
Rishikant_Prince19-Dec-06 22:35
Rishikant_Prince19-Dec-06 22:35 
AnswerRe: Image segmentation Pin
567890123419-Dec-06 22:45
567890123419-Dec-06 22:45 
QuestionApplication Error Pin
Programm3r19-Dec-06 22:32
Programm3r19-Dec-06 22:32 
AnswerRe: Application Error Pin
Hamid_RT20-Dec-06 0:01
Hamid_RT20-Dec-06 0:01 
QuestionRe: Application Error [modified] Pin
Programm3r20-Dec-06 0:33
Programm3r20-Dec-06 0:33 
AnswerRe: Application Error Pin
Mila02520-Dec-06 1:47
Mila02520-Dec-06 1:47 
QuestionRe: Application Error [modified] Pin
Programm3r20-Dec-06 3:28
Programm3r20-Dec-06 3:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.