//ver- und wieder entschlüsseln eines Strings mit einem Passwort #include <windows.h> #include <stdio.h> #include <wincrypt.h> // lc cryptencryptdata.c advapi32.lib //---------------------------------------------------------------------------- void CryptandEncryptData(BYTE Buf[100],char *Password) { HCRYPTPROV hProv; HCRYPTHASH hPwdHash; HCRYPTKEY hKey; DWORD DataLen; DWORD i; DataLen = lstrlen(Buf)+1; printf("vorher:\t\t%s\n",Buf); if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) { printf("error: %d\n",GetLastError()); exit(1); } if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hPwdHash)) { printf("error: %d\n",GetLastError()); CryptReleaseContext(hProv, 0); exit(1); } if(!CryptHashData(hPwdHash, Password, lstrlen(Password)+1, 0)) { printf("error: %d\n",GetLastError()); CryptDestroyHash(hPwdHash); CryptReleaseContext(hProv, 0); exit(1); } if(!CryptDeriveKey(hProv, CALG_RC4, hPwdHash, CRYPT_CREATE_SALT, &hKey)) { printf("error: %d\n",GetLastError()); CryptDestroyHash(hPwdHash); CryptReleaseContext(hProv, 0); exit(1); } printf("CryptDeriveKey:\t"); for(i=0; i<DataLen; i++) printf("%02x ", Buf[i]); printf("\n"); CryptEncrypt(hKey, 0, TRUE, 0, Buf, &DataLen, 100); printf("Crypt:\t"); for(i=0; i<DataLen; i++) printf("0x%02x, ",Buf[i]); printf("\n"); CryptDecrypt(hKey, 0, TRUE, 0, Buf, &DataLen); printf("Decrypt:\t"); for(i=0; i<DataLen; i++) printf("%02x ",Buf[i]); printf("\n"); printf("nachher:\t%s\n", Buf); CryptDestroyKey(hKey); CryptDestroyHash(hPwdHash); CryptReleaseContext(hProv, 0); } //-------------------------------------------------------------------------- int main (int argc, char **argv) { /* Sind die erforderlichen Kommandozeilenargumente vorhanden? */ if (argc != 3) { fprintf( stderr, "usage: %s stringtoCrypt password\n",argv[0]); exit(1); } //printf("%s\t%s\n", argv[1],argv[2]); CryptandEncryptData(argv[1],argv[2]); return 0; }