简体   繁体   English

如何将字符串复制到CryptUnprotectData的字节数组中

[英]how do I copy string into bytearray for CryptUnprotectData

I am attempting to pass data from a sqlite3 file to CryptUnprotectData. 我正在尝试将数据从sqlite3文件传递给CryptUnprotectData。

After selecting the data using a sqlite3 library I can print it to console. 使用sqlite3库选择数据后,我可以将其打印到控制台。 This is the data I am trying to decrypt (argv[i]): 这是我要解密的数据(argv [i]):

printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");

hex(password_value) = 01000000D08C9DDF0115D1118C7A00C04FC297EB01000000EEB05AE6044E5749B7BB63FAB045C99C00000000020000000000106600000001000020000000F6027D9B2EA5742C36075600DDFA7ECDAFD55BE247F984FBC92BFC9C7F9DE9520000000000000000020000200000006EC519ACA4DA90EFA7149FF16502E0985F4B86C75F52A1EF7CAAAC5FC88E48CC10000000F0B305A4829F3D397F1379CD63EAB48F400000001BD5A3B07DAA31AE35A2FCE8BDDBBA28055307E3137B3EBE899C0A0AD35E905AE125FF0ACBCA2982169ABAB0AE899493446897297D47BA65A09115AB13821EFE 十六进制(password_value)= 01000000D08C9DDF0115D1118C7A00C04FC297EB01000000EEB05AE6044E5749B7BB63FAB045C99C00000000020000000000106600000001000020000000F6027D9B2EA5742C36075600DDFA7ECDAFD55BE247F984FBC92BFC9C7F9DE9520000000000000000020000200000006EC519ACA4DA90EFA7149FF16502E0985F4B86C75F52A1EF7CAAAC5FC88E48CC10000000F0B305A4829F3D397F1379CD63EAB48F400000001BD5A3B07DAA31AE35A2FCE8BDDBBA28055307E3137B3EBE899C0A0AD35E905AE125FF0ACBCA2982169ABAB0AE899493446897297D47BA65A09115AB13821EFE

This is the prototype for the function that will decrypt: 这是将解密的函数的原型:

BOOL WINAPI CryptUnprotectData(
  __in       DATA_BLOB *pDataIn,
  __out_opt  LPWSTR *ppszDataDescr,
  __in_opt   DATA_BLOB *pOptionalEntropy,
  __in       PVOID pvReserved,
  __in_opt   CRYPTPROTECT_PROMPTSTRUCT *pPromptStruct,
  __in       DWORD dwFlags,
  __out      DATA_BLOB *pDataOut
);

DATA_BLOB struct looks like this: DATA_BLOB结构看起来像这样:

typedef struct _CRYPTOAPI_BLOB {
  DWORD cbData;
  BYTE  *pbData;
};

Where: cbData == A DWORD variable that contains the count, in bytes, of data. 其中:cbData ==一个DWORD变量,包含以字节为单位的数据计数。 pbData == A pointer to the data buffer. pbData ==指向数据缓冲区的指针。

I think what I need to do is create a structure like this: DATA_BLOB DataEncrypted; 认为我需要做的是创建一个像这样的结构:DATA_BLOB DataEncrypted;

DataEncrypted.pbData = ??? DataEncrypted.pbData = ??? DataEncrypted.cbData = strlen(argv[i])/2 DataEncrypted.cbData = strlen(argv [i])/ 2

and copy the data out of argv[i] into a byte-array... and then set pbData == pointer to byte array. 并将数据从argv [i]复制到字节数组...,然后设置pbData ==指向字节数组的指针。

I'm not sure how to do the that part... any suggestions? 我不确定该怎么做...有什么建议吗?

Here's a good answer . 这是一个很好的答案 It's in Java, but the algorithm is the same: 它使用Java,但是算法相同:

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                             + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM