213 shaares
1 résultat
taggé
encrypt
J'ai commencé à regarder Bouncy Castle et je suis pas mal bloquée sur la compréhension des algorithmes de chiffrement...
Je met ici le début de ma réflexion :
package com.moi.search;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.paddings.BlockCipherPadding;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Base64;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
public class AESBouncyCastle {
private final BlockCipher AESCipher = new AESEngine();
private PaddedBufferedBlockCipher paddedBufferedBlockCipher;
private KeyParameter key;
public void setPadding(BlockCipherPadding bcp) {
this.paddedBufferedBlockCipher = new PaddedBufferedBlockCipher(AESCipher, bcp);
}
public SecretKey generateKey() throws NoSuchAlgorithmException {
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(256);
return kg.generateKey();
}
public String generateSecretKeyStringFormatted() throws NoSuchAlgorithmException {
SecretKey key = this.generateKey();
return this.convertToString(Base64.encode(key.getEncoded()));
}
public void setKey(byte[] key) {
this.key = new KeyParameter(key);
}
public void setKey(SecretKey key) {
this.setKey(key.getEncoded());
}
public byte[] retrieveKeyFromString(String key) {
return Base64.decode(key);
}
public String encrypt(String input) throws DataLengthException, InvalidCipherTextException {
byte[] result = this.processing(this.convertToBytes(input), true);
return this.convertToString(Base64.encode(result));
}
public String decrypt(String input) throws DataLengthException, InvalidCipherTextException {
byte[] bytesToDecrypt = Base64.decode(input);
return this.convertToString(this.processing(bytesToDecrypt, false));
}
private byte[] processing(byte[] input, boolean encrypt) throws DataLengthException, InvalidCipherTextException {
paddedBufferedBlockCipher.init(encrypt, key);
byte[] output = new byte[paddedBufferedBlockCipher.getOutputSize(input.length)];
int bytesWrittenOut = paddedBufferedBlockCipher.processBytes(input, 0, input.length, output, 0);
paddedBufferedBlockCipher.doFinal(output, bytesWrittenOut);
return output;
}
private byte[] convertToBytes(String content) {
return content.trim().getBytes(StandardCharsets.UTF_8);
}
private String convertToString(byte[] content) {
return new String(content, StandardCharsets.UTF_8).trim();
}
}
Exemple d'utilisation :
AESBouncyCastle cipherEngine = new AESBouncyCastle();
cipherEngine.setPadding(new PKCS7Padding());
String key = cipherEngine.generateSecretKeyStringFormatted();
cipherEngine.setKey(cipherEngine.retrieveKeyFromString(key));
cipherEngine.encrypt(stuffToEncrypt);
toto = cipherEngine.decrypt(stuffToDecrypt);