📄 mac_center.java
字号:
package IS_Project1;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
public class MAC_Center {
private Mac mac;
private ArrayList<Key> MAC_key_list;
private ArrayList<My_Cipher> boy_cipher_list, girl_cipher_list;
private byte[] mac_bytes;
//constructor
public MAC_Center()
{
this.MAC_key_list = new ArrayList<Key>();
this.boy_cipher_list = new ArrayList<My_Cipher>();
this.girl_cipher_list = new ArrayList<My_Cipher>();
}
//register the boy cipher in the array list
public void register_boy_cipher(My_Cipher boy_cipher)
{
this.boy_cipher_list.add(boy_cipher);
}
//register the girl cipher in the array list
public void register_girl_cipher(My_Cipher girl_cipher)
{
this.girl_cipher_list.add(girl_cipher);
}
//generate the key used to initialize the MAC
public void generate_MAC_key(String key_algorithm, int key_len, My_Cipher cipher)
{
KeyGenerator keyGen;
Key MAC_key;
try
{
keyGen = KeyGenerator.getInstance(key_algorithm);
keyGen.init(key_len);
MAC_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.MAC_key_list.add(index, MAC_key);
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
}
//generate the corrsponding MAC, using the HmacMD5 algorithm
public byte[] generate_MAC(byte[] input_bytes, My_Cipher whose_cipher)
{
try
{
this.mac = Mac.getInstance("HmacMD5");
int index_boy = this.boy_cipher_list.indexOf(whose_cipher);
int index_girl = this.girl_cipher_list.indexOf(whose_cipher);
int index = (index_boy != -1) ? index_boy : index_girl;
Key key = this.MAC_key_list.get(index);
this.mac.init(key);
this.mac_bytes = this.mac.doFinal(input_bytes);
return this.mac_bytes;
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (InvalidKeyException e) {
e.printStackTrace();
}
return null;
}
//verify the MAC according to the input bytes
public boolean verify_MAC(byte[] input_bytes, My_Cipher whose_cipher)
{
try
{
this.mac = Mac.getInstance("HmacMD5");
int index_boy = this.boy_cipher_list.indexOf(whose_cipher);
int index_girl = this.girl_cipher_list.indexOf(whose_cipher);
int index = (index_boy != -1) ? index_boy : index_girl;
Key key = this.MAC_key_list.get(index);
this.mac.init(key);
if (input_bytes.length < 16)
{
return false;
}
byte[] message_to_verify = new byte[input_bytes.length - 16];
byte[] message_mac_bytes = new byte[16];
for (int i = 0; i < message_to_verify.length; i++)
{
message_to_verify[i] = input_bytes[i];
}
for (int i = 0; i < 16; i++)
{
message_mac_bytes[i] = input_bytes[input_bytes.length - 16 + i];
}
byte[] verify_mac_bytes = this.mac.doFinal(message_to_verify);
for (int i = 0; i < 16; i++)
{
if (message_mac_bytes[i] != verify_mac_bytes[i])
{
return false;
}
}
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return false;
}
catch (InvalidKeyException e) {
e.printStackTrace();
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -