Aes Algo - Decryption Issue

AES algo - Decryption Issue

http://android-developers.blogspot.in/2013/02/using-cryptography-to-store-credentials.html

http://developer.android.com/reference/javax/crypto/SecretKeyFactory.html

Check the above links. Use the below for reference.Modify the below according to your needs.

Usage

 try {
DescEncrypter ec = new DescEncrypter();
byte[] cipherText =ec.encrypt("hi", "hello");
String enc = new String(cipherText,"UTF-8");
String decryp= ec.decrypt("hi", cipherText);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

DescEncrypter.java

    import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class DescEncrypter {

public static final int SALT_LENGTH = 20;
public static final int PBE_ITERATION_COUNT = 200; //1024;

private static final String PBE_ALGORITHM = "PBEWithSHA256And256BitAES-CBC-BC";

//algoritmo / modo / relleno
private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";

byte[] iv = "1234567890asdfgh".getBytes();

byte[] salt = "dfghjklpoiuytgftgyhj".getBytes();

public byte[] encrypt(String password, String cleartext) {

byte[] encryptedText = null;

try {

PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, PBE_ITERATION_COUNT, 256);

//Factoria para crear la SecretKey, debemos indicar el Algoritmo
SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_ALGORITHM);

SecretKey tmp = factory.generateSecret(pbeKeySpec);

//Creamos una llave;
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

//Obtenemos la llave, solo informativo
byte[] key = secret.getEncoded();

//La clase Cipher, se usa para cifrar mediante algoritmos de clave simétrica
Cipher encryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);

//byte[] iv = generateIv();

IvParameterSpec ivspec = new IvParameterSpec(iv);

//Accion, SecretKey, parameter specification for an initialization vector
encryptionCipher.init(Cipher.ENCRYPT_MODE, secret, ivspec);

//Realizamos el cifrado
encryptedText = encryptionCipher.doFinal(cleartext.getBytes());

} catch (Exception e) {
e.printStackTrace();
}

return encryptedText;
}

public String decrypt(String password, byte[] encryptedText) {

String cleartext = "";

try {

PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, PBE_ITERATION_COUNT, 256);

//Factoria para crear la SecretKey, debemos indicar el Algoritmo
SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_ALGORITHM);

SecretKey tmp = factory.generateSecret(pbeKeySpec);

//Creamos una llave;
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

//Obtenemos la llave, solo informativo
byte[] key = secret.getEncoded();

//La clase Cipher, se usa para cifrar mediante algoritmos de clave simétrica
Cipher decryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM);

//byte[] iv = generateIv();

IvParameterSpec ivspec = new IvParameterSpec(iv);

//Accion, SecretKey, parameter specification for an initialization vector
decryptionCipher.init(Cipher.DECRYPT_MODE, secret, ivspec);

//Realizamos el descifrado
byte[] decryptedText = decryptionCipher.doFinal(encryptedText);

cleartext = new String(decryptedText);

} catch (Exception e) {
e.printStackTrace();
}

return cleartext;
}
}

Aes decryption in java - problems with padding

Just use the CipherOutputStream. Do not invoke cipher.doFinal(buffer) and don't forget to close the output stream.

FileInputStream inputStream = new FileInputStream(inputFile);
FileOutputStream fileout = new FileOutputStream(outputFile);
CipherOutputStream out = new CipherOutputStream(fileout , cipher);

try {
byte[] buffer = new byte[8192];
int count;

while ((count = inputStream.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
} finally {
out.close();
inputStream.close();
}

The CipherOutputStream manages the cipher for you. It invokes doFinal when the stream will be closed and flushes the internal buffer.

Java AES decryption error, encryption is working fine

Arrays.toString(byte[] a) "Returns a string representation of the contents of the specified array." It does not convert a byte array to a String. Instead try using:

new String(decryptAES(text1), "UTF-8");

Problems with AES encryption (encrypted string is not what it should be - Java & .NET)

Thanks to Topaco (first comment in the question), I was able to sort it out.

Here is Topaco's original answer:

The Java code uses PBKDF2 with HMAC/SHA1, the C# code an algorithm based on PBKDF1. For PBKDF2 in the C# code PasswordDeriveBytes has to be replaced by Rfc2898DeriveBytes (with HMAC/SHA1 as default). Note that the .NET implementation expects a minimum 8 bytes salt. Also, Java uses a 32 bytes key, the C# code a 16 bytes key. With consistent key derivation and key size, the generated ciphertexts are identical. –
Topaco

The only thing I had to change was to upgrade from .NET 5 to .NET 6.



Related Topics



Leave a reply



Submit