A
Allen Holman
I need to sign files with the same technique on both my
PC and my PDA. Below is the code I currently have on my
PC.
--------------------------------------------------------
public bool SignFile()
{
bool bSuccess = false;
try
{
FileStream fileStream = null;
SHA1 hash = new SHA1CryptoServiceProvider();
byte []
hashCode = null,
signedData = null;
RSACryptoServiceProvider
RSASign = new RSACryptoServiceProvider();
// Compute the SHA1 hash code for the import file
fileStream = File.Open(strFileName, FileMode.Open,
FileAccess.Read, FileShare.None);
hashCode = hash.ComputeHash(fileStream);
fileStream.Close();
// Encrypt the hashCode using the private RSA key
RSASign.FromXmlString(strXMLPrivateKey);
// The second argument is the NIST OSE Implementors
// Workshop (OIW) Security SIG algorithm identifiers
// for the sha1 hash algorithm
signedData = RSASign.SignHash(
hashCode, "1.3.14.3.2.26");
fileStream = File.Open(strFileName + ".Signature",
FileMode.OpenOrCreate, FileAccess.Write,
FileShare.None);
fileStream.Write(signedData, 0, signedData.Length);
fileStream.Close();
bSuccess = true;
}
catch
{
bSuccess = false;
}
return bSuccess;
}
Here is the code for my PDA.
-------------------------------------------------------
public unsafe void SignFile(string strImportFileName)
{
const int iBufferSize = 1024;
IntPtr
hKey = (IntPtr)0,
hProvider = (IntPtr)0,
hHash = (IntPtr)0;
byte []
bHashData = new byte[iBufferSize],
bSignature = null;
int
iBytesRead = 0,
iLgth = 0;
FileStream
stream = null;
// Encrypt the file
if (CryptAcquireContext(ref hProvider, null, null,
RSA_PROV_FULL, 0))
{
// Replace CryptGenKey with CryptImportKey when working
if (CryptGenKey(hProvider, AT_SIGNATURE,
CRYPT_EXPORTABLE, ref hKey))
{
if (CryptCreateHash(hProvider, CALG_SHA1,
(IntPtr)0, 0, ref hHash))
{
stream = File.OpenRead(strImportFileName);
iBytesRead = stream.Read(bHashData, 0,
iBufferSize);
while (iBytesRead > 0)
{
CryptHashData(hHash,bHashData,iBytesRead,0);
iBytesRead = stream.Read(bHashData, 0,
iBufferSize);
}
stream.Close();
iLgth = iBufferSize;
CryptSignHash(hHash,AT_SIGNATURE,null,0,null,
ref iLgth);
bSignature = new byte[iLgth];
CryptSignHash(hHash, AT_SIGNATURE, null, 0,
bSignature,ref iLgth);
stream = File.OpenWrite(strImportFileName
+ ".Signature");
stream.Write(bSignature,0,iLgth);
stream.Close();
CryptDestroyHash(hHash);
}
}
CryptReleaseContext(hProvider,0);
}
}
Any assistance in getting the two code to produced
identical hashes and signed hashes will be appreciated.
Allen
PC and my PDA. Below is the code I currently have on my
PC.
--------------------------------------------------------
public bool SignFile()
{
bool bSuccess = false;
try
{
FileStream fileStream = null;
SHA1 hash = new SHA1CryptoServiceProvider();
byte []
hashCode = null,
signedData = null;
RSACryptoServiceProvider
RSASign = new RSACryptoServiceProvider();
// Compute the SHA1 hash code for the import file
fileStream = File.Open(strFileName, FileMode.Open,
FileAccess.Read, FileShare.None);
hashCode = hash.ComputeHash(fileStream);
fileStream.Close();
// Encrypt the hashCode using the private RSA key
RSASign.FromXmlString(strXMLPrivateKey);
// The second argument is the NIST OSE Implementors
// Workshop (OIW) Security SIG algorithm identifiers
// for the sha1 hash algorithm
signedData = RSASign.SignHash(
hashCode, "1.3.14.3.2.26");
fileStream = File.Open(strFileName + ".Signature",
FileMode.OpenOrCreate, FileAccess.Write,
FileShare.None);
fileStream.Write(signedData, 0, signedData.Length);
fileStream.Close();
bSuccess = true;
}
catch
{
bSuccess = false;
}
return bSuccess;
}
Here is the code for my PDA.
-------------------------------------------------------
public unsafe void SignFile(string strImportFileName)
{
const int iBufferSize = 1024;
IntPtr
hKey = (IntPtr)0,
hProvider = (IntPtr)0,
hHash = (IntPtr)0;
byte []
bHashData = new byte[iBufferSize],
bSignature = null;
int
iBytesRead = 0,
iLgth = 0;
FileStream
stream = null;
// Encrypt the file
if (CryptAcquireContext(ref hProvider, null, null,
RSA_PROV_FULL, 0))
{
// Replace CryptGenKey with CryptImportKey when working
if (CryptGenKey(hProvider, AT_SIGNATURE,
CRYPT_EXPORTABLE, ref hKey))
{
if (CryptCreateHash(hProvider, CALG_SHA1,
(IntPtr)0, 0, ref hHash))
{
stream = File.OpenRead(strImportFileName);
iBytesRead = stream.Read(bHashData, 0,
iBufferSize);
while (iBytesRead > 0)
{
CryptHashData(hHash,bHashData,iBytesRead,0);
iBytesRead = stream.Read(bHashData, 0,
iBufferSize);
}
stream.Close();
iLgth = iBufferSize;
CryptSignHash(hHash,AT_SIGNATURE,null,0,null,
ref iLgth);
bSignature = new byte[iLgth];
CryptSignHash(hHash, AT_SIGNATURE, null, 0,
bSignature,ref iLgth);
stream = File.OpenWrite(strImportFileName
+ ".Signature");
stream.Write(bSignature,0,iLgth);
stream.Close();
CryptDestroyHash(hHash);
}
}
CryptReleaseContext(hProvider,0);
}
}
Any assistance in getting the two code to produced
identical hashes and signed hashes will be appreciated.
Allen