Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tkhabbab opened this issue Oct 22, 2019 · 1 comment
Open

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

tkhabbab opened this issue Oct 22, 2019 · 1 comment

Comments

@tkhabbab
Copy link

tkhabbab commented Oct 22, 2019

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
* initialization Vector
* @return returns encrypted (cipher) text
* @throws InvalidKeyException
* @throws UnsupportedEncodingException
* @throws InvalidAlgorithmParameterException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/

public String encrypt(String _plainText, String _key)
        throws InvalidKeyException, UnsupportedEncodingException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException,
        BadPaddingException {

// return encryptDecrypt(_plainText, _key, EncryptMode.ENCRYPT, _iv);
String key = _key;
try {
key = SHA256("HALTech^%$#@!Trn", 32);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}

    return encryptDecrypt(_plainText, key, EncryptMode.ENCRYPT, _key);
}

/***
 * This funtion decrypts the encrypted text to plain text using the key
 * provided. You'll have to use the same key which you used during
 * encryprtion
 *
 * @param _encryptedText
 *            Encrypted/Cipher text to be decrypted
 * @param _key
 *            Encryption key which you used during encryption

// * @param _iv
* initialization Vector
* @return encrypted value
* @throws InvalidKeyException
* @throws UnsupportedEncodingException
* @throws InvalidAlgorithmParameterException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public String decrypt(String _encryptedText, String _key)
throws InvalidKeyException, UnsupportedEncodingException,
InvalidAlgorithmParameterException, IllegalBlockSizeException,
BadPaddingException {
String key = _key;
try {
key = SHA256("HALTech^%$#@!Trn", 32);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return encryptDecrypt(_encryptedText, key, EncryptMode.DECRYPT, _key);
// return encryptDecrypt(_encryptedText, _key, EncryptMode.DECRYPT, _iv);
}

/***
 * This function computes the SHA256 hash of input string
 * @param text input text whose SHA256 hash has to be computed
 * @param length length of the text to be returned
 * @return returns SHA256 hash of input text
 * @throws NoSuchAlgorithmException
 * @throws UnsupportedEncodingException
 */

public static String SHA256 (String text, int length) throws NoSuchAlgorithmException, UnsupportedEncodingException {

    String resultStr;
    MessageDigest md = MessageDigest.getInstance("SHA-256");

    md.update(text.getBytes("UTF-8"));
    byte[] digest = md.digest();

    StringBuffer result = new StringBuffer();
    for (byte b : digest) {
        result.append(String.format("%02x", b)); //convert to hex
    }
    //return result.toString();

    if(length > result.toString().length())
    {
        resultStr = result.toString();
    }
    else
    {
        resultStr = result.toString().substring(0, length);
    }

    return resultStr;

}

}

@Deepali-11
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants