zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java加密算法分享(rsa解密、对称加密、md5加密)

JAVA加密解密 分享 MD5 rsa 加密算法 对称
2023-06-13 09:15:25 时间

复制代码代码如下:


importjava.io.UnsupportedEncodingException;
importjava.security.InvalidKeyException;
importjava.security.MessageDigest;
importjava.security.NoSuchAlgorithmException;
importjava.security.PrivateKey;
importjava.security.PublicKey;
importjava.security.SecureRandom;

importjavax.crypto.BadPaddingException;
importjavax.crypto.Cipher;
importjavax.crypto.IllegalBlockSizeException;
importjavax.crypto.KeyGenerator;
importjavax.crypto.NoSuchPaddingException;
importjavax.crypto.SecretKey;

importcom.sun.mail.util.BASE64DecoderStream;
importcom.sun.mail.util.BASE64EncoderStream;


publicclassutil{
   /**
    *传入名文和公钥钥对数据进行RSA解密
    *<br>返回值:String
    *<br>@paramsrc
    *<br>@parampubkey
    *<br>@return
    */
   publicstaticStringrsaEncoding(Stringsrc,PublicKeypubkey){
       try{
           Ciphercip=Cipher.getInstance("RSA");
           cip.init(cip.ENCRYPT_MODE,pubkey);
           byte[]by=cip.doFinal(src.getBytes());
           returnnewString(BASE64EncoderStream.encode(by));

       }catch(NoSuchAlgorithmExceptione){
           thrownewRuntimeException(e);
       }catch(NoSuchPaddingExceptione){
           thrownewRuntimeException(e);
       }catch(InvalidKeyExceptione){
           thrownewRuntimeException(e);
       }catch(IllegalBlockSizeExceptione){
           thrownewRuntimeException(e);
       }catch(BadPaddingExceptione){
           thrownewRuntimeException(e);
       }

   }
   /**
    *传入RSA密文和私钥对数据进行解密
    *<br>返回值:String
    *<br>@paramsec
    *<br>@paramprivkey
    *<br>@return
    */
   publicstaticStringrsaDeEncoding(Stringsec,PrivateKeyprivkey){
       try{
           Ciphercip=Cipher.getInstance("RSA");
           cip.init(cip.DECRYPT_MODE,privkey);
           byte[]by=BASE64DecoderStream.decode(sec.getBytes());
           returnnewString(cip.doFinal(by));

       }catch(NoSuchAlgorithmExceptione){
           thrownewRuntimeException(e);
       }catch(NoSuchPaddingExceptione){
           thrownewRuntimeException(e);
       }catch(InvalidKeyExceptione){
           thrownewRuntimeException(e);
       }catch(IllegalBlockSizeExceptione){
           thrownewRuntimeException(e);
       }catch(BadPaddingExceptione){
           thrownewRuntimeException(e);
       }

   }

   /**
    *传入字符串、密钥,并加密字符串(对称加密加密),支持:DES、AES、DESede(3DES)
    *<br>返回值:String密文
    *<br>@paramsrc
    *<br>@paramkey
    *<br>@parammethod(DES、AES、DESede)
    *<br>@return
    */
   //对称加密加密
   publicstaticStringdoubKeyEncoding(Stringsrc,Stringkeysrc,Stringmethod){
       SecretKeykey;
       try{
           //生成密钥
           KeyGeneratorkg= KeyGenerator.getInstance(method);
           //初始化此密钥生成器。
           kg.init(newSecureRandom(keysrc.getBytes("utf-8")));
           key=kg.generateKey();

           //加密
           Cipherciph= Cipher.getInstance(method);
           ciph.init(Cipher.ENCRYPT_MODE,key);
           ciph.update(src.getBytes("utf-8"));
           //使用64进行编码,一避免出现丢数据情景
           byte[]by=BASE64EncoderStream.encode(ciph.doFinal());
           returnnewString(by);
       }catch(NoSuchAlgorithmExceptione){
           thrownewRuntimeException(e);
       }catch(NoSuchPaddingExceptione){
           thrownewRuntimeException(e);
       }catch(InvalidKeyExceptione){
           thrownewRuntimeException(e);
       }catch(IllegalBlockSizeExceptione){
           thrownewRuntimeException(e);
       }catch(BadPaddingExceptione){
           thrownewRuntimeException(e);
       }catch(UnsupportedEncodingExceptione){
           thrownewRuntimeException(e);
       }
   }
   /**
    *传入字符串、密钥、加密方式,并解密字符串(对称加密解密密),支持:DES、AES、DESede(3DES)
    *<br>生成时间:2014年5月2日 下午1:12:13
    *<br>返回值:String密钥原文
    *<br>@paramsec
    *<br>@paramkey
    *<br>@parammethod(DES、AES、DESede)
    *<br>@return
    */
   publicstaticStringdoubKeyDencoding(Stringsec,Stringkeysrc,Stringmethod){
       SecretKeykey;
       try{
           //生成密钥
           KeyGeneratorkg= KeyGenerator.getInstance(method);
           //初始化此密钥生成器。
           kg.init(newSecureRandom(keysrc.getBytes("utf-8")));
           key=kg.generateKey();
           //加密
           Cipherciph= Cipher.getInstance(method);
           ciph.init(ciph.DECRYPT_MODE,key);
           //使用64进行解码,一避免出现丢数据情景
           byte[]by=BASE64DecoderStream.decode(sec.getBytes());
           ciph.update(by);
           returnnewString(ciph.doFinal());

       }catch(NoSuchAlgorithmExceptione){
           thrownewRuntimeException(e);
       }catch(NoSuchPaddingExceptione){
           thrownewRuntimeException(e);
       }catch(InvalidKeyExceptione){
           thrownewRuntimeException(e);
       }catch(IllegalBlockSizeExceptione){
           thrownewRuntimeException(e);
       }catch(BadPaddingExceptione){
           thrownewRuntimeException(e);
       }catch(UnsupportedEncodingExceptione){
           thrownewRuntimeException(e);
       }
   }

   /**
    *单向信息加密(信息摘要),支持:md5、md2、SHA(SHA-1,SHA1)、SHA-256、SHA-384、SHA-512,
    *<br>返回值:String        加密后的密文
    *<br>@paramsrc    传入加密字符串(明文)
    *<br>@parammethod 指定算法(md5、md2、SHA(SHA-1,SHA1)、SHA-256、SHA-384、SHA-512)
    *<br>@return
    */
   publicstaticStringecodingPasswd(Stringsrc,Stringmethod){

       try{
           //信息摘要算法
           MessageDigestmd5=MessageDigest.getInstance(method);
           md5.update(src.getBytes());
           byte[]encoding=md5.digest();
           //使用64进行编码,一避免出现丢数据情景
           returnnewString(BASE64EncoderStream.encode(encoding));
       }catch(NoSuchAlgorithmExceptione){
           thrownewRuntimeException(e+"加密失败!!");
       }

   }
}