也许这个片段可以回答你的问题:
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidParameterSpecException;
public class CipherDetails {
public static void main(String[] args) throws NoSuchPaddingException, NoSuchAlgorithmException, IOException, InvalidKeyException, InvalidParameterSpecException {
System.out.println("https://stackoverflow.com/questions/61456475/get-cipher-mode-and-padding-scheme-from-existing-cipher");
byte[] keyByte = "12345678901234567890123456789013".getBytes("UTF-8");
SecretKey secretKey = new SecretKeySpec(keyByte, "AES");
//Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
//Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
// key & algorithm details
System.out.println("SecretKey Algorithm: " + secretKey.getAlgorithm() + " keylength: " + secretKey.getEncoded().length);
System.out.println("SecretKey format: " + secretKey.getFormat());
System.out.println("Cipher Algorithm: " + cipher.getAlgorithm() + " Blocksize: " + cipher.getBlockSize());
System.out.println("Cipher Parameters: " + cipher.getParameters());
System.out.println("Cipher Provider: " + cipher.getProvider());
System.out.println("Cipher Provider getInfo: " + cipher.getProvider().getInfo());
try {
System.out.println("Cipher IV: " + bytesToHex(cipher.getIV()) + " IV length: " + cipher.getIV().length);
} catch (NullPointerException e) {
System.out.println("cipher IV: Algorithm does not use an IV");
}
try {
System.out.println("Cipher parameters encoded: " + bytesToHex(cipher.getParameters().getEncoded()));
} catch (NullPointerException e) {
System.out.println("cipher parameters encoded: not available");
}
}
private static String bytesToHex(byte[] bytes) {
StringBuffer result = new StringBuffer();
for (byte b : bytes) result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
return result.toString();
}
}
这是结果:
SecretKey Algorithm: AES keylength: 32
SecretKey format: RAW
Cipher Algorithm: AES/GCM/NoPadding Blocksize: 16
Cipher Parameters:
iv:
[0000: AE 93 E3 D9 72 83 E1 84 D5 FA C4 97 ....r.......
]
tLen(bits):
128
Cipher Provider: SunJCE version 11
Cipher Provider getInfo: SunJCE Provider (implements RSA, DES, Triple DES, AES, Blowfish, ARCFOUR, RC2, PBE, Diffie-Hellman, HMAC, ChaCha20)
Cipher IV: ae93e3d97283e184d5fac497 IV length: 12
Cipher parameters encoded: 3011040cae93e3d97283e184d5fac497020110