bcgit / pc-dart

Pointy Castle - Dart Derived Bouncy Castle APIs
MIT License
237 stars 122 forks source link

How to decrypt a string encrypted with Python. #131

Closed JacoFourie closed 3 years ago

JacoFourie commented 3 years ago

Hi.

Sorry to bug you. I need to decrypt some data that is encrypted in Python using Blowfish.

Here is the Python code that will create a encrypted base64 string. I need the Dart code to decrypt it. Can somebody please help me with it?

`#!/usr/bin/python from Crypto.Cipher import Blowfish import base64

blocksize = Blowfish.block_size keystr = b'this is my key' iv = b'abcdefgh'

cipher = Blowfish.new(keystr, Blowfish.MODE_CBC, iv)

data_string = 'This is my data'

while len(data_string) % blocksize !=0: data_string = data_string + ' ' encrypted_data = cipher.encrypt(data_string) encoded = base64.b64encode(encrypted_data)

print(b'Encrypted Base64 String : ' + encoded)`

This is the output.

Encrypted Base64 String : gxZXDnt1ek/Ivx91kjwJXg==

So I need to take the string

gxZXDnt1ek/Ivx91kjwJXg==

back to

This is my data

AKushWarrior commented 3 years ago

Hi, sorry, I wrote and deleted an earlier comment which was generally inaccurate about the state of Blowfish. We don't currently support Blowfish in this library; below is my justification for why adding Blowfish might be a poor use of developer resources.

JacoFourie commented 3 years ago

OK does it support the rijndael algorithm?

JacoFourie commented 3 years ago

Here is the Python code to use AES.

#!/usr/bin/python
from Crypto.Cipher import AES
import base64

blocksize = 16
keystr = b'mysimplekey     '
iv = b'123456          '
data_string = 'This is my data'

#pad with spaces until 16 bytes
while len(data_string) % blocksize !=0: data_string = data_string + ' '

cipher = AES.new(keystr, AES.MODE_CBC, iv)

encrypted_data = cipher.encrypt(data_string)
encoded = base64.b64encode(encrypted_data)

print(b'Encrypted Base64 String : ' + encoded)

The output is

Encrypted Base64 String : zd3qRzdrFGsoxjJ2iJbTyw==

So what will the code be in dart to use AES to get the text

zd3qRzdrFGsoxjJ2iJbTyw==

back to

This is my data

JacoFourie commented 3 years ago

OK I got it working. Is this the best way?

Uint8List aesCbcDecrypt(Uint8List key, Uint8List iv, Uint8List cipherText) {
  // Create a CBC block cipher with AES, and initialize with key and IV

  final cbc = pointy.CBCBlockCipher(pointy.AESFastEngine())
    ..init(false,
        pointy.ParametersWithIV(pointy.KeyParameter(key), iv)); // false=decrypt

  // Decrypt the cipherText block-by-block

  final paddedPlainText = Uint8List(cipherText.length); // allocate space

  var offset = 0;
  while (offset < cipherText.length) {
    offset += cbc.processBlock(cipherText, offset, paddedPlainText, offset);
  }
  assert(offset == cipherText.length);

  return paddedPlainText;
}
String dc = String.fromCharCodes((aesCbcDecrypt(
      Uint8List.fromList('mysimplekey     '.codeUnits),
      Uint8List.fromList('123456          '.codeUnits),
      base64.decode('zd3qRzdrFGsoxjJ2iJbTyw=='))));
AKushWarrior commented 3 years ago

Yes, that would be the best way to encrypt using AES in PointyCastle.

JacoFourie commented 3 years ago

OK thank you.

sagar-doshaheen commented 1 year ago

Hello @JacoFourie, can you please share a working example of both encryption and decryption using the above mentioned methods?