【问题标题】:Decrypt java (DESede/ECB/PKCS5Padding) cipher using python使用 python 解密 java (DESede/ECB/PKCS5Padding) 密码
【发布时间】:2020-10-03 11:25:42
【问题描述】:

是否可以使用python在java中实现这种技术(加密和解密)?

这里的 3DES 在这个 java 代码中使用。

Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
String keytext = "key......";
byte[] keyArray = keytext.getBytes("utf-8");
SecretKey key = new SecretKeySpec(keyArray, "DESede");

String mytext = "anytext......";
Cipher.init(1, key);
byte[] enc = cipher.doFinal(plaintext.getBytes("utf-8"));
String value = Base64.getUrlEncode().encodeToString(enc);
return value;

希望有人可以帮助我,如果可能的话,使用 python2 或 python3 来实现它

【问题讨论】:

    标签: java python encryption


    【解决方案1】:

    编辑:

    from Crypto.Cipher import DES3
    from Crypto.Random import get_random_bytes
    
    while True:
      try:
        key = DES3.adjust_key_parity(get_random_bytes(24))
        break
      except ValueError:
        pass
    
    cipher = DES3.new(key, DES3.MODE_CFB)
    plaintext = b'We are no longer the knights who say ni!'
    msg = cipher.iv + cipher.encrypt(plaintext)
    
    print(msg)
    

    取自 PyCryptodome 手册本身:Link

    【讨论】:

    • pycrypto 不再支持,pycryptodome 是后续项目。这段代码很可能会在 pycryptodome 中保持不变。
    • 谢谢@Snix,我正在寻找使用python而不是加密的解密方式
    • 老兄,看说明书就行了,你得用解密方法简单地反转机制
    猜你喜欢
    • 2021-09-25
    • 2012-03-01
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多