navneet83 / Cross-platform-AES-encryption

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

How can i use it in Swift 5 or 4 #62

Open tkhabbab opened 4 years ago

tkhabbab commented 4 years ago

In java they use this class now I want to use it in ios swift

public class CryptLib {

String privateKey = "KHATHOS^%$#@!Trn";

/**
 * Encryption mode enumeration
 */
private enum EncryptMode {
    ENCRYPT, DECRYPT;
}

// cipher to be used for encryption and decryption
Cipher _cx;

// encryption key and initialization vector
byte[] _key, _iv;

public CryptLib() throws NoSuchAlgorithmException, NoSuchPaddingException {
    // initialize the cipher with transformation AES/CBC/PKCS5Padding
    _cx = Cipher.getInstance("AES/CBC/PKCS5Padding");
    _key = new byte[32]; //256 bit key space
    _iv = new byte[16]; //128 bit IV

}

private String encryptDecrypt(String _inputText, String _encryptionKey,
                              EncryptMode _mode, String _initVector) throws UnsupportedEncodingException,
        InvalidKeyException, InvalidAlgorithmParameterException,
        IllegalBlockSizeException, BadPaddingException {

    String _out = "";// output string
    //_encryptionKey = md5(_encryptionKey);
    //System.out.println("key="+_encryptionKey);

    int len = _encryptionKey.getBytes("UTF-8").length; // length of the key provided

    if (_encryptionKey.getBytes("UTF-8").length > _key.length)
        len = _key.length;

    int ivlen = _initVector.getBytes("UTF-8").length;

    if (_initVector.getBytes("UTF-8").length > _iv.length)
        ivlen = _iv.length;

    System.arraycopy(_encryptionKey.getBytes("UTF-8"), 0, _key, 0, len);
    System.arraycopy(_initVector.getBytes("UTF-8"), 0, _iv, 0, ivlen);
    //KeyGenerator _keyGen = KeyGenerator.getInstance("AES");
    //_keyGen.init(128);

    SecretKeySpec keySpec = new SecretKeySpec(_key, "AES"); // Create a new SecretKeySpec
    // for the
    // specified key
    // data and
    // algorithm
    // name.

    IvParameterSpec ivSpec = new IvParameterSpec(_iv); // Create a new
    // IvParameterSpec
    // instance with the
    // bytes from the
    // specified buffer
    // iv used as
    // initialization
    // vector.

    // encryption
    if (_mode.equals(EncryptMode.ENCRYPT)) {
        // Potentially insecure random numbers on Android 4.3 and older.
        // Read
        // https://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html
        // for more info.
        _cx.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);// Initialize this cipher instance
        byte[] results = _cx.doFinal(_inputText.getBytes("UTF-8")); // Finish
        // multi-part
        // transformation
        // (encryption)
        _out = Base64.encodeToString(results, Base64.DEFAULT); // ciphertext
        // output
    }

    // decryption
    if (_mode.equals(EncryptMode.DECRYPT)) {
        _cx.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);// Initialize this ipher instance

        byte[] decodedValue = Base64.decode(_inputText.getBytes(),
                Base64.DEFAULT);
        byte[] decryptedVal = _cx.doFinal(decodedValue); // Finish
        // multi-part
        // transformation
        // (decryption)
        _out = new String(decryptedVal);
    }

// System.out.println(_out); return _out; // return encrypted/decrypted string }

/***
 * This function encrypts the plain text to cipher text using the key
 * provided. You'll have to use the same key for decryption
 *
 * @param _plainText
 *            Plain text to be encrypted
 * @param _key
 *            Encryption Key. You'll have to use the same key for decryption

// * @param _iv

Deepali-11 commented 1 year ago

I am unable to use this method "- (NSData )encrypt:(NSData )plainText key:(NSString )key iv:(NSString )iv" in my project. I can successfully encrypt a string but unable to encrypt a Data . Every time I got error "Can't convert Value of type Data to expected argument type String". Please resolve this issue as soon as Possible.