gpt4 book ai didi

java - Java 中的 DiffieHellman key 交换为 AES 或 DESede

转载 作者:行者123 更新时间:2023-12-01 12:18:51 31 4
gpt4 key购买 nike

代码使用 DiffieHellman 和 DES 加密。DES 不安全,我想使用 DESede 或 AES。

SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");

SecretKeyFactory skf = SecretKeyFactory.getInstance("AES");

都失败了

SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");

但是可以工作(但是如所指出的那样太弱了)这是我的模块代码(下面的测试类)当前正在使用已实现的“DES”

import java.security.AlgorithmParameterGenerator;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidParameterSpecException;

import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DHParameterSpec;

/**
* Diffie-Hellman module for demonstrating KeyAgreement Algorithm
*/
public class DiffieHellmanModule {
private static KeyPairGenerator kpg;
static {
try {
// Create the parameter generator for a 512-bit DH key pair
AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator.getInstance("DH");
paramGen.init(512);
// Generate the parameters Prime p and root g
AlgorithmParameters params = paramGen.generateParameters();
// Use the params p and g to create a DiffieHellmanSpec
DHParameterSpec dhSpec
= (DHParameterSpec)params.getParameterSpec(DHParameterSpec.class);
//Generates and inits a KeyPairGenerator
kpg = KeyPairGenerator.getInstance("DH");
kpg.initialize(dhSpec);
} catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (InvalidParameterSpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* Create KeyAgreement and generate secret key
*
* @param prk_self
* the private key from the user who wants to generate the secret
* key
* @param pbk_peer
* the public key from the user whom is to be agree on the secret
* key with
* @param lastPhase
* flag which indicates whether or not this is the last phase of
* this key agreement
* @return the secret key
*/
public static SecretKey agreeSecretKey(PrivateKey prk_self,
PublicKey pbk_peer, boolean lastPhase) throws Exception {
// instantiates and inits a KeyAgreement
KeyAgreement ka = KeyAgreement.getInstance("DH");
ka.init(prk_self);
// Computes the KeyAgreement
ka.doPhase(pbk_peer, lastPhase);
// Generates the shared secret
byte[] bkey = ka.generateSecret();
// Generates a DES key
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
DESKeySpec desSpec = new DESKeySpec(bkey);
SecretKey key = skf.generateSecret(desSpec);
return key;
//return new SecretKeySpec(bkey, "AES");
}

/**
* Generate a key pair of algorithm "DiffieHellman"
*
* @return the public and private key pair
*/
public static KeyPair genDHKeyPair() {
return kpg.genKeyPair();
}
}

测试类

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;

public class DHTest {

public static void main(String[] args) {
//Generates keyPairs for Alice and Bob
KeyPair kp1 = DiffieHellmanModule.genDHKeyPair();
KeyPair kp2 = DiffieHellmanModule.genDHKeyPair();
//Gets the public key of Alice(g^X mod p) and Bob (g^Y mod p)
PublicKey pbk1 = kp1.getPublic();
PublicKey pbk2 = kp2.getPublic();
//Gets the private key of Alice X and Bob Y
PrivateKey prk1 = kp1.getPrivate();
PrivateKey prk2 = kp2.getPrivate();
try {
//Computes secret keys for Alice (g^Y mod p)^X mod p == Bob (g^X mod p)^Y mod p
SecretKey key1 = DiffieHellmanModule.agreeSecretKey(prk1, pbk2, true);
SecretKey key2 = DiffieHellmanModule.agreeSecretKey(prk2, pbk1, true);
//Instantiate the Cipher of algorithm "DES"
Cipher c = Cipher.getInstance("DES/ECB/PKCS5Padding");
//Init the cipher with Alice's key1
c.init(Cipher.ENCRYPT_MODE, key1);
//Compute the cipher text = E(key,plainText)
byte[] ciphertext = c.doFinal("Stand and unfold yourself".getBytes());
//prints ciphertext
System.out.println("Encrypted: " + new String(ciphertext,"utf-8"));
//inits the encryptionMode
c.init(Cipher.DECRYPT_MODE, key2);
//Decrypts and print
System.out.println("Decrypted: " + new String(c.doFinal(ciphertext), "utf-8"));
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
}
}

}

最佳答案

好的,因为您花时间创建了 MCVE:

这个例子:

  • 删除了一些类和注释,只是为了使其更加紧凑
  • 使用预定义的参数,通常不应生成参数
  • 使用 SHA-256 最左边的字节作为穷人的 key 派生函数
  • 删除 SecretKeyFactory,因为 AES 不需要它

代码本身中有更多注释,用 === 标记。

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
* Diffie-Hellman module for demonstrating KeyAgreement Algorithm
*/
public class DiffieHellmanModule {

private static final int AES_KEY_SIZE = 128;

public static void main(String[] args) {
// Generates keyPairs for Alice and Bob
KeyPair kp1 = DiffieHellmanModule.genDHKeyPair();
KeyPair kp2 = DiffieHellmanModule.genDHKeyPair();
// Gets the public key of Alice(g^X mod p) and Bob (g^Y mod p)
PublicKey pbk1 = kp1.getPublic();
PublicKey pbk2 = kp2.getPublic();
// Gets the private key of Alice X and Bob Y
PrivateKey prk1 = kp1.getPrivate();
PrivateKey prk2 = kp2.getPrivate();
try {
// Computes secret keys for Alice (g^Y mod p)^X mod p == Bob (g^X
// mod p)^Y mod p
SecretKey key1 = DiffieHellmanModule.agreeSecretKey(prk1, pbk2,
true);
SecretKey key2 = DiffieHellmanModule.agreeSecretKey(prk2, pbk1,
true);
// Instantiate the Cipher of algorithm "DES"
Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
// Init the cipher with Alice's key1
c.init(Cipher.ENCRYPT_MODE, key1);
// Compute the cipher text = E(key,plainText)
byte[] ciphertext = c.doFinal("Stand and unfold yourself"
.getBytes());
// prints ciphertext
System.out.println("Encrypted: " + new String(ciphertext, "utf-8"));
// inits the encryptionMode
c.init(Cipher.DECRYPT_MODE, key2);
// Decrypts and print
System.out.println("Decrypted: "
+ new String(c.doFinal(ciphertext), "utf-8"));
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
}
}

private static KeyPairGenerator kpg;

static {
try {
// === Generates and inits a KeyPairGenerator ===

// changed this to use default parameters, generating your
// own takes a lot of time and should be avoided
// use ECDH or a newer Java (8) to support key generation with
// higher strength
kpg = KeyPairGenerator.getInstance("DH");
kpg.initialize(1024);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}

public static SecretKey agreeSecretKey(PrivateKey prk_self,
PublicKey pbk_peer, boolean lastPhase) throws Exception {
// instantiates and inits a KeyAgreement
KeyAgreement ka = KeyAgreement.getInstance("DH");
ka.init(prk_self);
// Computes the KeyAgreement
ka.doPhase(pbk_peer, lastPhase);
// Generates the shared secret
byte[] secret = ka.generateSecret();

// === Generates an AES key ===

// you should really use a Key Derivation Function instead, but this is
// rather safe

MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] bkey = Arrays.copyOf(
sha256.digest(secret), AES_KEY_SIZE / Byte.SIZE);

SecretKey desSpec = new SecretKeySpec(bkey, "AES");
return desSpec;
}

public static KeyPair genDHKeyPair() {
return kpg.genKeyPair();
}
}

关于java - Java 中的 DiffieHellman key 交换为 AES 或 DESede,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26828649/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com