标签: pycrypto

AES - 使用Pycrypto(python)加密加密(node-js)/解密

我正在写这个问题+答案,因为我经历了很多努力(可能是因为缺乏经验),迷失了许多不同的加密/解密节点或python的方法.

我想也许我的案子可以帮助将来的人.

我需要做什么:

  • 从表单获取数据,使用Crypto(node-js)加密它们
  • 将加密数据传递给Python并使用PyCrypto对其进行解密.

我选择使用AES加密.

这是我开始的方式(我不会经历我尝试过的所有事情):

  • 我按照本页末尾的示例进行操作

    在我的情况下给出了:

    (这可能是javascript和coffeescript之间非常糟糕的混合)

    crypto = require "crypto"
    [...]
    key = "mykeywhatever"
    cipher = crypto.createCipher('aes192', key)
    cipher.update('string i want to encode', 'binary', 'hex')
    encoded_string = cipher.final('hex')
    [...]
    
    Run Code Online (Sandbox Code Playgroud)

    这对我的字符串编码非常好.

  • 然后我编写了我的python脚本来解密这个字符串,使用PyCrypto的github页面上的自述文件:

    from Crypto.Cipher import AES
    [...]
    my_string = data_coming_from_rabbitmq
    obj = AES.new('mykeywhatever', AES.MODE_CBC)
    obj.decrypt(ciphertext)
    [...]
    
    Run Code Online (Sandbox Code Playgroud)

    这显然不起作用:在自述文件中有一个IV,但由于我没有在节点脚本中给出一个,为什么我要在python中给一个?

经过更多的谷歌搜索,我了解到节点的Crypto使用OpenSSL,而PyCrypto显然没有.所以我调查了一下,找到了那些页面:

事情变得复杂,没有人做同样的事情来解密数据,我迷路了,并寻求帮助.

答案就是我的同事和我想出来的(好吧,主要是我的corworker).

python cryptography pycrypto node.js

18
推荐指数
2
解决办法
2万
查看次数

试图在Mac OSX mavericks上安装pycrypto

我目前正在尝试安装pycrypto,当我执行python setup.py build时,我收到以下错误:

cc -bundle -undefined dynamic_lookup -arch x86_64 -arch i386 -Wl,-F. build/temp.macosx-10.9-intel-2.7/src/_fastmath.o -lgmp -o build/lib.macosx-10.9-intel-2.7/Crypto/PublicKey/_fastmath.so
ld: illegal text-relocation to '___gmp_binvert_limb_table' in /usr/local/lib/libgmp.a(mp_minv_tab.o) from '___gmpn_divexact_1' in /usr/local/lib/libgmp.a(dive_1.o) for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'cc' failed with exit status 1
Run Code Online (Sandbox Code Playgroud)

我已经尝试重新安装命令行工具并删除旧的Xcode实例.

任何帮助都会非常感谢

python macos pycrypto

18
推荐指数
5
解决办法
3万
查看次数

在Python中使用RSA

我正在使用RSA在Python中加密/解密我的会话密钥.我正在使用Pycrypto库.生成密钥对后,我想从生成的密钥中提取私钥和公钥,并将它们存储在不同的文件中.我怎样才能做到这一点?我可以看到有一个Private方法,可以告诉生成的密钥对有私有组件,但无法找到如何从这个生成的密钥对中提取密钥.任何建议都会有很大的帮助.

python rsa pycrypto

17
推荐指数
1
解决办法
2万
查看次数

ImportError:没有名为Crypto的模块

我刚刚开始探索Python.我正在尝试运行AES算法代码,我正面临:

ImportError:没有名为Crypto的模块.

你是如何解决这个问题的?

python aes pycrypto

17
推荐指数
2
解决办法
4万
查看次数

python 3的加密工具

我在python 3中编写了一个需要加密函数的程序(至少是aes和rsa).我发现PyCrypto似乎只适用于2.x版本.

是否有任何好的工具可用于python 3或者我应该开始翻译我的程序以兼容python 2(或任何其他解决方案)?

谢谢


更新如下所述,PyCrypto现在可以在py3k上使用

python cryptography pycrypto python-3.x

16
推荐指数
3
解决办法
2万
查看次数

无法在Debian lenny上安装python模块"pycrypto"

我尝试通过下载源代码并执行以下命令来安装pycrypto模块python setup.py install,然后出现错误

running install
running build
running build_py
running build_ext
warning: GMP library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Hash.MD2' extension
gcc -pthread -fno-strict-aliasing -fwrapv -Wall -Wstrict-prototypes -fPIC -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/usr/include/python2.5 -c src/MD2.c -o build/temp.linux-x86_64-2.5/src/MD2.o
src/MD2.c:31:20: error: Python.h: No such file or directory
src/MD2.c:118: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
In file included from src/MD2.c:134:
src/hash_template.c:42: error: expected specifier-qualifier-list before 'PyObject_HEAD'
src/hash_template.c:46: error: expected '=', ',', ';', 'asm' or '__attribute__' …
Run Code Online (Sandbox Code Playgroud)

