aescryptography.java

来自「一个简单的实现Kerberos验证的程序」· Java 代码 · 共 59 行

JAVA
59
字号
/**
 * AES加密解密及相关方法
 */
package src;

import java.security.*;
import javax.crypto.*;

/**
 * RSACryptography
 * RSACryptography use the privated key to encrypt the plain text and decrypt
 * the cipher text with the public key
 */
public class AESCryptography{
	private Cipher cipher;
	/**
	 * Default Constructure
	 * initialize the cipher
	 */
	public AESCryptography() {
		try {
			cipher = Cipher.getInstance("AES");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param byteInput input byte is the bytes want to be ciphered and deciphered it is up
	 * to the param crypto
	 * @param key used to cipher or decipher the byte
	 * @param crytpo if true, the function is used to cipher the bytes otherwise the bytes will
	 * be deciphered
	 * @return the bytes after ciphered or deciphered
	 */
	public byte[] encrypt_decrypt(byte[] byteInput, Key key, boolean crypto){
		try {			
			if(crypto){
				cipher.init(Cipher.ENCRYPT_MODE,key);
			}else{
				cipher.init(Cipher.DECRYPT_MODE,key);
			}
			byte[] cipherByte = cipher.doFinal(byteInput);
			return cipherByte;
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;	
	}
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?