Encriptar Y Desencriptar Texto En Java (Sin Errores De SonarQube)

Higarigaoka - Odaiba, Sin comentarios

Hola,

Si requieren realizar la encriptación de una cadena y luego desencriptar esa cadena sin que se les genere errores o advertencias del SonarQube, pueden utilizar los siguientes métodos: 

Método para encriptar una cadena:

 /**

 * variables for text encryption
*/
private static final int GCM_IV_LENGTH = 12;
private static final int GCM_TAG_LENGTH = 16;

/**
* Method in charge of encrypting a text, using the AES SecretKey.
*
* @param pText Define the text to encrypt
* @return a String with the encrypted text
*/
public static String encryptedText(String pText) {

String encryptedBase64 = null;

/*
* key is 16 zero bytes
*/
SecretKey secretKey = new SecretKeySpec(new byte[16], "AES");

try {

byte[] iv = new byte[GCM_IV_LENGTH];
(
new SecureRandom()).nextBytes(iv);

Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
GCMParameterSpec ivSpec = new GCMParameterSpec(GCM_TAG_LENGTH * Byte.SIZE, iv);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);

byte[] cipherText = cipher.doFinal(pText.getBytes(StandardCharsets.UTF_8));
byte[] encrypted = new byte[iv.length + cipherText.length];
System.arraycopy(iv, 0, encrypted, 0, iv.length);
System.arraycopy(cipherText, 0, encrypted, iv.length, cipherText.length);

encryptedBase64 =
Base64.encodeBase64String(encrypted);

}
catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | InvalidKeyException | BadPaddingException
| IllegalBlockSizeException | NoSuchPaddingException e) {
// Do nothing
}

return encryptedBase64;

}


Método para desencriptar una cadena:

/**
* Method in charge of decrypting a text, using the AES SecretKey.
*
* @param pTextEncrypted Define the text to decrypt
* @return a String with the decrypted text
*/
public static String decryptedText(String pTextEncrypted) {

String decryptedBase64 = null;

/*
* key is 16 zero bytes
*/
SecretKey secretKey = new SecretKeySpec(new byte[16], "AES");

try {

byte[] decoded = Base64.decodeBase64(pTextEncrypted);

byte[] iv = Arrays.copyOfRange(decoded, 0, GCM_IV_LENGTH);

Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
GCMParameterSpec ivSpec = new GCMParameterSpec(GCM_TAG_LENGTH * Byte.SIZE, iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);

byte[] cipherText = cipher.doFinal(decoded, GCM_IV_LENGTH, decoded.length - GCM_IV_LENGTH);

decryptedBase64 = new String(cipherText, StandardCharsets.UTF_8);

} catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | InvalidKeyException | BadPaddingException
| IllegalBlockSizeException | NoSuchPaddingException e) {
// Do nothing
}

return decryptedBase64;

}

Para invocar estos métodos, utilicen el siguiente código:

public static void main(String[] args) {
String miTextoAEncriptar = encryptedText("Esto es una prueba de lo que quiero encriptar.");
System.out.println("Texto encriptado: " + miTextoAEncriptar);

String miTextoADesEncriptar = decryptedText(miTextoAEncriptar);
System.out.println("Texto desencriptado: " + miTextoADesEncriptar);
}

0 comentarios :

Publicar un comentario