J'essaie de chiffrer et de déchiffrer en utilisant AES/CBC/NoPadding en JAVA. J'ai fait le cryptage à la fois en JAVA et en PHP en utilisant (mcrypt) et j'ai obtenu le même résultat, en utilisant la même clé et iv. Cependant, lorsque j'essaie de déchiffrer en JAVA, j'obtiens le mot correctement mais toujours avec des caractères supplémentaires. J'ai lu d'autres questions et j'ai constaté que je devais ajouter un rembourrage. J'ai donc ajouté Padding5 mais j'ai obtenu le même résultat. Quoi qu'il en soit, j'en ai besoin sans remplissage car c'est ainsi que cela fonctionne en PHP. Toute aide est appréciée. Mon code est ci-dessous et le résultat est ici : ] 2
public class RijndaelCrypt {
//private String key = "2a4e2471c77344b3bf1de28ab9aa492a444abc1379c3824e3162664a2c2b811d";
private static String iv = "beadfacebadc0fee";
private static String hashedKey = "6a2dad9f75b87f5bdd365c9de0b9c842";
private static Cipher cipher;
public static String decrypt(String text) throws UnsupportedEncodingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException {
SecretKeySpec keyspec = new SecretKeySpec(hashedKey.getBytes("UTF-8"), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes("UTF-8"));
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] decodedValue = Base64.decode(text.getBytes("UTF-8"));
byte[] decryptedVal = cipher.doFinal(decodedValue);
return new String(decryptedVal);
}
public static String encryptNew(String data) throws Exception {
cipher = Cipher.getInstance("AES/CBC/NoPadding");
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes("UTF-8");
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(hashedKey.getBytes("UTF-8"), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes("UTF-8"));
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return DatatypeConverter.printBase64Binary(encrypted);
}
public static void main (String [] args) throws Exception
{
Security.addProvider(new BouncyCastleProvider());
String data = "Hello";
System.out.println("New Decrypted: " + RijndaelCrypt.decrypt(RijndaelCrypt.encryptNew(data)));
System.out.println("New Encryption: " + RijndaelCrypt.encryptNew(data));
}
}
The PHP mcrypt wrapper (or underlying mcrypt library) pads with zero bytes up to the block length (zero to 15 padding bytes, if 16 is the block size of the cipher). After that the blocks are encrypted by the cipher.
When decrypting in Java you need to manually remove any zero bytes from the right hand side of the plaintext after decryption using NoPadding
. The zero valued padding bytes can of course be seen when hex-encoding the decrypted plaintext. However when outputting a string the zero bytes are either left out or converted to a replacement character (depending on the character set and terminal).
Notez que le remplissage par zéro de PHP a un gros inconvénient : si le texte en clair se termine par un ou plusieurs octets de valeur zéro, il peut être supprimé du texte en clair déchiffré par n'importe quelle routine de décompression. C'est pourquoi le remplissage PKCS#7 (qui remplit de 1 à 16 octets) doit être préféré.
Notez également que PHP doit rtrim("\0")
en fait supprimer lui-même les zéros ; mcrypt les laisse simplement là, mais ils ne seront généralement pas imprimés.
Notez que les bibliothèques cryptographiques Bouncy Castle ont également ZeroPadding
en option. Cependant, il s'agit d'un remplissage nul de 1 à 16 octets (c'est-à-dire qu'il remplit/décompresse toujours), il est donc incompatible avec le remplissage défini utilisé par PHP mcrypt et peut échouer si la taille du texte en clair peut être divisée par la taille du bloc du chiffrement .
Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.
En cas d'infraction, veuillez [email protected] Supprimer.
laisse moi dire quelques mots