Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://android-developers.blogspot.in/2013/02/using-cryptography-to-store-credentials.html" rel="noreferrer">http://android-developers.blogspot.in/2013/02/using-cryptography-to-store-credentials.html</a></p> <p><a href="http://developer.android.com/reference/javax/crypto/SecretKeyFactory.html" rel="noreferrer">http://developer.android.com/reference/javax/crypto/SecretKeyFactory.html</a></p> <p>Check the above links. Use the below for reference.Modify the below according to your needs. </p> <p>Usage</p> <pre><code> try { DescEncrypter ec = new DescEncrypter(); byte[] cipherText =ec.encrypt("hi", "hello"); String enc = new String(cipherText,"UTF-8"); String decryp= ec.decrypt("hi", cipherText); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } </code></pre> <p>DescEncrypter.java</p> <pre><code> import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec; public class DescEncrypter { public static final int SALT_LENGTH = 20; public static final int PBE_ITERATION_COUNT = 200; //1024; private static final String PBE_ALGORITHM = "PBEWithSHA256And256BitAES-CBC-BC"; //algoritmo / modo / relleno private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding"; byte[] iv = "1234567890asdfgh".getBytes(); byte[] salt = "dfghjklpoiuytgftgyhj".getBytes(); public byte[] encrypt(String password, String cleartext) { byte[] encryptedText = null; try { PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, PBE_ITERATION_COUNT, 256); //Factoria para crear la SecretKey, debemos indicar el Algoritmo SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_ALGORITHM); SecretKey tmp = factory.generateSecret(pbeKeySpec); //Creamos una llave; SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); //Obtenemos la llave, solo informativo byte[] key = secret.getEncoded(); //La clase Cipher, se usa para cifrar mediante algoritmos de clave simétrica Cipher encryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM); //byte[] iv = generateIv(); IvParameterSpec ivspec = new IvParameterSpec(iv); //Accion, SecretKey, parameter specification for an initialization vector encryptionCipher.init(Cipher.ENCRYPT_MODE, secret, ivspec); //Realizamos el cifrado encryptedText = encryptionCipher.doFinal(cleartext.getBytes()); } catch (Exception e) { e.printStackTrace(); } return encryptedText; } public String decrypt(String password, byte[] encryptedText) { String cleartext = ""; try { PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, PBE_ITERATION_COUNT, 256); //Factoria para crear la SecretKey, debemos indicar el Algoritmo SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_ALGORITHM); SecretKey tmp = factory.generateSecret(pbeKeySpec); //Creamos una llave; SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); //Obtenemos la llave, solo informativo byte[] key = secret.getEncoded(); //La clase Cipher, se usa para cifrar mediante algoritmos de clave simétrica Cipher decryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM); //byte[] iv = generateIv(); IvParameterSpec ivspec = new IvParameterSpec(iv); //Accion, SecretKey, parameter specification for an initialization vector decryptionCipher.init(Cipher.DECRYPT_MODE, secret, ivspec); //Realizamos el descifrado byte[] decryptedText = decryptionCipher.doFinal(encryptedText); cleartext = new String(decryptedText); } catch (Exception e) { e.printStackTrace(); } return cleartext; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload