Last active
June 28, 2016 07:01
-
-
Save hitesh-dhamshaniya/10adc84d5101c6dd433c to your computer and use it in GitHub Desktop.
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 android.util.Base64; | |
import android.util.Log; | |
import java.io.UnsupportedEncodingException; | |
import java.security.InvalidKeyException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.NoSuchProviderException; | |
import java.security.Security; | |
import javax.crypto.BadPaddingException; | |
import javax.crypto.Cipher; | |
import javax.crypto.IllegalBlockSizeException; | |
import javax.crypto.NoSuchPaddingException; | |
import javax.crypto.ShortBufferException; | |
import javax.crypto.spec.SecretKeySpec; | |
/** | |
* Basic symmetric encryption example | |
*/ | |
public class Cryptography { | |
private byte[] keyBytes = "ABCD657865BHNKKK".getBytes(); | |
private Cipher cipher; | |
SecretKeySpec key; | |
public Cryptography() { | |
keyBytes = "ABCD657865BHNKKK".getBytes(); | |
key = new SecretKeySpec(keyBytes, "AES"); | |
try { | |
cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC"); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} catch (NoSuchProviderException e) { | |
e.printStackTrace(); | |
} catch (NoSuchPaddingException e) { | |
e.printStackTrace(); | |
} | |
} | |
public Cryptography(byte[] keyBytes) { | |
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); | |
key = new SecretKeySpec(keyBytes, "AES"); | |
this.keyBytes = keyBytes; | |
try { | |
cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC"); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} catch (NoSuchProviderException e) { | |
e.printStackTrace(); | |
} catch (NoSuchPaddingException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Method used to encryptiong | |
* return value is without Base64 encoding. | |
* | |
* @param valueTobeEncrypt | |
* @return | |
*/ | |
public byte[] encryption(String valueTobeEncrypt) { | |
//Cipher cipher = Cipher.getInstance("AES/ECB/ZeroBytePadding", "BC"); | |
try { | |
// encryption pass | |
cipher.init(Cipher.ENCRYPT_MODE, key); | |
byte[] input = valueTobeEncrypt.getBytes(); | |
byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; | |
int ctLength = cipher.update(input, 0, input.length, cipherText, 0); | |
ctLength += cipher.doFinal(cipherText, ctLength); | |
Log.e("==> ", " E Method == > Encode " + Base64.encodeToString(cipherText, Base64.DEFAULT)); | |
return cipherText; | |
} catch (IllegalBlockSizeException e) { | |
e.printStackTrace(); | |
return null; | |
} catch (BadPaddingException e) { | |
e.printStackTrace(); | |
return null; | |
} catch (ShortBufferException e) { | |
e.printStackTrace(); | |
return null; | |
} catch (InvalidKeyException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
public String decryptData(byte[] cipherText) { | |
key = new SecretKeySpec(keyBytes, "AES"); | |
try { | |
cipher.init(Cipher.DECRYPT_MODE, key); | |
int ctLength = cipherText.length; | |
byte[] plainText = new byte[cipher.getOutputSize(ctLength)]; | |
int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0); | |
ptLength += cipher.doFinal(plainText, ptLength); | |
Log.e("==> ", "D Method Decoded " + new String(plainText, 0, ptLength, "UTF-8")); | |
return new String(plainText, 0, ptLength, "UTF-8"); | |
} catch (InvalidKeyException e) { | |
e.printStackTrace(); | |
return null; | |
} catch (BadPaddingException e) { | |
e.printStackTrace(); | |
return null; | |
} catch (ShortBufferException e) { | |
e.printStackTrace(); | |
return null; | |
} catch (IllegalBlockSizeException e) { | |
e.printStackTrace(); | |
return null; | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
public String decrypt(String encryptedText) {
byte[] encryptedTextByte = Base64.decode(encryptedText, Base64.DEFAULT);
String decryptedText = null;
decryptedText = decryptData(encryptedTextByte);
return decryptedText;
}
Update Method for decrypt from string value