where to get and install crypto.dll on 64 bit Windows
I tried to reproduce the error on my computer and was successful when I ran the "error-producing" file _big_num_ctypes.py
. Although, I do not have Visual Studio, the error stems from the missing crypto.dll
file. We will deduce this step-by-step. Let's first examine the error causing code snippet in the file _big_num_ctypes.py
:
#imports
from ctypes.util import find_library
.
.
from .._ffi import LibraryNotFoundError, FFIEngineError
try:
# On Python 2, the unicode string here may raise a UnicodeDecodeError as it
# tries to join a bytestring path to the unicode name "crypto"
libcrypto_path = find_library(b'crypto' if sys.version_info < (3,) else 'crypto')
if not libcrypto_path:
raise LibraryNotFoundError('The library libcrypto could not be found')
.
.
except (AttributeError):
raise FFIEngineError('Error initializing ctypes')
I ran the file:
C:\>cd "C:\ProgramData\Anaconda3\Lib\site-packages\asn1crypto\_perf"
C:\ProgramData\Anaconda3\Lib\site-packages\asn1crypto\_perf>python _big_num_ctypes.py
and had a Traceback
for the library import:
Traceback (most recent call last):
File "_big_num_ctypes.py", line 27, in <module>
from .._ffi import LibraryNotFoundError, FFIEngineError
ValueError: attempted relative import beyond top-level package
So, I changed the import path for.ffi
to:
from asn1crypto._ffi import LibraryNotFoundError, FFIEngineError
On the second run, the missing libcrypto library error appeared:
asn1crypto._ffi.LibraryNotFoundError: The library libcrypto could not be found
The exception is raised when the dll library named crypto could not be found at C:\Windows\System32 and/or SYSWOW64(for 64-bit)
libcrypto_path = find_library(b'crypto' if sys.version_info < (3,) else 'crypto')
The purpose of find_library
is to find a specified library and return a pathname. The behavior of this method varies with OS as described in the docs. If this method cannot find any packages, it returns None
.
>>> from ctypes.util import find_library
>>> print(find_library("c"))
None
In our case, the search is for crypto.dll
and I couldn't find this file on my computer. So, I downloaded and installed it exactly according to the instructions here. When I checked again:
>>> find_library('crypto')
'C:\\windows\\system32\\crypto.dll'
Now I ran python _big_num_ctypes.py
again and got a different Traceback
:
Traceback (most recent call last):
File "_big_num_ctypes.py", line 37, in <module>
libcrypto = CDLL(libcrypto_path)
File "C:\ProgramData\Anaconda3\lib\ctypes\__init__.py", line 348, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application
A further investigation into the above error revealed that if I'm using a 32bit DLL with 64bit Python, or vice-versa, then I may get such errors as explained here. So, I installed Python 3.6 32-bit and tried again with py -3.6-32 _big_num_ctypes.py
. I also installed all the required packages, but this error persisted.
Could it be possible that we may require 32-bit binaries for the Crypto
package? This answer and this give more information.
I realized that Pycryptodome is a regularly maintained package and is preferred over the old Crypto
package but can still be installed under Crypto
. Another point to notice is that one of the requirements for this package is MS Visual Studio 2015 (Community Edition) and the C/C++ compilers and the redistributable only. It could be possible that some C++ compiler files or MS Visual Studio files are missing at present and causing these issues to happen.
If you install all the above prerequisites, the crypto.dll
file and the Pycryptodome
package, I believe this error will be Resolved. You have already installed other required packages OpenSSL
& Twilio
. Unfortunately, I am restricted to install MS Visual Studio on my computer and so I couldn't test this further.
I also ran the unittest
code and it ran successfully for me:
#Output
.
----------------------------------------------------------------------
Ran 1 test in 0.771s
OK
I searched a lot and could find that you are missing crypto.dll
file. Your code is looking for this dll file and it is unable to find it.
Please note this wont be installed by pip install crypto
as this is python library and the code is looking for a dll file.
ctypes.util.find_library
searches for dll file from windows environment path variable.
Reference : find_library() in ctypes
To verify I checked.
find_library('l2gpstore')
>>'C:\\WINDOWS\\system32\\l2gpstore.dll'
find_library('java')
>>'C:\\Program Files\\Java\\jdk-9.0.4\\bin\\java.dll'
Furthermore you should install OpenSSL
with libcrypto
module from here
OpenSSL
OpenSSL installation instructions
The master sources are maintained in our git repository, which is accessible over the network and cloned on GitHub, at https://github.com/openssl/openssl. Bugs and pull patches (issues and pull requests) should be file on the GitHub repo. Please familiarize yourself with the license.
libcrypto with respect to OpenSSL
reference : GitHub
libcrypto (with platform specific naming): Provides general cryptographic and X.509 support needed by SSL/TLS but not logically part of it.
Once you install binaries and check crypto.dll
is available in one of the path strings in your environment variables this issue should be resolved.
If not add it into path variable and check.
Update:
Update since the question has been updated and the issue has recurred.
There are architectural changes with OpenSSL 1.1.0 as compared to 1.0.2
September 13, 2018 - OpenSSL 1.1.0 and later are quite different from previous releases. Users should install BOTH the 1.0.2 series (LTS) and the 1.1.1 (LTS) series for maximum application compatibility. Developers need to recompile their software to support 1.1.1. See the official OpenSSL release strategy document for more details. – Prateek yesterday
If you open 1.0.2 from Github you can see crypto.h
file , the same file is missing in latest version. Also OpenSSL
there is change in DLL
names , they renamed libeay32
to libcrypto
You need to post code which makes use of asn1crypto
library in the post. There is no code that explicitly uses asn1crypto in your post. So, not able to reproduce your issue using pipenv
.
Make sure you are using updated libraries too.
I would not recommend downloading DLL source from unreliable source like DLLdownloader
If you are having issues with latest version of OpenSSL
and asn1crypto
its better to downgrade OpenSSL
to 1.0.2
,I think that would work considering it ships with crypto.h
file.
Good luck!