- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
代码使用 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:
这个例子:
代码本身中有更多注释,用 ===
标记。
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/
我在理解这些函数如何更新底层引用、原子等时遇到问题。 文档说:(应用当前身份值参数) (def one (atom 0)) (swap! one inc) ;; => 1 所以我想知道它是如何“扩展到
尝试让一段代码看起来更好。 我在 Clojurescript 中有以下内容: (swap! app-state assoc-in [:lastresults] []) (swap! app-state
我在数据库中有带有排序顺序号的记录。现在我想创建一个带有向上和向下按钮的用户界面来重新排序它们。制作两个 functionsUp(record) 和 functionDown(record) 的最佳算
如何才能让第二次点击时返回?我想我必须以某种方式找到活跃的,但不确定。 $("#test").click(function(){ $("#dsa").fadeOut() $("#asd
我需要有关这次考试的帮助。我需要反转输入字符串。 int main(void) { char str[30]; int strlen; int i=0; int count=0;int
我正在用 C 语言玩指针...我尝试编写一个接收指向值的指针、检索指针的指针并交换指向值的指针的交换,而不是接收指向值的指针和交换值的常规交换。 这是代码... 互换功能: void swap(voi
如何在 javascript 中切换值?例如,如果 x = apple,则函数应返回 x = orange。如果 x = orange,则函数应返回 x = apple。不确定,这里有什么用,切换或交
刚接触这类东西,可能做错了什么,但是- 我有 3 个成员 std::unique_ptr currentWeapon; std::unique_ptr weaponSlotOne; std::uniq
我想在 Map 内的不可变列表内交换项目,示例: const Map = Immutable.fromJS({ name:'lolo', ids:[3,4,5] }); 我正在尝试使用
我创建了动态数组。如果具有某些值,则填充。打印它。但是交换/交换指针后(任务是在特定条件下交换行) 条件取决于sumL。为了不浪费您的时间,我没有描述细节。 问题在于交换指针。 for ( k = 0
要反转整个 vector,存在 std::reverse。但我想将一个 vector “划分”为两部分(恰好在中间)并将两者反转,将它们放回一起并再次反转整个 vector 。例如我们有: 0 1 2
我正在致力于代码最小化和增强。我的问题是:是否可以在不破坏代码逻辑的情况下交换上面的 if 语句? int c1 = Integer.parseInt(args[0]) ; int c
我读过释放 vector 内存的最佳方法是: vector().swap(my_vector); 而且我真的不明白发生了什么。交换函数需要 2 个 vector 并交换它们的元素,例如: vector
我正在尝试编写一个 Haskell 函数,该函数接受一串字母对,并在所有字母组成的字符串中交换该对字母,但我想出的方法感觉很尴尬且不惯用。 我有 swap a b = map (\x-> if x =
我正在尝试使用向上和向下箭头交换两个元素。 JSFiddle 解决方案会很棒! 我的 HTML: Some text down Some ot
当将 subview 与另一个太阳 View 交换时,是否需要重新应用约束?是否需要删除适用于已删除 View 的约束? 或者它们应该自动持续存在? 最佳答案 约束是 View 的“一部分”。当您删除
所以我制作网站已经有一段时间了,但只是真正用于显示和信息的东西。我想尝试一下 AngularJs,所以我遵循了 Codeschool 上的指南。当我根据在线文档意识到我使用的语法不被推荐时,我在该应用
我正在尝试编写一个函数,可以将字符串中的 unicode 字符替换为非 unicode ASCII 字符,问题是上传包含它们的字符串时,unicode 连字符和引号不会被读取。 我希望该函数有一个带有
我目前正在使用 Azure 网站来部署我的应用程序。我目前正在使用两个网站,每个网站监听我的 GIT 的不同分支。如图所示here . 现在,为了让它变得完美,我只是缺少一种在这两个实例之间快速切换的
在我的 javascript 中,有两个包含一些值的 div。 我想交换这些div中的值。 有什么解决办法吗? 最佳答案 var temp = $('#div1').html(); $('#div1'
我是一名优秀的程序员,十分优秀!