|
security
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
problem with encryption / decryptionCan someone help me out WHY the two files encrypting the same content has different decryt content? My Problem is to encrypt data in c++(using CryptoAPI) and decrypt it using C# Thanks in advance. ///////////////////////////////////////////////////////////////////////////////////////////////////// //C# code using System; using System.IO; using System.Security.Cryptography; using System.Text; using System.Resources; using System.Collections; namespace MySampleCryptoApp { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { [STAThread] static void Main(string[] args) { Encrypt(); Decrypt(); } private static void Encrypt() { RC2CryptoServiceProvider RCCrypto = new RC2CryptoServiceProvider(); RCCrypto.Mode = CipherMode.CBC; RCCrypto.KeySize = 40; RCCrypto.EffectiveKeySize = 40; RCCrypto.BlockSize = 64; UnicodeEncoding UE = new UnicodeEncoding(); //following code ins troublesome //byte[] Key = UE.GetBytes("abcdefgh"); PasswordDeriveBytes pwd = new PasswordDeriveBytes("abcdefgh",null); byte[] Key = pwd.CryptDeriveKey("RC2","MD5",0,new byte[8]); byte[] IV = {0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00}; FileStream oFStream = new FileStream(@"E:\Output.txt", FileMode.OpenOrCreate, FileAccess.Write); //Create a CryptoStream, pass it the NetworkStream, and encrypt //it with the RC2Crypto class. CryptoStream CryptStream = new CryptoStream(oFStream, RCCrypto.CreateEncryptor (Key, IV), CryptoStreamMode.Write); //Create a StreamWriter for easy writing to the //file stream. StreamWriter SWriter = new StreamWriter(CryptStream); long lLength = oFStream.Length; //Write to the stream. //SWriter.WriteLine("abcdefgh"); SWriter.WriteLine("ab"); //Inform the user that the message was written //to the stream. Console.WriteLine("The message was sent."); //Close all of the connections. SWriter.Close(); CryptStream.Close(); oFStream.Close(); } private static void Decrypt() { UnicodeEncoding UE = new UnicodeEncoding(); PasswordDeriveBytes pwd = new PasswordDeriveBytes("abcdefgh",null); byte[] Key = pwd.CryptDeriveKey("RC2","MD5",0,new byte[8]); byte[] IV = {0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00}; //FileStream oFStream = new FileStream(@"E:\Output.txt", FileMode.Open, FileAccess.Read); FileStream oFStream = new FileStream(@" E:\\Output.txt", FileMode.Open, FileAccess.Read); //Create a new instance of the RC2Crypto class // and decrypt the stream. RC2CryptoServiceProvider RCCrypto = new RC2CryptoServiceProvider(); RCCrypto.Mode = CipherMode.CBC; RCCrypto.KeySize = 40; RCCrypto.EffectiveKeySize = 40; RCCrypto.BlockSize = 64; //Create a CryptoStream, pass it the NetworkStream, and decrypt //it with the Rijndael class using key and IV. CryptoStream CryptStream = new CryptoStream(oFStream, RCCrypto.CreateDecryptor (Key, IV), CryptoStreamMode.Read); //Read the stream. StreamReader SReader = new StreamReader(CryptStream); //Display the message. Console.WriteLine("The decrypted original message: {0}", SReader.ReadToEnd()); //Close the streams. CryptStream.Close(); SReader.Close(); oFStream.Close(); } } } //////////////////////////////////////////////////////////////////////////////////////////////////// // SampleCryptoNativeApp.cpp : Defines the entry point for // the console application. // #include "stdafx.h" #include "windows.h" #include <wincrypt.h> #include <TCHAR.h> void PRINTERROR(char* msg){ printf(" %s : error code %d ",msg,GetLastError());} int main(int argc, char* argv[]) { BOOL bResult; HCRYPTPROV hProv; HCRYPTHASH hHash; HCRYPTKEY hKey; FILE *fp ; BYTE *pByte = NULL; pByte = new BYTE[100]; memset(pByte, 0, 100); DWORD dwError; bResult = CryptAcquireContext( &hProv, NULL, //generates default (username) keystore, MS_ENHANCED_PROV, PROV_RSA_FULL, NULL); if(!bResult) // dwError = GetLastError(); PRINTERROR("CryptAcquireContext 1"); bResult = CryptAcquireContext(&hProv, NULL,MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); //bResult = CryptAcquireContext(&hProv, NULL,MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); if(!bResult) // dwError = GetLastError(); PRINTERROR("CryptAcquireContext 2"); bResult = CryptCreateHash(hProv, CALG_MD5, 0, 0,&hHash); if(!bResult) // dwError = GetLastError(); PRINTERROR("CryptCreateHash "); BYTE pByteBaseData[] ={97,0,98,0,99,0,100,0,101,0,102,0,103,0,104,0}; //abcdefgh int nKeyLen = 16; bResult = CryptHashData(hHash, pByteBaseData,(DWORD) nKeyLen, 0); if(!bResult) //dwError = GetLastError(); PRINTERROR("CryptHashData "); //bResult = CryptDeriveKey(hProv, CALG_RC2, hHash,CRYPT_CREATE_SALT, &hKey); bResult = CryptDeriveKey(hProv, CALG_RC2, hHash,CRYPT_NO_SALT, &hKey); if(!bResult) //dwError = GetLastError(); PRINTERROR("CryptDeriveKey "); BYTE pByteIV[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; bResult = CryptSetKeyParam(hKey,KP_IV, pByteIV, 0); if(!bResult) // dwError = GetLastError(); PRINTERROR("CryptSetKeyParam "); //char pToEncryptString[] = "abcdefgh"; //char pToEncryptString[] = "a"; BYTE *pToEncryptBuffer = new BYTE[100]; memset(pToEncryptBuffer, 0, 100); pToEncryptBuffer[0] = 'a'; pToEncryptBuffer[0] = 0; pToEncryptBuffer[0] = 'b'; pToEncryptBuffer[0] = 0; // memset(pToEncryptBuffer, 0, 100); // memcpy(pToEncryptBuffer,pToEncryptString,_tcslen(pToEncryptString)*2); //DWORD dwInputLen = _tcslen(pToEncryptString)*2; DWORD dwInputLen = 0x04; DWORD dwBufLen = 0x20; bResult = CryptEncrypt(hKey, NULL, TRUE, 0,pToEncryptBuffer,&dwInputLen, dwBufLen); if(!bResult) // dwError = GetLastError(); PRINTERROR("CryptEncrypt "); fp = fopen("E:\\Win32Output.txt", "w"); for(int nIndex = 0; nIndex < (int) dwInputLen;nIndex++) { fprintf(fp, "%c", pToEncryptBuffer[nIndex]); } fclose(fp); /*DWORD dwOutputLen = nLen; bResult = CryptDecrypt(hKey, NULL, TRUE, 0, pByte,&dwOutputLen); if(!bResult) dwError = GetLastError(); bResult = CryptDecrypt(hKey, NULL, TRUE, 0,pToEncryptBuffer, &dwOutputLen); if(!bResult) dwError = GetLastError();*/ return 0; } ----------------------- Posted by a user from .NET 247 (http://www.dotnet247.com/) <Id>OCArCUTvl0+ps7rTWX+pGA==</Id>
Basic question about Public Private Key Pairs
Windows authentication Check for certian privileges How to protect data in executable file? Possible security error loading an Xsl? Forms authentication fails on Windows XP PRO Encrypting short data w/ asymmetric cipher Access Denied .Net Security Hole Problem - clients read each others files PasswordDeriveBytes in .NET 2.0 Beta |
|||||||||||||||||||||||