First I want to mention that I have looked at all other questions related to this kind on encryption, so this is a unique question, not duplicate in any form.
I have been working on this problem for three days now, I finally got a piece of code that encrypts, but I doubt that this encryption is correct because the encrypted text is too short, that is what online decryptor tools are telling me any how, because I want decryption to be done using stand alone tool (by providing the other party a secret key), these are the tools I tried using, they all agree on one thing: The provided data/text is too small
.
The code I have is as simple as I tried to make it:
import javax.crypto.*;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.ByteBuffer;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Arrays;
import java.util.Base64;
public class DataEncrypt {
//static String plainText = "This is a plain text which need to be encrypted by Java AES 256 GCM Encryption Algorithm";
public static final int AES_KEY_SIZE = 256;
public static final int GCM_IV_LENGTH = 12;
public static final int GCM_TAG_LENGTH = 16;
public String encrypt(byte[] plaintext)
{
String res=null;
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(AES_KEY_SIZE);
// Generate Key
SecretKey key = keyGenerator.generateKey();
byte[] IV = new byte[GCM_IV_LENGTH];
SecureRandom random = new SecureRandom();
random.nextBytes(IV);
// Get Cipher Instance
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
// Create SecretKeySpec
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
// Create GCMParameterSpec
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, IV);
// Initialize Cipher for ENCRYPT_MODE
cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec);
// Perform Encryption
byte[] cipherText = cipher.doFinal(plaintext);
res= "cipheredText= " + cipherText.toString() + "\nkeySpec=" + keySpec.toString() + "\nKey=" +
Base64.getEncoder().encodeToString(key.getEncoded());
}catch (Exception e){
}
return res;
}
}
Where this line is for debugging:
res= "cipheredText= " + cipherText.toString() + "\nkeySpec=" + keySpec.toString() + "\nKey=" +
Base64.getEncoder().encodeToString(key.getEncoded())
This is how I am calling the function:
//string number entered here is not actually the same one I used, but it has the same length
System.out.println(dataEncrypt.encrypt("320900401024111".getBytes()));
This is the console output:
cipheredText= [B@2d612431
keySpec=javax.crypto.spec.SecretKeySpec@fffe956d
Key=3OF/L6XHck3imIRO9q8yi/Dr8+h6cMw99/21zMlByuo=
So I have a key (from the output) which i can use in an online tool to decrypt, and I have an encrypted text (cipheredText) to enter in the tool, but the cipheredText seems to be short, or generally the tools are just not able to give me back my original string (320900401024111).
Tools I have used for decryption:
- https://webkit.org/demos/webcrypto/aes-gcm.html
- https://encode-decode.com/aes-256-gcm-encrypt-online/
- https://8gwifi.org/CipherFunctions.jsp
- http://aes.online-domain-tools.com/
I am not an expert with encryption, but I am asking this question because I have exhausted most efforts, not for the lack of trying, I just need help with:
- Is there something wrong with this code?
- Is there somethign wrong with the result of this code?
- Why I am unable to decrypt using the tools mentioned?
Thank you for any effort to help.