eyalb
eyalb

Reputation: 3022

How to encrypt file in c# and decrypt it on android and IOS

i need to create files with data encrypted. i create the encrypted data on asp.net app.

now i need to read the file on android app and IOS app and decrypte the data.

what type of encryption i can use that will have library's for all kind of client?

for encription i use this code

public static string Encrypt(string plainText, string passPhrase)
    {
        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
        using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
        {
            byte[] keyBytes = password.GetBytes(keysize / 8);
            using (RijndaelManaged symmetricKey = new RijndaelManaged())
            {
                symmetricKey.Mode = CipherMode.CBC;
                using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes))
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                            cryptoStream.FlushFinalBlock();
                            byte[] cipherTextBytes = memoryStream.ToArray();
                            return Convert.ToBase64String(cipherTextBytes);
                        }
                    }
                }
            }
        }
    }

Upvotes: 0

Views: 1423

Answers (3)

riaz hasan
riaz hasan

Reputation: 1155

For cross encryption and decryption you can use XOR.Its pretty simple.

for IOS:

+(NSString *) EncryptOrDecrypt:(NSString *)string {

NSString *key=@"Your_Key";

// Create data object from the string
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];

// Get pointer to data to obfuscate
char *dataPtr = (char *) [data bytes];

// Get pointer to key data
char *keyData = (char *) [[key dataUsingEncoding:NSUTF8StringEncoding] bytes];

// Points to each char in sequence in the key
char *keyPtr = keyData;
int keyIndex = 0;

// For each character in data, xor with current value in key
for (int x = 0; x < [data length]; x++)
{
    // Replace current character in data with
    // current character xor'd with current key value.
    // Bump each pointer to the next character
    *dataPtr = *dataPtr ^ *keyPtr;
    dataPtr++;
    keyPtr++;

    // If at end of key data, reset count and
    // set key pointer back to start of key value
    if (++keyIndex == [key length])
        keyIndex = 0, keyPtr = keyData;
}

return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

For Android:

public static String EncryptOrDecrypt(String input) {
    char[] key = {'K', 'E', 'Y'}; //Your key in char array
    StringBuilder output = new StringBuilder();

    for(int i = 0; i < input.length(); i++) {
        output.append((char) (input.charAt(i) ^ key[i % key.length]));
    }

    return output.toString();
}

For C#:

string EncryptOrDecrypt(string text, string key)//Your Key as parameter
{
var result = new StringBuilder();

for (int c = 0; c < text.Length; c++)
    result.Append((char)((uint)text[c] ^ (uint)key[c % key.Length]));

return result.ToString();

}

Use the same function for encryption and decryption.You must use same KEY for encryption and decryption.

Upvotes: 2

zaph
zaph

Reputation: 112857

Given the level of knowledge of encryption the best bet is to use RNCryptor. It is available for multiple languages including Objective-C and C#.

RNCryptor handles all the details necessary for successful secure encryption such as key derivation, authentication and versioning. Getting all this correct is error prone for those not well versed in using encryption.

Securing the key remains a difficult problem. Using the keychain to store the key is the best practice. The problem is how the key will be shared between the app and the server, if it is compiled into the app that is a major weakness that is virtually impossible to overcome.

Upvotes: 2

TheEye
TheEye

Reputation: 9346

Have a look at this framework https://github.com/RNCryptor/RNCryptor - it provides a cross-platform way of encrypting/decrypting.

Upvotes: 0

Related Questions