| | |
| | | * @param context |
| | | * @return |
| | | */ |
| | | public static String encrypt(String context) { |
| | | try { |
| | | byte[] decode = context.getBytes(CHARSET); |
| | | byte[] bytes = createKeyAndIv(decode, Cipher.ENCRYPT_MODE); |
| | | return Base64.getEncoder().encodeToString(bytes); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | // public static String encrypt(String context) { |
| | | // try { |
| | | // byte[] decode = context.getBytes(CHARSET); |
| | | // byte[] bytes = createKeyAndIv(decode, Cipher.ENCRYPT_MODE); |
| | | // return Base64.getEncoder().encodeToString(bytes); |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // return null; |
| | | // } |
| | | |
| | | /** |
| | | * 解密 |
| | |
| | | public static void main(String[] args) { |
| | | String context = "zhaors"; |
| | | System.out.println("元数据" + context); |
| | | String encrypt = encrypt(context); |
| | | System.out.println("加密之后:" + encrypt); |
| | | String decrypt = decrypt(encrypt); |
| | | System.out.println("解密之后:" + decrypt); |
| | | // String encrypt = encrypt(context); |
| | | // System.out.println("加密之后:" + encrypt); |
| | | // String decrypt = decrypt(encrypt); |
| | | // System.out.println("解密之后:" + decrypt); |
| | | } |
| | | } |