String, byte[], file and other objects encryption and decryption tool collection, including a variety of encryption schemes.

Encryption type Abstract Relevant methods
Simple encryption Another encoding format Base64Util
One-way encryption It can only be encrypted, not decrypted MD5Util, SHAUtil
Symmetric encryption Use the same secret key for encryption and decryption AESUtil, DESUtil
Asymmetric encryption There are public and private keys, one encrypted, the other decrypted RSAUtil

Method of use

Base64util

methods Abstract
String base64EncodeStr(String str) coding
String base64DecodedStr(String str) decoding

Unit tests:

System.out.println("base64"); / base64 string encryption/decryption test assertEquals (" R2NzU2xvb3DkuK3mloc = \ n ", Base64Util base64EncodeStr (" GcsSloop Chinese ")); AssertEquals (" GcsSloop Chinese, "Base64Util base64DecodedStr (" R2NzU2xvb3DkuK3mloc = \ n"));Copy the code

MD5Util

methods Abstract
String md5(String string) Encrypted string
String md5(String string, String slat) Encrypt string with salt
String md5(String string, int times) Multiple encryption
String md5(File file) Calculates the MD5 value of the file

Unit tests:

System.out.println("md5"); AssertEquals ("", md5util.md5 ("")); AssertEquals (" 386 d3ff3fa6def1ec307428e885e03a1 ", MD5Util. Md5 (" GcsSloop Chinese ")); AssertEquals (" fd01aa74bb73bbdb094bae28a558c6d1 MD5Util. Md5 (" GcsSloop Chinese ", "salt")); AssertEquals ("GcsSloop中文", MD5Util. MD5 ("GcsSloop中文", 0)); AssertEquals (" 386 d3ff3fa6def1ec307428e885e03a1 ", MD5Util. Md5 (" GcsSloop Chinese ", 1)); D9fdd834c5c852fa2f946b670f3731f assertEquals (" 2 ", MD5Util md5 (" Chinese GcsSloop ", 2)); AssertEquals (" 211 dd7a16d5a01df756278cea9a38d53 ", MD5Util. Md5 (" Chinese GcsSloop ", 3)); // MD5 File MD5 Test File File = new File("./Encrypt/Test/demo" + ".flv"); assertEquals("a4e592e6160e0102e7ecc4ab6117b700", MD5Util.md5(file));Copy the code

SHAUtil

methods Abstract
String sha(String string, String type) encryption

Unit tests:

System.out.println("sha"); String source = "GcsSloop中文"; assertEquals("b9dd1d754ee3ac16dc584b8fd4655ca581a0637eab8ff25128b0a522372e7233", SHAUtil.sha(source, null)); assertEquals("34d44835ce4cc4d7ecf66428e49273bf02f748d7213be24c767c5f4f", SHAUtil.sha(source, SHAUtil.SHA224)); assertEquals("b9dd1d754ee3ac16dc584b8fd4655ca581a0637eab8ff25128b0a522372e7233", SHAUtil.sha(source, SHAUtil.SHA256)); assertEquals("2e3c27201c21b06b01289ebef09c9c36e752ca6a5b6425ca7b2501b4baaed29876954ca710b7e75c80b7b542df28fde6", SHAUtil.sha(source, SHAUtil.SHA384)); assertEquals("bc3f55fcb03272ee166d7804ccba348ffba05ddce08bf3fab719fa2c97c8dc71993fc9524e21b8fee9491aafc0b309ebca797163bc a45ece7c3dd73dae3698ee", SHAUtil.sha(source, SHAUtil.SHA512));Copy the code

AESUtil

methods Abstract
String aes(String content, String password, int type) Encrypt/decrypt

Unit tests:

System.out.println("aes"); String source = "GcsSloop中文"; String key = "1234567890123456"; System.out.println(" source = "+ source); String aesStr = AESUtil.aes(source, key, Cipher.ENCRYPT_MODE); System.out.println(" after encryption = "+ aesStr); String result = AESUtil.aes(aesStr, key, Cipher.DECRYPT_MODE); System.out.println(" decrypt = "+ result); assertEquals(source, result);Copy the code

DESUtil

methods Abstract
String des(String content, String password, int type) Encrypt/decrypt

Unit tests:

System.out.println("des"); String source = "GcsSloop中文"; String key = "1234567890123456"; System.out.println(" source = "+ source); String aesStr = DESUtil.des(source, key, Cipher.ENCRYPT_MODE); System.out.println(" after encryption = "+ aesStr); String result = DESUtil.des(aesStr, key, Cipher.DECRYPT_MODE); System.out.println(" decrypt = "+ result); assertEquals(source, result);Copy the code

RSAUtil

methods Abstract
Map<String, Object> getKeyPair() Randomly obtain the key (public key and private key), encrypt the client public key, and decrypt the server private key
String getKey(Map<String, Object> keyMap, boolean isPublicKey) Obtain the public/private key (true: obtain the public key, false: obtain the private key)
String sign(byte[] data, String privateKey) Obtaining a digital signature
boolean verify(byte[] data, String publicKey, String sign) Digital signature verification
byte[] rsa(byte[] data, String string, int type) Rsa encryption/decryption (generally, public key encryption/private key decryption)

Unit tests:

System.out.println("rsa"); Byte [] data = "GcsSloop中文".getbytes (); byte[] data = "GcsSloop中文".getbytes (); Map<String, Object> keyMap = rsautil.getkeypair (); String publicKey = RSAUtil.getKey(keyMap, true); System.out.println("rsa to obtain the publicKey: "+ publicKey); String privateKey = RSAUtil.getKey(keyMap, false); System.out.println("rsa get privateKey: "+ privateKey); Byte [] rsaPublic = rsautil. rsa(data, publicKey, rsautil. RSA_PUBLIC_ENCRYPT); byte[] rsaPublic = rsautil. System.out.println("rsa public key encryption: "+ new String(rsaPublic)); System.out.println("rsa privateKey decryption: "+ new String(rsautil. rsa(rsaPublic, privateKey, rsautil.rsa_private_decrypt))); Byte [] rsaPrivate = rsautil. rsa(data, privateKey, rsautil. RSA_PRIVATE_ENCRYPT); byte[] rsaPrivate = rsautil. rsa(data, privateKey, rSAUtil. System.out.println("rsa private key encryption: "+ new String(rsaPrivate)); System.out.println("rsa publicKey decryption: "+ new String(rsautil. rsa(rsaPrivate, publicKey, rsautil.rsa_public_decrypt))); String signStr = rsautil. sign(rsaPrivate, privateKey); String signStr = rsautil. sign(rsaPrivate, privateKey); System.out.println("rsa digital signature generation: "+ signStr); System.out.println(" rsautil. verify(rsaPrivate, publicKey, signStr));Copy the code

Add methods

  1. Root in your projectbuild.gradleAdd remote warehouse to
Allprojects {repositories {jcenter () / / is below this line maven {url "http://lib.gcssloop.com/repository/gcssloop-central/"}} }Copy the code
  1. Add concrete dependencies to the modules that need to be referenced.
The compile 'com. Gcssloop. Util: encrypt: 1.0.0'Copy the code

Version information

v1.0.0

Add basic encryption and decryption tools and helper classes.

  • base
    • Base64
    • BaseUtils
    • CloseUtils
    • CryptoProvider
    • TextUtils
  • encode
    • Base64Util
  • oneway
    • MD5Util
    • SHAUtil
  • symmetric
    • AESUtil
    • DESUtil
  • unsymmetric
    • RSAUtil

note

Most of the code in this tool library refers to an aggregated encryption and decryption tool class, but it was found in the testing process that part of the method results are incorrect, and part of the method was modified in the Android upgrade process, so part of the improvement, hereby make a tool library, if found any inaccurate places welcome to submit Issues.

The resources

An aggregated encryption and decryption utility class

Online encryption and decryption

Android DATA encryption Aes encryption

java.security.NoSuchProviderException: no such provider: Crypto

Author’s brief introduction

Author’s Microblog:@GcsSloop

Personal Website:www.gcssloop.com

Copyright information

Copyright (c) 2017 GcsSloop

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Copy the code