Created
April 13, 2018 12:27
-
-
Save erfanegtfi/a6fd8fa91960c93dba3f683b3cc1b4f8 to your computer and use it in GitHub Desktop.
Secret Key Spec Cipher
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.security.InvalidKeyException; | |
import java.security.NoSuchAlgorithmException; | |
import javax.crypto.Cipher; | |
import javax.crypto.NoSuchPaddingException; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.SecretKeySpec; | |
import pl.droidsonroids.gif.BuildConfig; | |
public class MCrypt { | |
private static Cipher dcipher; | |
private static Cipher ecipher; | |
private static SecretKey key; | |
public MCrypt() { | |
try { | |
key = new SecretKeySpec(FileManager.ReadByte(Lib.getUniquePsuedoID()), "AES"); | |
ecipher = Cipher.getInstance("AES"); | |
dcipher = Cipher.getInstance("AES"); | |
ecipher.init(1, key); | |
dcipher.init(2, key); | |
} catch (NoSuchAlgorithmException e) { | |
Log.d("EncryptDecryptStringWithDES_Error", "No Such Algorithm:" + e.getMessage()); | |
} catch (NoSuchPaddingException e2) { | |
Log.d("EncryptDecryptStringWithDES_Error", "No Such Padding:" + e2.getMessage()); | |
} catch (InvalidKeyException e3) { | |
Log.d("EncryptDecryptStringWithDES_Error", "Invalid Key:" + e3.getMessage()); | |
} | |
} | |
public String encrypt(String str) { | |
try { | |
return Base64.encodeToString(ecipher.doFinal(str.getBytes("UTF8")), 0); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
public String decrypt(String str) { | |
try { | |
if (str.equals(BuildConfig.FLAVOR)) { | |
return str; | |
} | |
return new String(dcipher.doFinal(Base64.decode(str, 0)), "UTF8"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment