1

I am new in swift. I have AES code in android so i need AES code for swift 2. I found a lot AES code but did not found the same code for swift 2 and Android. please, advise me.

this code for android:

public class AES {
public static String SALT = "8e0b86611d5922ffd57fcc053644ff6d73459b2b";
public static SecretKeySpec getKey(String myKey) {
    MessageDigest sha = null;
    byte[] key;
    try {
        key = myKey.getBytes("UTF-8");
        sha = MessageDigest.getInstance("SHA-1");
        key = sha.digest(key);
        key = Arrays.copyOf(key, 16); // use only first 128 bit
        return new SecretKeySpec(key, "AES");
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

public static String encrypt(String strToEncrypt, String password) {
    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, getKey(password));
        return Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
    } catch (Exception e) {
        System.out.println("Error while encrypting: " + e.toString());
    }
    return null;

}

public static String decrypt(String strToDecrypt, String password) {
    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, getKey(password));
        return new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
    } catch (Exception e) {
        System.out.println("Error while decrypting: " + e.toString());
    }
    return null;
}


public static void main(String args[]) {
    String text = "Hello World!";
    String encrypt = AES.encrypt(text,SALT);

    System.out.println("String to Encrypt: " + text);
    System.out.println("Encrypted: " + encrypt);

    System.out.println("String To Decrypt : " + encrypt);
    System.out.println("Decrypted : " + AES.decrypt(encrypt,SALT));
}

}
centic
  • 15,565
  • 9
  • 68
  • 125
Clever
  • 401
  • 1
  • 6
  • 17
  • if you can, plz write aes code for swift – Clever Nov 03 '15 at 11:33
  • There are several Swift AES examples on SO, search for them. – zaph Nov 03 '15 at 23:36
  • @zaph, i have already used this https://github.com/krzyzanowskim/CryptoSwift but not found AES encryption like android code. – Clever Nov 04 '15 at 02:41
  • As in the comment, do not use CryptoSwift if you want either performance of security. You are going to have to understand what the Android code is doing. There is a message digest and a SALT which looks like it is used as the encryption key but it looks like 40 hex-ascii characters, AES keys are 16, 24 of 32 bytes (128, 192 or 256 bits) so that does not make sense. Also ECB mode is being used which is not secure. Your best choice is to use RNCryptor, there are versions in several languages. – zaph Nov 04 '15 at 05:34
  • @zaph how to use RNCryptor ?. i found this https://github.com/RNCryptor/RNCryptor but never use this . plz give me example how to use encode and decode – Clever Nov 04 '15 at 06:44

1 Answers1

-1

I would recommend against writing your own AES encryption and using CryptoSwift instead.

Bea Boxler
  • 302
  • 2
  • 8
  • I would recommend against using CryptoSwift, among other things CryptoSwift AES is 500 to 1000 times slower than the Apple supplied Common Crypto because it does not use the iOS hardware crypto engine. Additionally it is not well vetted and may contain errors. CryptoSwift suffers from the same recommendation "against writing your own AES encryption" because that is what the CryptoSwift author did. – zaph Nov 03 '15 at 23:15