python debian python-2.6 lenny pycrypto

16
推荐指数
1
解决办法
1万
查看次数

来自Python的Wincrypt的Diffie-Hellman(到RC4)

我目前正在开发一个用C++编写的项目,该项目利用CryptoAPI执行Diffie-Hellman密钥交换.我在使用这个工作时遇到了一些麻烦,因为我得到的最终RC4会话密钥不能用于加密Python中的相同文本(使用pycrypto).

执行Diffie-Hellman密钥交换的C++代码取自msdn,但此处包含后代:

#include <tchar.h>
#include <windows.h>
#include <wincrypt.h>
#pragma comment(lib, "crypt32.lib")

// The key size, in bits.
#define DHKEYSIZE 512

// Prime in little-endian format.
static const BYTE g_rgbPrime[] = 
{
    0x91, 0x02, 0xc8, 0x31, 0xee, 0x36, 0x07, 0xec, 
    0xc2, 0x24, 0x37, 0xf8, 0xfb, 0x3d, 0x69, 0x49, 
    0xac, 0x7a, 0xab, 0x32, 0xac, 0xad, 0xe9, 0xc2, 
    0xaf, 0x0e, 0x21, 0xb7, 0xc5, 0x2f, 0x76, 0xd0, 
    0xe5, 0x82, 0x78, 0x0d, 0x4f, 0x32, 0xb8, 0xcb,
    0xf7, 0x0c, 0x8d, 0xfb, 0x3a, 0xd8, 0xc0, …
Run Code Online (Sandbox Code Playgroud)

c++ python encryption cryptoapi pycrypto

16
推荐指数
1
解决办法
595
查看次数

使用pycrypto将RSA密钥保存到文件

我正在使用PyCrypto 2.3,我想将生成的密钥保存到文件中,以便将它们分发到客户端和服务器.我似乎无法找到正确打印密钥的方法,也无法在互联网上找到示例.

    def resetKeys(self):
        keys = RSA.generate(1024)

        privHandle = open(self.privateKeyFile, 'wb')
        privHandle.write(keys.privatekey())
        privHandle.close()

        pubHandle = open(self.publicKeyFile, 'wb')
        pubHandle.write(keys.publickey())
        pubHandle.close()
Run Code Online (Sandbox Code Playgroud)

此代码不起作用的原因有很多:首先,keys.publickey()似乎不可打印,它返回:

    <_RSAobj @0x10f810d0 n(1024),e>
Run Code Online (Sandbox Code Playgroud)

第二,keys没有命名的功能privatekey.

以前有人这样做过吗?

python pycrypto

15
推荐指数
2
解决办法
2万
查看次数

Ubuntu上的pycrypto给出了编译器错误

我试图用Python 2.7.3 在Ubuntu 10.04(Lucid Lynx)上安装pycrypto2.6 .

我遇到以下错误:

running build
running build_py
running build_ext
running build_configure
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/home/pratibha/Desktop/pycrypto-2.6':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
Traceback (most recent call last):
File "setup.py", line 456, in <module>
core.setup(**kw)
File "/usr/local/lib/python2.7/distutils/core.py", line 152, in setup
dist.run_commands()
File "/usr/local/lib/python2.7/distutils/dist.py", line 953, in run_commands
self.run_command(cmd)
File "/usr/local/lib/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run() …
Run Code Online (Sandbox Code Playgroud)

ubuntu gcc pycrypto

15
推荐指数
3
解决办法
2万
查看次数

PyCrypto在Windows上安装错误

我正在尝试在我的计算机上安装PyCrypto 2.6 Library.但我不断收到以下错误

D:\Software\Python\package\pycrypto-2.6>python setup.py build
running build
running build_py
running build_ext
warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.

building 'Crypto.Random.OSRNG.winrandom' extension
error: Unable to find vcvarsall.bat
Run Code Online (Sandbox Code Playgroud)

我的系统有Windows 8 Pro 64位,Visual Studio Enterprise 2012和Python 3.3

为了解决这个错误,我尝试设置环境变量,VS90COMNTOOLS=%VS110COMNTOOLS%fmuecke在post错误中所建议的:无法找到vcvarsall.bat,但它对我不起作用.

任何人都可以告诉我如何解决这个错误.

仅供参考,我不安装VC2008等.

python distutils pycrypto python-3.x

15
推荐指数
2
解决办法
3万
查看次数