Java Cryptographic algorithm sharing (RSA decryption, symmetric encryption, MD5 encryption) _java

Source: Internet
Author: User
Tags generator md5 md5 encryption sha1

Copy Code code as follows:

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 {
/**
* RSA decryption of data by incoming name and public key
* <br> return value: String
* <br> @param src
* <br> @param PubKey
* <br> @return
*/
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 a 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);
}

}
/**
* Decryption of data by incoming RSA ciphertext and private key
* <br> return value: String
* <br> @param sec
* <br> @param Privkey
* <br> @return
*/
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 a 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);
}

}

/**
* Incoming string, key, and encrypted string (symmetric encryption encryption), support: DES, AES, Desede (3DES)
* <br> return value: String cipher
* <br> @param src
* <br> @param key
* <br> @param method (DES, AES, Desede)
* <br> @return
*/
Symmetric cryptographic encryption
public static string Doubkeyencoding (String src,string keysrc,string method) {
Secretkey key;
try {
Generate key
keygenerator kg = keygenerator.getinstance (method);
Initializes this 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 lost data scenarios
byte[] by = Base64encoderstream.encode (Ciph.dofinal ());
Return a 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);
}
}
/**
* Incoming string, key, encryption method, and decryption string (symmetric encryption decryption), support: DES, AES, Desede (3DES)
* <br> Production time: May 2, 2014 1:12:13
* <br> return Value: String key Original
* <br> @param sec
* <br> @param key
* <br> @param method (DES, AES, Desede)
* <br> @return
*/
public static string Doubkeydencoding (String sec,string keysrc,string method) {
Secretkey key;
try {
Generate key
keygenerator kg = keygenerator.getinstance (method);
Initializes this 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, one to avoid losing data scenarios
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);
}
}

/**
* One-way information encryption (Information Digest), support: MD5, MD2, SHA (SHA-1,SHA1), SHA-256, SHA-384, SHA-512,
* <br> return value: Ciphertext after String encryption
* <br> @param src incoming encrypted string (plaintext)
* <br> @param method to specify the algorithm (MD5, MD2, SHA (SHA-1,SHA1), SHA-256, SHA-384, SHA-512)
* <br> @return
*/
public static string ecodingpasswd (String src,string method) {

try {
Information Digest algorithm
MessageDigest MD5 = Messagedigest.getinstance (method);
Md5.update (Src.getbytes ());
byte[] encoding = Md5.digest ();
Code with 64 to avoid lost data scenarios
return new String (Base64encoderstream.encode (encoding));
catch (NoSuchAlgorithmException e) {
throw new RuntimeException (e+) encryption failed!! ");
}

}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.