在Golang中解密Python AES CFB加密的内容。

huangapple go评论122阅读模式
英文:

Decrypt in Golang what was encrypted in Python AES CFB

问题

根据Golang关于CFB解密的文档,我编写了一个最小工作示例,用于解密使用AES CFB加密并在python3中进行base64编码的字符串。

当消息在Golang中加密(使用Golang文档示例中的加密函数)时,Golang解密正常工作。
然而,当我在python脚本中使用python加密包加密消息时,我无法成功在Golang脚本中解密它。我没有得到正确的字节返回。

  1. $ python3 stack.py
  2. Going to encrypt and base64 "This is not encrypted" result:
  3. b'jf9A5LCxKWPuNb1XiH+G3APAgR//'
  4. Now going to call the Golang script:
  5. b'Hello from Golang, going to decrypt: jf9A5LCxKWPuNb1XiH+G3APAgR//
  6. Result: Tl!\xca/\xf1\xc0\xb2\xd01Y\x02V\xec\xdf\xecy\xd38&\xd9\n'

AES实现的块大小默认为16。

所以问题是:出了什么问题?

Golang脚本:

  1. package main
  2. import (
  3. "crypto/aes"
  4. "crypto/cipher"
  5. "encoding/base64"
  6. "fmt"
  7. "os"
  8. )
  9. func main() {
  10. key := []byte("TfvY7I358yospfWKcoviZizOShpm5hyH")
  11. iv := []byte("mb13KcoviZizvYhp")
  12. payload_python := os.Args[1]
  13. fmt.Println("Hello from Golang, going to decrypt: "+payload_python+" Result: "+string(decrypt(key, payload_python, iv)))
  14. }
  15. func decrypt(key []byte, cryptoText string, iv []byte) []byte {
  16. ciphertext, _ := base64.StdEncoding.DecodeString(cryptoText) //decode base64 coding
  17. //prepare decryption based on key and iv
  18. block, _ := aes.NewCipher(key)
  19. stream := cipher.NewCFBDecrypter(block, iv)
  20. //decrypt
  21. stream.XORKeyStream(ciphertext, ciphertext)
  22. return ciphertext
  23. }

Python脚本:

  1. #!/usr/bin/env python3
  2. import base64
  3. from Crypto.Cipher import AES
  4. from subprocess import check_output
  5. original_message = 'This is not encrypted'
  6. key = 'TfvY7I358yospfWKcoviZizOShpm5hyH'
  7. iv = 'mb13KcoviZizvYhp'
  8. #prepare encryption
  9. cfb_cipher_encrypt = AES.new(key, AES.MODE_CFB, iv)
  10. #encrypt and base64 encode
  11. encryptedpayload = base64.b64encode(cfb_cipher_encrypt.encrypt(original_message))
  12. print('Going to encrypt and base64 "{}" result:\n{}\n'.format(original_message,encryptedpayload))
  13. print('Now going to call the Golang script:')
  14. print(check_output('go run stack.go {}'.format(encryptedpayload.decode()),shell=True))
英文:

Based on the Golang documentation on CFB decryption I wrote a minimal working example to decrypt a string that was encrypted with AES CFB and then base 64 encoded in python3.

The golang decryption works fine when the message was encrypted within Golang (with the encryption function from the Golang doc example).
However when I encrypt the message in a python script using the python crypto package, I am unable to decrypt it in the golang script successfully. I don't get the right bytes back.

  1. $ python3 stack.py
  2. Going to encrypt and base64 "This is not encrypted" result:
  3. b'jf9A5LCxKWPuNb1XiH+G3APAgR//'
  4. Now going to call the Golang script:
  5. b'Hello from Golang, going to decrypt: jf9A5LCxKWPuNb1XiH+G3APAgR//
  6. Result: Tl!\xca/\xf1\xc0\xb2\xd01Y\x02V\xec\xdf\xecy\xd38&\xd9\n'

Blocksize is 16 by default for both AES implementations.

So the question: What is going wrong?

Golang script:

