web-dev-qa-db-fra.com

Un moyen facile de chiffrer/déchiffrer une chaîne dans Android

Ma question est de savoir comment chiffrer un String :

String AndroidId;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.download_movie_activity);

  cancel = (Button)findViewById(R.id.img_cancle);

  linear= (LinearLayout)findViewById(R.id.progress);
  linear.setVisibility(View.GONE);

  String encrypted = "MzIyNTE2" + "OTQNzM4NTQ=";

  Log.e("Encrypt", encrypted);

  WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  WifiInfo wInfo = wifiManager.getConnectionInfo();
  AndroidId = wInfo.getMacAddress();

  AndroidId=encrypted;

Comment vous chiffrez votre AndroidId dans lequel je stocke une adresse MAC.

14
Sumit Kumar

Vous pouvez utiliser Cipher pour cela.

Cette classe fournit les fonctionnalités d'un chiffrement cryptographique pour le chiffrement et le déchiffrement. Il constitue le noyau de la structure JCE (Java Cryptographic Extension).

Exemple de cryptage et décryptage:

public static SecretKey generateKey() 
    throws NoSuchAlgorithmException, InvalidKeySpecException 
{ 
    return secret = new SecretKeySpec(password.getBytes(), "AES"); 
}

public static byte[] encryptMsg(String message, SecretKey secret)
    throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException 
{ 
   /* Encrypt the message. */
   Cipher cipher = null; 
   cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
   cipher.init(Cipher.ENCRYPT_MODE, secret); 
   byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8")); 
   return cipherText; 
}

public static String decryptMsg(byte[] cipherText, SecretKey secret) 
    throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException 
{
    /* Decrypt the message, given derived encContentValues and initialization vector. */
    Cipher cipher = null;
    cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secret); 
    String decryptString = new String(cipher.doFinal(cipherText), "UTF-8");
    return decryptString; 
}

Pour chiffrer:

SecretKey secret = generateKey();
EncUtil.encryptMsg(String toEncrypt, secret))

Pour déchiffrer:

EncUtil.decryptMsg(byte[] toDecrypt, secret))
27
Patrick R

LISEZ S'IL VOUS PLAÎT

Les nouvelles versions du SDK Android ne prennent plus en charge le fournisseur Crypto.

Si votre application utilisait setSeed() pour dériver des clés à partir de chaînes, vous devriez passer à utiliser SecretKeySpec pour charger directement des octets de clé bruts OU Utiliser une fonction de dérivation de clé réelle (KDF). 

Voir les conseils ici: 

http://Android-developers.blogspot.com/2016/06/security-crypto-provider-deprecated-in.html

7
Duna

En utilisant l'algorithme BlueFish, vous pouvez facilement chiffrer et déchiffrer n'importe quelle chaîne. Blowfish avec une taille de clé de 128 bits à 448 bits, est considéré comme un meilleur algorithme plus rapide. Essayez comme suit 

public class CryptUtil {
private static final String ALGORITHM = "Blowfish";
private static final String MODE = "Blowfish/CBC/PKCS5Padding";
private static final String IV = "abcdefgh";
private static final String KEY= "MyKey";

public static  String encrypt(String value ) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    SecretKeySpec secretKeySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
    Cipher cipher = Cipher.getInstance(MODE);
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(IV.getBytes()));
    byte[] values = cipher.doFinal(value.getBytes());
    return Base64.encodeToString(values, Base64.DEFAULT);
}

public static  String decrypt(String value) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    byte[] values = Base64.decode(value, Base64.DEFAULT);
    SecretKeySpec secretKeySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
    Cipher cipher = Cipher.getInstance(MODE);
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(IV.getBytes()));
    return new String(cipher.doFinal(values));
  }

}
0
Farid Haq