0

I have the python code encryption by AES. I've already made compatible it with android. I googled but i couldn't fine the same solution in IOS xcode. The big problem is to deal with options like padding, hashing key, iv size and MODE_CBC. Do anyone can help me?

import base64
import hashlib
from Crypto import Random
from Crypto.Cipher import AES


class AESCipher:

    def __init__(self, key):
        self.bs = 16
        self.key = hashlib.sha256(key.encode()).digest()

    def encrypt(self, message):
        message = self._pad(message)
        iv = Random.new().read(AES.block_size)
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return base64.b64encode(iv + cipher.encrypt(message)).decode('utf-8')

    def decrypt(self, enc):
        enc = base64.b64decode(enc)
        iv = enc[:AES.block_size]
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')

    def _pad(self, s):
        return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)

    @staticmethod
    def _unpad(s):
        return s[:-ord(s[len(s)-1:])]



t = AESCipher('this is my key')
enc = t.encrypt('Hello World')
print(enc.decode('utf-8'))



t2 = AESCipher('this is my key')
print(t2.decrypt(enc))
3
  • 1
    you may take a look into: github.com/RNCryptor/RNCryptor Commented Sep 30, 2015 at 10:46
  • Stack Overflow is not a code translation service. Have you tried anything? This should be rather easy with Common Crypto. Commented Sep 30, 2015 at 11:13
  • Yes. i tried some codes. after comment of @teamnorge , i find out RNCryoto is same as PyCrypto Commented Sep 30, 2015 at 11:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.