navneet83 / Cross-platform-AES-encryption

Basic cross platform AES encryption
Apache License 2.0
320 stars 152 forks source link

It does not work for iOS #35

Open i5kender opened 8 years ago

i5kender commented 8 years ago

i have a one c# web service and i can decript and encript c# webservice, android project and c# client project. but i can not implement pakhee ios code for my ios application. There are a lot of error in ios classes. Could you fix ios classes and give usage example for ios application?

MVakas commented 8 years ago

Not working for me for iOS as well. It gives too many errors.

navneet83 commented 8 years ago

@i5kender @MVakas I had tested all the code provided in this library. Unfortunately, I won't be able to help you with the information you have provided. What are the errors which you are getting ? Can you create a gist which I can look at ?

i5kender commented 8 years ago

I have used https://github.com/benoitsan/BBAES classes for ios encrption. I have done. Thanks a lot navneet

rishbaranwal commented 6 years ago

I am stuck somewhere in between, please help

This is the function I am using to decrypt in iOS

func aesCBCDecrypt(data: Data, keyData: Data) throws -> Data? {
    let keyLength = keyData.count
    let validKeyLengths = [kCCKeySizeAES128, kCCKeySizeAES192, kCCKeySizeAES256]
    if validKeyLengths.contains(keyLength) == false {
        throw AESError.KeyError(("Invalid key length", keyLength))
    }
    var numBytesDecrypted: size_t = 0
    let options   = CCOptions(kCCOptionPKCS7Padding)
    let cryptLength  = size_t(data.count)
    var cryptData = Data(count: cryptLength)

    let cryptStatus = cryptData.withUnsafeMutableBytes {cryptBytes in
        data.withUnsafeBytes {dataBytes in
            keyData.withUnsafeBytes {keyBytes in
                CCCrypt(CCOperation(kCCDecrypt),
                        CCAlgorithm(kCCAlgorithmAES),
                        options,
                        keyBytes, keyLength + 1,
                        keyBytes,
                        dataBytes, data.count,
                        cryptBytes, cryptLength,
                        &numBytesDecrypted)
            }
        }
    }

This is the function I am using to encrypt in .net

public string EncryptData(string textData, string Encryptionkey) { RijndaelManaged objrij = new RijndaelManaged { //set the mode for operation of the algorithm
Mode = CipherMode.CBC, //set the padding mode used in the algorithm.
Padding = PaddingMode.PKCS7, //set the size, in bits, for the secret key.
KeySize = 256, //set the block size in bits for the cryptographic operation.
BlockSize = 128 }; //set the symmetric key that is used for encryption & decryption.
byte[] passBytes = Encoding.UTF8.GetBytes(Encryptionkey); //set the initialization vector (IV) for the symmetric algorithm
byte[] EncryptionkeyBytes = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

                //int len = passBytes.Length;
                //if (len > EncryptionkeyBytes.Length)
                //{
                //     len = EncryptionkeyBytes.Length;
                //}
                //Array.Copy(passBytes, EncryptionkeyBytes, len);

                objrij.Key = passBytes;
                objrij.IV = EncryptionkeyBytes;

                //Creates symmetric AES object with the current key and initialization vector IV.   
                ICryptoTransform objtransform = objrij.CreateEncryptor();
                byte[] textDataByte = Encoding.UTF8.GetBytes(textData);
                //Final transform the test string. 
                return Convert.ToBase64String(objtransform.TransformFinalBlock(textDataByte, 0, textDataByte.Length));
         }

I am not getting the expected output.

Input:

Plain text: dd00bd64-38d6-4d68-b0fc-2bc6389ee9b3 key: abcdef0123456789abcdef0123456789

Output:

\u{05}\u{06}ST\u{07}\u{02}\u{06}\u{05}\u{1F}\0\u{0C}Q\0\u{1A}\u{0C}]68-b0fc-2bc6389ee9b3

Any help is appreciated