Created
June 26, 2014 07:37
-
-
Save jorgevila/ffb35687dd669a504f0f to your computer and use it in GitHub Desktop.
CryptHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.UnsupportedEncodingException; | |
import java.math.BigInteger; | |
import java.security.InvalidKeyException; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.Arrays; | |
import javax.crypto.BadPaddingException; | |
import javax.crypto.Cipher; | |
import javax.crypto.CipherInputStream; | |
import javax.crypto.CipherOutputStream; | |
import javax.crypto.IllegalBlockSizeException; | |
import javax.crypto.NoSuchPaddingException; | |
import javax.crypto.spec.SecretKeySpec; | |
/* | |
* http://stackoverflow.com/questions/6788018/android-encryption-decryption-with-aes | |
* http://www.androidsnippets.com/encryptdecrypt-strings | |
*/ | |
/** | |
* Helper class for crypt issues. | |
*/ | |
public final class CryptHelper { | |
private static final String TAG = CryptHelper.class.getSimpleName(); | |
private static final String ALGORITHM = "AES"; | |
/** | |
* Private constructor. | |
*/ | |
private CryptHelper() { | |
} | |
/** | |
* Crypt text based on seed, using AES algorithm. | |
* | |
* @param seed Shared secret | |
* @param cleartext Value to encrypt | |
* @return hexadecimal value of crypted text or null | |
* @throws NoSuchAlgorithmException | |
* @throws BadPaddingException | |
* @throws IllegalBlockSizeException | |
* @throws NoSuchPaddingException | |
* @throws InvalidKeyException | |
*/ | |
public static String encrypt(String seed, String cleartext) throws NoSuchAlgorithmException, | |
InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, | |
BadPaddingException { | |
if (seed != null && cleartext != null) { | |
final byte[] rawKey = getRawKey(seed.getBytes()); | |
final byte[] result = encrypt(rawKey, cleartext.getBytes()); | |
return toHex(result); | |
} else { | |
return null; | |
} | |
} | |
/** | |
* Decrypt text provided with seed using AES. | |
* | |
* @param seed Shared secret | |
* @param encrypted Encrypted value to decrypt | |
* @return Decrypted value or null | |
* @throws NoSuchAlgorithmException | |
* @throws BadPaddingException | |
* @throws IllegalBlockSizeException | |
* @throws NoSuchPaddingException | |
* @throws InvalidKeyException | |
*/ | |
public static String decrypt(String seed, String encrypted) throws NoSuchAlgorithmException, | |
InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, | |
BadPaddingException { | |
if (seed != null && encrypted != null) { | |
final byte[] rawKey = getRawKey(seed.getBytes()); | |
final byte[] enc = toByte(encrypted); | |
final byte[] result = decrypt(rawKey, enc); | |
return new String(result); | |
} else { | |
return null; | |
} | |
} | |
/** | |
* Compute key based on seed. 128 bit. | |
* | |
* @param seed Shared secret | |
* @return raw Key byte array | |
* @throws NoSuchAlgorithmException | |
*/ | |
private static byte[] getRawKey(byte[] seed) throws NoSuchAlgorithmException { | |
final byte[] keySeed = Arrays.copyOf(seed, 16); | |
return keySeed; | |
} | |
/** | |
* Encrypt clear byte array with key. | |
* | |
* @param rawKey Raw bytes for key | |
* @param clear Raw bytes for value to crypt | |
* @return The encrypted value | |
* @throws NoSuchPaddingException | |
* @throws NoSuchAlgorithmException | |
* @throws InvalidKeyException | |
* @throws BadPaddingException | |
* @throws IllegalBlockSizeException | |
* @throws Exception In case of failure | |
*/ | |
private static byte[] encrypt(byte[] rawKey, byte[] clear) throws NoSuchAlgorithmException, | |
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, | |
BadPaddingException { | |
final SecretKeySpec skeySpec = new SecretKeySpec(rawKey, ALGORITHM); | |
final Cipher cipher = Cipher.getInstance(ALGORITHM); | |
cipher.init(Cipher.ENCRYPT_MODE, skeySpec); | |
return cipher.doFinal(clear); | |
} | |
/** | |
* Decrypt value bytes with key raw bytes. | |
* | |
* @param rawKey Raw bytes for key | |
* @param clear Raw bytes for value to decrypt | |
* @return The clear value | |
* @throws NoSuchPaddingException | |
* @throws NoSuchAlgorithmException | |
* @throws InvalidKeyException | |
* @throws BadPaddingException | |
* @throws IllegalBlockSizeException | |
* @throws Exception In case of failure | |
*/ | |
private static byte[] decrypt(byte[] rawKey, byte[] encrypted) throws NoSuchAlgorithmException, | |
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, | |
BadPaddingException { | |
final SecretKeySpec skeySpec = new SecretKeySpec(rawKey, ALGORITHM); | |
final Cipher cipher = Cipher.getInstance(ALGORITHM); | |
cipher.init(Cipher.DECRYPT_MODE, skeySpec); | |
return cipher.doFinal(encrypted); | |
} | |
/** | |
* Convert hexadecimal string to bytes. | |
* | |
* @param hexString Hexadecimal string | |
* @return byte array | |
*/ | |
private static byte[] toByte(String hexString) { | |
final int len = hexString.length() / 2; | |
final byte[] result = new byte[len]; | |
for (int i = 0; i < len; i++) { | |
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue(); | |
} | |
return result; | |
} | |
/** | |
* Convert byte array to hexadecimal string. | |
* | |
* @param buf Byte array | |
* @return Hexadecimal string | |
*/ | |
private static String toHex(byte[] buf) { | |
if (buf == null) { | |
return ""; | |
} | |
final StringBuffer result = new StringBuffer(2 * buf.length); | |
for (int i = 0; i < buf.length; i++) { | |
appendHex(result, buf[i]); | |
} | |
return result.toString(); | |
} | |
private static final String HEX = "0123456789ABCDEF"; | |
private static void appendHex(StringBuffer sb, byte b) { | |
sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f)); | |
} | |
/** | |
* Compute MD5 hash on cursor on text. | |
* | |
* @param text | |
* @return MD5 representation | |
*/ | |
public static String computeMD5(final String text) { | |
String md5 = null; | |
try { | |
final byte[] md5Input = text.substring(text.indexOf("\n") + 1).getBytes("UTF-8"); | |
final MessageDigest digest = MessageDigest.getInstance("MD5"); | |
digest.update(md5Input, 0, md5Input.length); | |
md5 = new BigInteger(1, digest.digest()).toString(16); | |
} catch (final NoSuchAlgorithmException e) { | |
; // Do nothing | |
} catch (final UnsupportedEncodingException e) { | |
; // Do nothing | |
} | |
return md5; | |
} | |
/* | |
* File Encryption See http://www.macs.hw.ac.uk/~ml355/lore/FileEncryption.java | |
*/ | |
/** | |
* Get Cypher output stream for output stream in order to encrypt stream output. | |
* | |
* @param seed Key seed | |
* @param out OutputStream | |
* @throws NoSuchAlgorithmException | |
*/ | |
public static OutputStream getCipherOutputStream(String seed, OutputStream out) | |
throws IOException, InvalidKeyException, NoSuchAlgorithmException, | |
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, | |
BadPaddingException { | |
if (seed != null && out != null) { | |
final byte[] rawKey = getRawKey(seed.getBytes()); | |
final SecretKeySpec skeySpec = new SecretKeySpec(rawKey, ALGORITHM); | |
final Cipher cipher = Cipher.getInstance(ALGORITHM); | |
cipher.init(Cipher.ENCRYPT_MODE, skeySpec); | |
final CipherOutputStream os = new CipherOutputStream(out, cipher); | |
return os; | |
} else { | |
return null; | |
} | |
} | |
/** | |
* Get cypher input stream for input stream in order to decrypt stream input. | |
* | |
* @param seed Key seed | |
* @param in InputStream | |
* @throws NoSuchAlgorithmException | |
* @throws NoSuchPaddingException | |
*/ | |
public static InputStream getCipherInputStream(String seed, InputStream in) throws IOException, | |
InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException { | |
if (seed != null && in != null) { | |
final byte[] rawKey = getRawKey(seed.getBytes()); | |
final SecretKeySpec skeySpec = new SecretKeySpec(rawKey, ALGORITHM); | |
final Cipher cipher = Cipher.getInstance(ALGORITHM); | |
cipher.init(Cipher.ENCRYPT_MODE, skeySpec); | |
final CipherInputStream is = new CipherInputStream(in, cipher); | |
return is; | |
} else { | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment