📄 lover_cipher.java
字号:
package IS_Project1;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class Lover_cipher implements My_Cipher {
private Cipher cipher;
private byte encryp_byte[], decryp_byte[];
//the following two array lists are used to store output bytes among the RSA encryption/decryption
private ArrayList<Byte> RSA_encryp_list, RSA_decryp_list;
//constructor
public Lover_cipher()
{
this.RSA_encryp_list = new ArrayList<Byte>();
this.RSA_decryp_list = new ArrayList<Byte>();
}
//generate the cipher according to the cipher_name
public void generate_cipher(String cipher_name)
{
try
{
this.cipher = Cipher.getInstance(cipher_name);
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch (NoSuchPaddingException e)
{
e.printStackTrace();
}
}
//encrypt the message in the unit of byte using the key
public void encryp_message(byte[] cipher_bytes, Key key)
{
try
{
this.cipher.init(Cipher.ENCRYPT_MODE, key);
this.encryp_byte = cipher.doFinal(cipher_bytes);
}
catch (InvalidKeyException e) {
e.printStackTrace();
}
catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
catch (BadPaddingException e) {
e.printStackTrace();
}
}
//decrypt the message in the unit of byte using the key
public void decryp_message(byte[] cipher_bytes, Key key)
{
try
{
this.cipher.init(Cipher.DECRYPT_MODE, key);
this.decryp_byte = this.cipher.doFinal(cipher_bytes);
}
catch (InvalidKeyException e) {
e.printStackTrace();
}
catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
catch (BadPaddingException e) {
e.printStackTrace();
}
}
//encrypt the message under the RSA asymmetric cipher
public void encryp_RSA(byte[] cipher_bytes, Key key)
{
try
{
this.cipher.init(Cipher.ENCRYPT_MODE, key);
if (cipher_bytes.length > 117)
{
for (int i = 0; i < cipher_bytes.length/117; i++)
{
byte[] temp_plain_bytes = new byte[117];
for (int j = 0; j < 117; j++)
{
temp_plain_bytes[j] = cipher_bytes[i*117 + j];
}
byte[] the_encryp_byte = this.cipher.doFinal(temp_plain_bytes);
for (int k = 0; k < the_encryp_byte.length; k++)
{
this.RSA_encryp_list.add(new Byte(the_encryp_byte[k]));
}
}
//process the remaining bytes
for (int i = (cipher_bytes.length/117)*117; i < cipher_bytes.length; i+=117)
{
byte[] temp_plain_bytes = new byte[cipher_bytes.length - (cipher_bytes.length/117)*117];
for (int j = 0; j < cipher_bytes.length - (cipher_bytes.length/117)*117; j++)
{
temp_plain_bytes[j] = cipher_bytes[i + j];
}
byte[] the_encryp_byte = this.cipher.doFinal(temp_plain_bytes);
for (int k = 0; k < the_encryp_byte.length; k++)
{
this.RSA_encryp_list.add(new Byte(the_encryp_byte[k]));
}
}
}
else if (cipher_bytes.length <= 117)
{
byte []the_encryp_byte = this.cipher.doFinal(cipher_bytes);
for (int i = 0; i < the_encryp_byte.length; i++)
{
this.RSA_encryp_list.add(new Byte(the_encryp_byte[i]));
}
}
}
catch (InvalidKeyException e) {
e.printStackTrace();
}
catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
catch (BadPaddingException e) {
e.printStackTrace();
}
}
//decrypt the message under the RSA asymmetric cipher
public void decryp_RSA(byte[] cipher_bytes, Key key)
{
try
{
this.cipher.init(Cipher.DECRYPT_MODE, key);
for (int k = 0; k < cipher_bytes.length/128; k++)
{
byte[] temp_cipher_bytes = new byte[128];
for (int i = 0; i < 128; i++)
{
temp_cipher_bytes[i] = cipher_bytes[128*k + i];
}
byte []decryp_byte = this.cipher.doFinal(temp_cipher_bytes);
for (int j = 0; j < decryp_byte.length; j++)
{
this.RSA_decryp_list.add(new Byte(decryp_byte[j]));
}
}
}
catch (InvalidKeyException e) {
e.printStackTrace();
}
catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
catch (BadPaddingException e) {
e.printStackTrace();
}
}
//get the cipher text after encryption
public byte[] get_encrypText()
{
return this.encryp_byte;
}
//get the plain text after decryption
public byte[] get_decrypText()
{
return this.decryp_byte;
}
//get the RSA plain text
public byte[] get_RSA_decrypText()
{
Iterator iter = this.RSA_decryp_list.iterator();
byte RSA_decryp_bytes[] = new byte[this.RSA_decryp_list.size()];
int i = 0;
while(iter.hasNext())
{
RSA_decryp_bytes[i] = ((Byte)iter.next()).byteValue();
i++;
}
return RSA_decryp_bytes;
}
//get the RSA cipher text
public byte[] get_RSA_encrypText()
{
Iterator iter = this.RSA_encryp_list.iterator();
byte RSA_encryp_bytes[] = new byte[this.RSA_encryp_list.size()];
int i = 0;
while(iter.hasNext())
{
RSA_encryp_bytes[i] = ((Byte)iter.next()).byteValue();
i++;
}
return RSA_encryp_bytes;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -