Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C+中转换AES加密代码+;使用pycrypto创建python_Python_C++_Aes_Pycrypto_Wincrypt - Fatal编程技术网

在C+中转换AES加密代码+;使用pycrypto创建python

在C+中转换AES加密代码+;使用pycrypto创建python,python,c++,aes,pycrypto,wincrypt,Python,C++,Aes,Pycrypto,Wincrypt,我正在学习python,并试图将上面在线找到的代码片段转换为python 据我所知,下面的代码是基于密码“Microsoft”的SHA1散列生成会话密钥的,但我不确定如何基于python中密码的散列派生AES 256密钥。当我使用AES.new()?16个随机字节 string encrypt ( const char *s ) { DWORD dwSize = strlen ( s ); DWORD dwSize2 = strlen ( s ); HCRYPTHASH

我正在学习python,并试图将上面在线找到的代码片段转换为python

据我所知,下面的代码是基于密码“Microsoft”的SHA1散列生成会话密钥的,但我不确定如何基于python中密码的散列派生AES 256密钥。当我使用
AES.new()?16个随机字节

string encrypt ( const char *s ) 
{
    DWORD dwSize = strlen ( s );
    DWORD dwSize2 = strlen ( s );
    HCRYPTHASH hHash = NULL;
    HCRYPTKEY hKey = NULL;
    HCRYPTPROV hProv = NULL;
    char *buffer;

    char *pwd = "Microsoft"; 
    int pwdLen = strlen ( pwd );

    // CryptAcquireContext function is used to acquire a handle to a particular key container within a particular cryptographic service provider (CSP)
    if ( ! CryptAcquireContext ( &hProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, 0 ) )
    {
        printf ( "Unable to acquire encryption context\n" );
        return NULL;
    }

    // CryptCreateHash function initiates the hashing of a stream of data. It creates and returns to the calling application a handle to a cryptographic service provider (CSP) hash object
    if ( ! CryptCreateHash ( hProv, CALG_SHA1, 0, 0, &hHash ) )
    {
        CryptReleaseContext ( hProv, 0 );
        printf ( "Unable to create hash\n" );
        return NULL;
    }

    // CryptHashData function adds data to a specified hash object
    if ( ! CryptHashData ( hHash, (const byte *)pwd, pwdLen, 0 ) )
    {
        CryptDestroyHash ( hHash );
        CryptReleaseContext ( hProv, 0 );
        printf ( "Unable to add key\n" );
        return NULL;
    }

    // CryptDeriveKey function generates cryptographic session keys derived from a base data value
    if ( ! CryptDeriveKey ( hProv, CALG_AES_256, hHash, 0, &hKey ) )
    {
        CryptDestroyHash ( hHash );
        CryptReleaseContext ( hProv, 0 );
        printf ( "Unable to derive key\n" );
        return NULL;
    }

    // CryptEncrypt function encrypts data; have API return us the required buffer size
    CryptEncrypt ( hKey, 0, true, 0, 0, &dwSize, strlen ( s ) ); 

}