Java encryption algorithm sharing of rsa decryption symmetric encryption md5 encryption

  • 2020-04-01 03:18:18
  • OfStack


import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import com.sun.mail.util.BASE64DecoderStream;
import com.sun.mail.util.BASE64EncoderStream;

public class util {
    
    public static String rsaEncoding(String src,PublicKey pubkey){
        try {
            Cipher cip = Cipher.getInstance("RSA");
            cip.init(cip.ENCRYPT_MODE, pubkey);
            byte[] by = cip.doFinal(src.getBytes());
            return new String(BASE64EncoderStream.encode(by));

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        }

    }
    
    public static String rsaDeEncoding(String sec,PrivateKey privkey){
        try {
            Cipher cip = Cipher.getInstance("RSA");
            cip.init(cip.DECRYPT_MODE, privkey);
            byte[] by = BASE64DecoderStream.decode(sec.getBytes());
            return new String(cip.doFinal(by));

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        }

    }

    
    //Symmetric encryption
    public static String doubKeyEncoding(String src,String keysrc,String method) {
        SecretKey key;
        try {
            //Generate the key
            KeyGenerator kg =  KeyGenerator.getInstance(method);
            //Initialize the key generator.
            kg.init(new SecureRandom(keysrc.getBytes("utf-8")));
            key = kg.generateKey();

            //encryption
            Cipher ciph =  Cipher.getInstance(method);
            ciph.init(Cipher.ENCRYPT_MODE, key);
            ciph.update(src.getBytes("utf-8"));
            //Code with 64 to avoid data loss scenarios
            byte[] by = BASE64EncoderStream.encode(ciph.doFinal());
            return new String(by);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
    
    public static String doubKeyDencoding(String sec,String keysrc,String method) {
        SecretKey key;
        try {
            //Generate the key
            KeyGenerator kg =  KeyGenerator.getInstance(method);
            //Initialize the key generator.
            kg.init(new SecureRandom(keysrc.getBytes("utf-8")));
            key = kg.generateKey();
            //encryption
            Cipher ciph =  Cipher.getInstance(method);
            ciph.init(ciph.DECRYPT_MODE, key);
            //Use 64 for decoding, so as to avoid the situation of losing data
            byte[] by = BASE64DecoderStream.decode(sec.getBytes());
            ciph.update(by);
            return new String(ciph.doFinal());

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    
    public static String ecodingPasswd(String src,String method){

        try {
            //Information summarization algorithm
            MessageDigest md5 = MessageDigest.getInstance(method);
            md5.update(src.getBytes());
            byte[] encoding = md5.digest();
            //Code with 64 to avoid data loss scenarios
            return new String(BASE64EncoderStream.encode(encoding));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e+"encryption Failure!!!!! ");
        }

    }
}


Related articles: