key_manager.java
来自「这是一个文本加密、解密系统」· Java 代码 · 共 215 行
JAVA
215 行
package IS_Project1;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.ArrayList;
import javax.crypto.KeyGenerator;
public class Key_Manager {
private ArrayList<PrivateKey> boy_privateKey_list, girl_privateKey_list;
private ArrayList<PublicKey> boy_publicKey_list, girl_publicKey_list;
private ArrayList<Key> symmetricKey_DES_list, symmetricKey_AES_list;
private ArrayList<My_Cipher> boy_cipher_list, girl_cipher_list;
public Key_Manager()
{
this.boy_publicKey_list = new ArrayList<PublicKey>();
this.girl_publicKey_list = new ArrayList<PublicKey>();
this.boy_privateKey_list = new ArrayList<PrivateKey>();
this.girl_privateKey_list = new ArrayList<PrivateKey>();
this.symmetricKey_DES_list = new ArrayList<Key>();
this.symmetricKey_AES_list = new ArrayList<Key>();
this.boy_cipher_list = new ArrayList<My_Cipher>();
this.girl_cipher_list = new ArrayList<My_Cipher>();
}
public void register_boycipher_entry(My_Cipher this_cipher)
{
this.boy_cipher_list.add(this_cipher);
}
public void register_girlcipher_entry(My_Cipher this_cipher)
{
this.girl_cipher_list.add(this_cipher);
}
public void generate_boy_key_pair(String key_algorithm, int key_len, My_Cipher cipher)
{
KeyPairGenerator keyGen;
PublicKey pub_key;
PrivateKey priv_key;
try
{
keyGen = KeyPairGenerator.getInstance(key_algorithm);
keyGen.initialize(key_len);
KeyPair key_pair = keyGen.generateKeyPair();
pub_key = key_pair.getPublic();
priv_key = key_pair.getPrivate();
int index = this.boy_cipher_list.indexOf(cipher);
this.boy_publicKey_list.add(index, pub_key);
this.boy_privateKey_list.add(index, priv_key);
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
}
public void generate_girl_key_pair(String key_algorithm, int key_len, My_Cipher cipher)
{
KeyPairGenerator keyGen;
PublicKey pub_key;
PrivateKey priv_key;
try
{
keyGen = KeyPairGenerator.getInstance(key_algorithm);
keyGen.initialize(key_len);
KeyPair key_pair = keyGen.generateKeyPair();
pub_key = key_pair.getPublic();
priv_key = key_pair.getPrivate();
int index = this.girl_cipher_list.indexOf(cipher);
this.girl_publicKey_list.add(index, pub_key);
this.girl_privateKey_list.add(index, priv_key);
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
}
public void generate_symmetric_DES_key(String key_algorithm, int key_len, My_Cipher cipher)
{
KeyGenerator keyGen;
Key sym_key;
try
{
keyGen = KeyGenerator.getInstance(key_algorithm);
keyGen.init(key_len);
sym_key = keyGen.generateKey();
int index_boy = this.boy_cipher_list.indexOf(cipher);
int index_girl = this.girl_cipher_list.indexOf(cipher);
int index = (index_boy != -1) ? index_boy : index_girl;
this.symmetricKey_DES_list.add(index, sym_key);
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
}
public void generate_symmetric_AES_key(String key_algorithm, int key_len, My_Cipher cipher)
{
KeyGenerator keyGen;
Key sym_key;
try
{
keyGen = KeyGenerator.getInstance(key_algorithm);
keyGen.init(key_len);
sym_key = keyGen.generateKey();
int index_boy = this.boy_cipher_list.indexOf(cipher);
int index_girl = this.girl_cipher_list.indexOf(cipher);
int index = (index_boy != -1) ? index_boy : index_girl;
this.symmetricKey_AES_list.add(index, sym_key);
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
}
public PublicKey get_boy_public_key(My_Cipher cipher)
{
int index;
index = (this.girl_cipher_list.indexOf(cipher) != -1) ? this.girl_cipher_list.indexOf(cipher) :
this.boy_cipher_list.indexOf(cipher);
return (PublicKey)this.boy_publicKey_list.get(index);
}
public PublicKey get_girl_public_key(My_Cipher cipher)
{
int index;
index = (this.boy_cipher_list.indexOf(cipher) != -1) ? this.boy_cipher_list.indexOf(cipher) :
this.girl_cipher_list.indexOf(cipher);
return (PublicKey)this.girl_publicKey_list.get(index);
}
protected PrivateKey get_boy_private_key(My_Cipher boy_cipher)
{
int index;
index = this.boy_cipher_list.indexOf(boy_cipher);
if (index == -1)
{
System.out.println("No such cipher!Can not publish boy's private key.");
return null;
}
return (PrivateKey)this.boy_privateKey_list.get(index);
}
protected PrivateKey get_girl_private_key(My_Cipher girl_cipher)
{
int index;
index = this.girl_cipher_list.indexOf(girl_cipher);
if (index == -1)
{
System.out.println("No such cipher!Can not publish girl's private key.");
return null;
}
return (PrivateKey)this.girl_privateKey_list.get(index);
}
protected Key get_symmetric_DES_key(My_Cipher cipher)
{
int index_boy, index_girl;
index_boy = this.boy_cipher_list.indexOf(cipher);
index_girl = this.girl_cipher_list.indexOf(cipher);
if (index_boy == -1 && index_girl == -1)
{
System.out.println("No such cipher!Can not publish the key.");
return null;
}
int index = (index_boy != -1) ? index_boy : index_girl;
return this.symmetricKey_DES_list.get(index);
}
protected Key get_symmetric_AES_key(My_Cipher cipher)
{
int index_boy, index_girl;
index_boy = this.boy_cipher_list.indexOf(cipher);
index_girl = this.girl_cipher_list.indexOf(cipher);
if (index_boy == -1 && index_girl == -1)
{
System.out.println("No such cipher!Can not publish the key.");
return null;
}
int index = (index_boy != -1) ? index_boy : index_girl;
return this.symmetricKey_AES_list.get(index);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?