Search code examples
javaencryptioncryptographydiffie-hellman

Diffie Hellman to AES


I am trying to create an application that can send data from a client to a server using encrypted data. I have set up a basic Diffie Hellman key exchange in Java and I was just wondering how I would convert that to a AES-128 key. Here are the numbers that my key exchange is producing: p: 45739334052801211486383982115714395642859523615469431203095063293408205085850655737540408983027523682219373484935815737826696483347058644040214365601698451568563654746844010747338983871136644386557631185640042912749789107007323073169713214532843785763128771045651074137977867781277726926260077346003 g: 405 produced key: 36248441610316046373940723150439573936645456312032028064365240345591632292456385336418395841257359594423512887912569551847749513145040862559588574541468845827085159018247962009639778000068069176753753861222140233559881791396088762185913485540314156993368037042615619321223426679365899278177180176354 Both p and the produced key are 299 characters long. I have tried to figure this out myself and read about hashing the produced key with SHA-256, this is the result: SHA-256: 4631d862806f2c22ae28959a8ffb441789ab306468d6bf04d87435dd81890519 I can not however see how I can turn any of these into a 16 byte (128 bit) key. An example of which is {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}. Do I take the first 16 numbers of my result?

Any help or information would be amazing and I would be very thankful. If any more information is required please just ask me.


Solution

    1. (Optional) Use 128-bit values for a and b secret parameters of key exchange.
    2. Convert the resulting value s into byte array and take any 16 elements. You don't need to dump s into hex string, or to hash that string.