简体   繁体   中英

i am trying to print the secret key in aes algo. but the key size printed is not 128 bits

print secret key

KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
cipher = Cipher.getInstance("AES");
String plainText = "AES Symmetric Encryption Decryption";
System.out.println("Plain Text Before Encryption: " + plainText);

String encryptedText = encrypt(plainText, secretKey);
System.out.println("Encrypted Text After Encryption: " + encryptedText);

String decryptedText = decrypt(encryptedText, secretKey);
System.out.println("Decrypted Text After Decryption: " + decryptedText);
System.out.println("srecretkey"+secretKey);

The ouput i am getting is:

Plain Text Before Encryption: AES Symmetric Encryption Decryption
Encrypted Text After Encryption: l4YZj1SjqS/vr1mK9K4LC7lBFfQ4wkJg2zIlb81ghpkM5qnvCyyfMeYop2ppHDoX
Decrypted Text After Decryption: AES Symmetric Encryption Decryption
srecretkeyjavax.crypto.spec.SecretKeySpec@15300

The size of key is not 128 bits here.

There are 2 issues with your question and code.

First: you are asking about a 128 bit AES key but your parameter for keygenerator is "256" that means you generate a 256 bit (= 32 byte) long AES key:

keyGenerator.init(256);

Second: an AES key is random data in a byte array that is not suitable for printing to a string. When you need a printout of the key there are several ways to to - I show you a way with encoding the key to a (Base64) encoded string:

String aesKeyBase64 = Base64.getEncoder().encodeToString(aesKey);

This is the output of the sample program:

Generate a 256 bit = 32 byte long AES key
secretKey: javax.crypto.spec.SecretKeySpec@1518c
aesKey length: 32
aesKey:[B@6d9c638
aesKeyBase64: cWEo4+jv0SXBgbiZbdiouasFYuV3rUYKQ3403y4wU14=

Below is the full code, run the example with my online compiler: https://repl.it/@javacrypto/SoGenerateAesKey#Main.java

import java.security.*;
import javax.crypto.KeyGenerator;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import java.util.Base64;

public class Main {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        System.out.println("Generate a 256 bit = 32 byte long AES key");
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(256);
        SecretKey secretKey = keyGenerator.generateKey();
        System.out.println("secretKey: " + secretKey);
        // the key is a 32 bytes long byte array
        byte[] aesKey = secretKey.getEncoded();
        System.out.println("aesKey length: " + aesKey.length);
        System.out.println("aesKey:" + aesKey);
        // convert to a base64 encoded String
        String aesKeyBase64 = Base64.getEncoder().encodeToString(aesKey);
        System.out.println("aesKeyBase64: " + aesKeyBase64);
    }
}

Edit:

To get the secret key back from Base64 encoded string use this line of code:

SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode("base64string"), "AES");

and use it as input for your cipher.init:

cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM