public class text {
static final byte[] bytes_a = new byte[]{66, 108, 111, 119, 102, 105, 115, 104};
static final byte[] bytes_key = new byte[]{104,101,108,108,111,50,48,49,55};
static final byte[] bytes_content = new byte[]{103,111,111,100,32,106,111,98,32,77,121,32,102,114,105,101,110,100};
public static void main(String[] args) {
SecretKeySpec se_key = new SecretKeySpec(bytes_key,new String(bytes_a));
byte[] target = encrypt(se_key,bytes_content);
System.out.println(new String(target));
byte[] bytes = decrypt(se_key,target);
System.out.println(new String(bytes));
}
public static byte[] encrypt(Key key, byte[] text){
try {
Cipher cipher = Cipher.getInstance(new String(bytes_a));
cipher.init(Cipher.ENCRYPT_MODE,key);
return cipher.doFinal(text);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
public static byte[] decrypt(Key key, byte[] text){
try {
Cipher cipher = Cipher.getInstance(new String(bytes_a));
cipher.init(Cipher.DECRYPT_MODE,key);
return cipher.doFinal(text);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
}
加密算法
最新推荐文章于 2021-03-09 14:14:34 发布