<!-- language: lang-go -->

  1. package main
  2. import (
  3. &quot;crypto/aes&quot;
  4. &quot;crypto/cipher&quot;
  5. &quot;encoding/base64&quot;
  6. &quot;fmt&quot;
  7. &quot;os&quot;
  8. )
  9. func main() {
  10. key := []byte(&quot;TfvY7I358yospfWKcoviZizOShpm5hyH&quot;)
  11. iv := []byte(&quot;mb13KcoviZizvYhp&quot;)
  12. payload_python := os.Args[1]
  13. fmt.Println(&quot;Hello from Golang, going to decrypt: &quot;+payload_python+&quot; Result: &quot;+string(decrypt(key, payload_python, iv)))
  14. }
  15. func decrypt(key []byte, cryptoText string, iv []byte) []byte {
  16. ciphertext, _ := base64.StdEncoding.DecodeString(cryptoText) //decode base64 coding
  17. //prepare decryption based on key and iv
  18. block, _ := aes.NewCipher(key)
  19. stream := cipher.NewCFBDecrypter(block, iv)
  20. //decrypt
  21. stream.XORKeyStream(ciphertext, ciphertext)
  22. return ciphertext
  23. }

Python script:

<!-- language: lang-python -->

  1. #!/usr/bin/env python3
  2. import base64
  3. from Crypto.Cipher import AES
  4. from subprocess import check_output
  5. original_message = &#39;This is not encrypted&#39;
  6. key = &#39;TfvY7I358yospfWKcoviZizOShpm5hyH&#39;
  7. iv = &#39;mb13KcoviZizvYhp&#39;
  8. #prepare encryption
  9. cfb_cipher_encrypt = AES.new(key, AES.MODE_CFB, iv)
  10. #encrypt and base64 encode
  11. encryptedpayload = base64.b64encode(cfb_cipher_encrypt.encrypt(original_message))
  12. print(&#39;Going to encrypt and base64 &quot;{}&quot; result:\n{}\n&#39;.format(original_message,encryptedpayload))
  13. print(&#39;Now going to call the Golang script:&#39;)
  14. print(check_output(&#39;go run stack.go {}&#39;.format(encryptedpayload.decode()),shell=True))

答案1

得分: 5

尝试使用以下Python代码进行加密。

然后可以成功地使用Go进行解密。

  1. #!/usr/bin/env python3
  2. import base64
  3. from Crypto.Cipher import AES
  4. MODE = AES.MODE_CFB
  5. BLOCK_SIZE = 16
  6. SEGMENT_SIZE = 128
  7. def _pad_string(value):
  8. length = len(value)
  9. pad_size = BLOCK_SIZE - (length % BLOCK_SIZE)
  10. return value.ljust(length + pad_size, '\x00')
  11. def encrypt(key, iv, plaintext):
  12. aes = AES.new(key, MODE, iv, segment_size=SEGMENT_SIZE)
  13. plaintext = _pad_string(plaintext)
  14. encrypted_text = aes.encrypt(plaintext)
  15. return encrypted_text
  16. key = 'TfvY7I358yospfWKcoviZizOShpm5hyH'
  17. iv = 'mb13KcoviZizvYhp'
  18. original_message = 'This is not encrypted'
  19. encryptedpayload = base64.b64encode(encrypt(key, iv, original_message))
  20. print('Going to encrypt and base64 "{}" result:\n{}\n'.format(original_message,encryptedpayload))

来源:http://chase-seibert.github.io/blog/2016/01/29/cryptojs-pycrypto-ios-aes256.html

英文:

Try encrypting from Python like this.

The result can then be unencrypted from Go successfully.

  1. #!/usr/bin/env python3
  2. import base64
  3. from Crypto.Cipher import AES
  4. MODE = AES.MODE_CFB
  5. BLOCK_SIZE = 16
  6. SEGMENT_SIZE = 128
  7. def _pad_string(value):
  8. length = len(value)
  9. pad_size = BLOCK_SIZE - (length % BLOCK_SIZE)
  10. return value.ljust(length + pad_size, &#39;\x00&#39;)
  11. def encrypt(key, iv, plaintext):
  12. aes = AES.new(key, MODE, iv, segment_size=SEGMENT_SIZE)
  13. plaintext = _pad_string(plaintext)
  14. encrypted_text = aes.encrypt(plaintext)
  15. return encrypted_text
  16. key = &#39;TfvY7I358yospfWKcoviZizOShpm5hyH&#39;
  17. iv = &#39;mb13KcoviZizvYhp&#39;
  18. original_message = &#39;This is not encrypted&#39;
  19. encryptedpayload = base64.b64encode(encrypt(key, iv, original_message))
  20. print(&#39;Going to encrypt and base64 &quot;{}&quot; result:\n{}\n&#39;.format(original_message,encryptedpayload))

Source: http://chase-seibert.github.io/blog/2016/01/29/cryptojs-pycrypto-ios-aes256.html

huangapple
  • 本文由 发表于 2017年6月30日 23:27:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/44850023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定