Java 如何在AES128算法中生成64位加密密钥?

Java 如何在AES128算法中生成64位加密密钥?,java,algorithm,encryption,aes,aescryptoserviceprovider,Java,Algorithm,Encryption,Aes,Aescryptoserviceprovider,我不熟悉加密概念。我正在做一个小项目,我需要使用AES128算法加密内容。接收器需要64位加密密钥进行解密。 但是,我认为密钥是在AES128中基于字符串生成的。那么如何生成64位加密密钥进行解密 我的代码如下 public class AESencrp { private static final String ALGO = "AES"; private static byte[] keyValue = {}; public static String encryp

我不熟悉加密概念。我正在做一个小项目,我需要使用AES128算法加密内容。接收器需要64位加密密钥进行解密。
但是,我认为密钥是在AES128中基于字符串生成的。那么如何生成64位加密密钥进行解密

我的代码如下

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);
    }

}
结果如下所示

 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

我需要与接收者分享哪一个?有人能帮我吗?

AES是一种对称加密算法。因此,对于加密和解密,密钥是相同的。所以,您不需要为解密“生成”密钥,您需要“拥有”它

对于密钥传输,您可以使用
RSA算法
(或任何其他非对称密钥算法)。两侧需要有相同的关键组件。在您的例子中,有一个组件,它是String
(=“privateKey”)
。所以,您需要告诉transfer这个组件,我建议使用非对称加密

使用可以使用私钥解密的公钥加密“privateKey”。然后,使用此组件生成AES密钥


阅读有关RSA算法的更多信息AES是一种对称加密算法。因此,对于加密和解密,密钥是相同的。所以,您不需要为解密“生成”密钥,您需要“拥有”它

对于密钥传输,您可以使用
RSA算法
(或任何其他非对称密钥算法)。两侧需要有相同的关键组件。在您的例子中,有一个组件,它是String
(=“privateKey”)
。所以,您需要告诉transfer这个组件,我建议使用非对称加密

使用可以使用私钥解密的公钥加密“privateKey”。然后,使用此组件生成AES密钥

阅读有关RSA算法的更多信息

请将二进制数据编码为十六进制,以使其可读。请将二进制数据编码为十六进制,以使其可读。