J'ai cherché presque tous les sujets ici sur ce sujet mais je n'arrive toujours pas à comprendre ce qui ne va pas. Où j'ai créé 3 EditText
sur mon application android: EditText
où vous insérez la clé/mot de passe pour crypter et décrypter; EditText2
où est affiché le texte chiffré; EditText3
où il est affiché le texte déchiffré.Blowfish sur studio android pour le cryptage/décryptage
Comme c'est toujours un test précoce, j'ai mis le message ou la chaîne à Crypt comme variable dans l'application.
Le problème est que le cryptage donne quelque chose comme un algorithme blowfish, donc pas de problème là (ça se termine par 2 == donc je pense que ça marche bien). J'ai également essayé de décoder la chaîne avant le décryptage ou en utilisant le cryptage byte[]
sans aucun résultat. Le déchiffrement ne restitue pas le texte String original, mais donne quelque chose de plus gros que le texte chiffré. Je n'ai pas de préférence sur le mode de blowfish donc j'ai commencé facile comme Blowfish/CFB/NoPadding.
str_key
, str2
et str3
sont déclarés publics pour le moment. str2
définira le texte pour le champ EditText2
et str3
définira le texte pour le champ EditText3
. Un exemple de sortie: Example of output generated from the app
Voici le code:
public void encrypt(){
//encrypt
EditText mEdit = (EditText)findViewById(R.id.editText);
str_key = (String) mEdit.getText().toString();
int iterationCount = 1000;
int keyLength = 256;
int saltLength = keyLength/8;
SecureRandom random = new SecureRandom();
byte[] salt = new byte[saltLength];
random.nextBytes(salt);
KeySpec keySpec = new PBEKeySpec(str_key.toCharArray(), salt,
iterationCount, keyLength);
SecretKeyFactory keyFactory = null;
try {
keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte[] keyBytes = new byte[0];
try {
keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
SecretKey key = new SecretKeySpec(keyBytes, "Blowfish");
Cipher cipher = null;
try {
cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
}
if (cipher == null || key == null) {
//throw new Exception("Invalid key or cypher");
str2="error";
}
else {
byte[] iv = new byte[cipher.getBlockSize()];
random.nextBytes(iv);
IvParameterSpec ivParams = new IvParameterSpec(iv);
try {
cipher.init(Cipher.ENCRYPT_MODE, key,ivParams);
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
try {
raw = cipher.doFinal(message.getBytes("UTF-8"));
} catch (IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException e) {
e.printStackTrace();
}
str2 = Base64.encodeToString(raw,Base64.DEFAULT);
}
}
Voici la fonction Décrypter:
public void decrypt(){
int iterationCount = 1000;
int keyLength = 256;
int saltLength = keyLength/8;
SecureRandom random = new SecureRandom();
byte[] salt = new byte[saltLength];
random.nextBytes(salt);
KeySpec keySpec = new PBEKeySpec(str_key.toCharArray(), salt, iterationCount, keyLength);
SecretKeyFactory keyFactory = null;
try {
keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte[] keyBytes = new byte[0];
try {
keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
SecretKey key = new SecretKeySpec(keyBytes, "Blowfish");
Cipher cipher2 = null;
try {
cipher2 = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
}
iv = new byte[cipher2.getBlockSize()];
random.nextBytes(iv);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
try {
cipher2.init(Cipher.DECRYPT_MODE, key, ivSpec);
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
byte[] decryptedBytes = null;
byte[] app= Base64.decode(str2,Base64.DEFAULT);
try {
decryptedBytes = cipher2.doFinal(app);
} catch (IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}
str3 = Base64.encodeToString(decryptedBytes,Base64.DEFAULT);
}
Merci pour votre réponse. J'ai également essayé d'utiliser le décodage base64 et aussi, d'utiliser le tableau d'octets codés "pre" d'origine pour le décryptage sans résultat. Pouvez-vous faire un exemple de ce que je devrais faire? Devrais-je simplement changer UTF-8 avec KDF.PBKDF2? J'ai également essayé de générer la IV comme: IvParameterSpec ivSpec = new IvParameterSpec (keySpec.getEncoded()); // dans la fonction de décryptage; mais j'ai une erreur d'exécution. Pouvez-vous me donner un exemple pour ça aussi? – Micene
Comment puis-je utiliser HMAC pour éviter de changer le texte cipherText? – Micene
C'est tout ce que vous pouvez google recherche, vous n'apprendrez rien si je viens d'écrire le code pour vous! Si vous l'avez essayé même avec le décodage du base64, vous avez simplement fait quelque chose de mal. Essayez de modifier le code dans votre message d'origine à ce que vous avez essayé et je vais vous aider à partir de là. –