CrossPlatform CryptLib This cross platform CryptLib uses AES 256 for encryption. This library can be used for encryptoion and de-cryption of string on iOS, Android and Windows platform. Features: 1. 256 bit AES encryption 2. Random IV generation. 3. Provision for SHA256 hashing of ke
| 32 | *****************************************************************/ |
| 33 | |
| 34 | public class CryptLib { |
| 35 | |
| 36 | /** |
| 37 | * Encryption mode enumeration |
| 38 | */ |
| 39 | private enum EncryptMode { |
| 40 | ENCRYPT, DECRYPT; |
| 41 | } |
| 42 | |
| 43 | // cipher to be used for encryption and decryption |
| 44 | Cipher _cx; |
| 45 | |
| 46 | // encryption key and initialization vector |
| 47 | byte[] _key, _iv; |
| 48 | |
| 49 | public CryptLib() throws NoSuchAlgorithmException, NoSuchPaddingException { |
| 50 | // initialize the cipher with transformation AES/CBC/PKCS5Padding |
| 51 | _cx = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
| 52 | _key = new byte[32]; //256 bit key space |
| 53 | _iv = new byte[16]; //128 bit IV |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Note: This function is no longer used. |
| 58 | * This function generates md5 hash of the input string |
| 59 | * @param inputString |
| 60 | * @return md5 hash of the input string |
| 61 | */ |
| 62 | public static final String md5(final String inputString) { |
| 63 | final String MD5 = "MD5"; |
| 64 | try { |
| 65 | // Create MD5 Hash |
| 66 | MessageDigest digest = java.security.MessageDigest |
| 67 | .getInstance(MD5); |
| 68 | digest.update(inputString.getBytes()); |
| 69 | byte messageDigest[] = digest.digest(); |
| 70 | |
| 71 | // Create Hex String |
| 72 | StringBuilder hexString = new StringBuilder(); |
| 73 | for (byte aMessageDigest : messageDigest) { |
| 74 | String h = Integer.toHexString(0xFF & aMessageDigest); |
| 75 | while (h.length() < 2) |
| 76 | h = "0" + h; |
| 77 | hexString.append(h); |
| 78 | } |
| 79 | return hexString.toString(); |
| 80 | |
| 81 | } catch (NoSuchAlgorithmException e) { |
| 82 | e.printStackTrace(); |
| 83 | } |
| 84 | return ""; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * |
| 89 | * @param _inputText |
| 90 | * Text to be encrypted or decrypted |
| 91 | * @param _encryptionKey |
nothing calls this directly
no outgoing calls
no test coverage detected