简体   繁体   中英

How to generate 64 Bit encrypted key in AES128 algorithm?

I am new to encryption concepts. I am working on a small project, I need to encrypt the content using the AES128 algorithm. The receiver needs a 64 bit encrypted key for decrypting.
But, I think the key is generated in AES128 based on a String. Then how to generate a 64 bit enckey for decryption?

My code is like below

public class AESencrp {

    private static final String ALGO = "AES";
    private static byte[] keyValue = {};

    public static String encrypt(String Data, Key key) throws Exception {
        System.out.println("Key Encryp ============="
                + new String(key.getEncoded()));
        System.out.println("\n\n key in Enc ==========>" + key.getEncoded());
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
        byte encryptedValue[] = new Base64().encode(encVal);
        return new String(encryptedValue);
    }

    public static String decrypt(String encryptedData, Key key)
            throws Exception {
        System.out.println("Key Decryp ============="
                + new String(key.getEncoded()));
        System.out.println("\n\n key in Dec ==========>" + key.getEncoded());
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new Base64().decode(encryptedData.getBytes());
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }

    public static void main(String[] args) throws Exception {

        try {
            String sPrivateKey = "privateKey";
            keyValue = sPrivateKey.getBytes();
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            keyValue = sha.digest(keyValue);
            keyValue = Arrays.copyOf(keyValue, 16); // use only first 128 bit
        } catch (Exception e) {
            e.printStackTrace();
        }

        Key key = new SecretKeySpec(keyValue, ALGO);
        System.out.println("\n\n key in GK ==========>" + key.getEncoded());
        System.out.println("\n\n key in GK ==========>"
                + new String(key.getEncoded()));
        String password = "mypassword";
        String passwordEnc = encrypt(password, key);
        String passwordDec = decrypt(passwordEnc, key);

        System.out.println("Plain Text : " + password);
        System.out.println("Encrypted Text : " + passwordEnc);
        System.out.println("Decrypted Text : " + passwordDec);
    }

}

and the result is like below,

 key in GK ==========>[B@a90653
 key in Enc ==========>[B@de6ced
 key in Dec ==========>[B@e80a59

 Plain Text : mypassword
 Encrypted Text : mBhBCUOlJbOBEaBl/42TZQ==
 Decrypted Text : mypassword

Which one do I need to share with the receiver? Can anyone help me?

AES is a symmetric encryption algorithm. So, for encryption and decryption, keys are same. So, you don't "generate" key for decryption, you need to "have" it.

For key transfer, you can use RSA algorithm (or any of the other asymmetric key algorithms). You need to have same key components on both sides. In your case, there is one component, which is String (="privateKey") . So, you need to tell transfer this component, for which I recommend asymmetric encryption.

Encrypt "privateKey" with the public key which can be deciphered using the private key. Then, use this component to generate the AES key.

Read more about RSA algorithm here

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