简体   繁体   English

我如何将生成的 AES 密钥从 Diffie-Hellman 类添加到我使用密钥的类中

[英]How can i add generated AES key from Diffie-Hellman class to the class where i use the key

Background: I have two devices which communicate via IP/port connection establishing live voice encrypting communication thanks to Diffie-Hellman key-exchange and encrypting it thanks to AES algorithm.背景:我有两个设备通过 IP/端口连接进行通信,通过 Diffie-Hellman 密钥交换建立实时语音加密通信,并通过 AES 算法对其进行加密。 Now some of the code is written and some just taken to use as an example of the prototype implementation.现在写了一些代码,一些只是用作原型实现的示例。

Problem: Now even when understanding how my classes work just like the title states: I can not figure out how to take the key from DH class and declare in AES class that this is the key it must use to encrypt.问题:现在即使理解我的类如何工作就像标题所述:我无法弄清楚如何从 DH 类中获取密钥并在 AES 类中声明这是它必须用于加密的密钥。

Ps Advice on code optimization, better practices and general tips are most welcome, please. Ps 最欢迎关于代码优化、更好实践和一般提示的建议。 Thank you for your time.感谢您的时间。

public class DH extends Thread {公共类 DH 扩展线程 {

int bitLength=512;  
int certainty=20;//

private static final SecureRandom rnd = new SecureRandom();

public DH() throws Exception{
    Random randomGenerator = new Random();
    BigInteger generatorValue,primeValue,publicA,publicB,secretA,secretB,sharedKeyA,sharedKeyB;

    primeValue = findPrime();// BigInteger.valueOf((long)g);
    System.out.println("the prime is "+primeValue);
     generatorValue = findPrimeRoot(primeValue);//BigInteger.valueOf((long)p);
    System.out.println("the generator of the prime is "+generatorValue);

    // on machine 1
    secretA = new BigInteger(bitLength-2,randomGenerator);
    // on machine 2
    secretB = new BigInteger(bitLength-2,randomGenerator);

    // to be published:
    publicA=generatorValue.modPow(secretA, primeValue);
    publicB=generatorValue.modPow(secretB, primeValue);
    sharedKeyA = publicB.modPow(secretA,primeValue);// should always be same as:
    sharedKeyB = publicA.modPow(secretB,primeValue);

    String getAValue=sharedKeyA.toString();

    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(getAValue.getBytes());

    byte byteData[] = md.digest();
    StringBuffer sb = new StringBuffer();

    for(int i=0;i<byteData.length;i++)
    {
        sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));// ??
    }

    String getHexValue = sb.toString();
    System.out.println("hex format in SHA-256 is "+getHexValue);

    byte [] initkey = getAValue.getBytes("UTF-8");

    MessageDigest sha = MessageDigest.getInstance("SHA-256");
    initkey =  sha.digest(initkey);
    initkey = Arrays.copyOf(initkey, 16);

    SecretKeySpec secretKeySpec =  new SecretKeySpec(initkey,"AES");

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);



}

As you can see i have coded AES key and IV statically but want for the generated AES key in DH to be assigned in this class如您所见,我已经静态编码了 AES 密钥和 IV,但希望在此类中分配 DH 中生成的 AES 密钥

public class AES {公共类 AES {

static String IV = "AAAAAAAAAAAAAAAA";
static String initkey = "13B_0(wcXNGkHAR[";

public static byte[] encrypt(byte[] plainData, int offset, int length) throws Exception {


    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");//CBC
    SecretKeySpec key = new SecretKeySpec(initkey.getBytes("UTF-8"), "AES");

    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
    return cipher.doFinal(plainData, offset, length);
}

public static byte[] decrypt(byte[] cipherSound, int offset, int length) throws Exception {

    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");//CBC
    SecretKeySpec key = new SecretKeySpec(initkey.getBytes("UTF-8"), "AES");

    cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
    return cipher.doFinal(cipherSound, offset, length);
}

} }

figure out how to take the key from DH class and declare in AES class that this is the key it must use to encrypt弄清楚如何从 DH 类中获取密钥并在 AES 类中声明这是它必须用来加密的密钥

Please check this tutorial请检查本教程

Then you can use the returned secret:然后你可以使用返回的秘密:

byte[] masterKey = aKeyAgree.generateSecret();
// maybe hash the master key too
SecretKeySpec key = new SecretKeySpec(masterKey, 0, 16, "AES");

However, if you have two way communication, you either need random IV or different keys for each direction (derived from the master)但是,如果您有双向通信,则每个方向都需要随机 IV 或不同的密钥(源自主)

Additional hint :附加提示:

I have two devices which communicate via IP/port connection establishing live voice encrypting communication我有两个设备通过 IP/端口连接建立实时语音加密通信

Maybe your devices are powerful enough to establish proper TLS, what would solve many things for you也许你的设备足够强大,可以建立正确的 TLS,什么可以为你解决很多问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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