| | |
| | | return Arrays.copyOf(output, length); |
| | | } |
| | | |
| | | public static String decrypt(String keys,String secert){ |
| | | byte[] key = keys.getBytes(StandardCharsets.UTF_8); |
| | | byte[] ming= Base64.getDecoder().decode(secert); |
| | | byte[] text=decrypt(key,ming); |
| | | return new String(text, StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | public static String encrypt(String keys, String input){ |
| | | byte[] key = keys.getBytes(StandardCharsets.UTF_8); |
| | | return encrypt(key,input.getBytes()); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | byte[] key = "jsimjrby3wqb7dbq".getBytes(StandardCharsets.UTF_8); // 16字节密钥 |
| | | |
| | | // 原始明文 |
| | | String plaintext = "hello"; |
| | | String plaintext = "{\n" + |
| | | "\"regioncode\": \"650000\",\n" + |
| | | "\"deviceid\": \"dh11111111111\",\n" + |
| | | "\"brand\": \"大疆\",\n" + |
| | | "\"model\": \"M3D\",\n" + |
| | | "\"longitude\": 120.6670,\n" + |
| | | "\"latitude\": 30.4567,\n" + |
| | | "\"height\": 45,\n" + |
| | | "\"radius\": 10000\n" + |
| | | "}\n"; |
| | | |
| | | System.out.println("Encoded: " + encrypt(key, plaintext.getBytes(StandardCharsets.UTF_8))); |
| | | String secert =encrypt(key,plaintext.getBytes()); |
| | | System.out.println(secert); |
| | | |
| | | // 解码并解密 |
| | | byte[] decoded = Base64.getDecoder().decode(encrypt(key, plaintext.getBytes(StandardCharsets.UTF_8))); |
| | | |
| | | byte[] ming= Base64.getDecoder().decode(secert); |
| | | byte[] text=decrypt(key,ming); |
| | | System.out.println(new String(text, StandardCharsets.UTF_8)); |
| | | System.out.println("封装"+encrypt("jsimjrby3wqb7dbq","hello")); |
| | | System.out.println("封装"+decrypt("jsimjrby3wqb7dbq","MkzmRdHSy/pB4sdTjNHb4ngG0q7Z+vWzNKdJxJFkhw4Y9tI8R9sapL6ECLyVnKZwiZbmVQFtn+sT294nCVdskK8TUx7CCI3qo21d9I4ONl+XAYnlDCtEa8KSFK3CB3/D9zRt96zEDkv31Av2rdAJW9O7Le1Z13wWGmsEqHjjrXLv5jd/Cop6+BB9+lqDRlhx+ubNOFdUgJALe4wHjZPCMdE0ob44Hx+1iLg7m/WJQWc=")); |
| | | // 直接加密 |
| | | String encryptedBase64 = encrypt(key, plaintext.getBytes(StandardCharsets.UTF_8)); |
| | | // System.out.println("Encrypted (Base64): " + encryptedBase64); |
| | | |
| | | // 直接解密 |
| | | byte[] decoded = Base64.getDecoder().decode(encryptedBase64); |
| | | byte[] decrypted = decrypt(key, decoded); |
| | | |
| | | // 输出结果 |
| | | System.out.println("Plaintext: " + plaintext); |
| | | System.out.println("Encrypted (Base64): " + encrypt(key, plaintext.getBytes(StandardCharsets.UTF_8))); |
| | | System.out.println("Decrypted: " + new String(decrypted, StandardCharsets.UTF_8)); |
| | | // // 输出结果 |
| | | // System.out.println("Plaintext: " + plaintext); |
| | | // System.out.println("Decrypted: " + new String(decrypted, StandardCharsets.UTF_8)); |
| | | } |
| | | } |