首页 邮箱使用技巧内容详情

java编写一个简单的加密解密程序

12-06 06:59:03 156 企业邮箱
java编写一个简单的加密解密程序

站点名称:java编写一个简单的加密解密程序

所属分类:邮箱使用技巧

相关标签: # 加密解密 # 编写 # 简单

官方网址:

SEO查询: 爱站网 站长工具

进入网站

站点介绍

package md5_sha;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

java编写一个简单的加密解密程序

import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/*
 * 消息摘要 :即对任意长度的数据 进行加密,产生一个唯一的指纹。MD5、SHA
 */
public class MessageDigestTest {
 public String encryptInfo(String info,String algorithm){
  MessageDigest messageDigest=null;
  byte[] message=null;
  try {
    messageDigest=MessageDigest.getInstance(algorithm);//获取消息摘要对象
    messageDigest.update(info.getBytes());   // 添加要进行计算摘要的信息 
    message= messageDigest.digest();   // 得到该摘要 
   // return byte2Str(message);
   // return byte2Hex(message);
    return encryptBase64(message);
  } catch (NoSuchAlgorithmException e) {
   // TODO Auto-generated catch block
   throw new RuntimeException(e);
  }
 }
 public String byte2Str(byte []b){
  ByteArrayOutputStream bos=new ByteArrayOutputStream();
  try {
   OutputStream outputStream=MimeUtility.encode(bos, base64);
   outputStream.write(b);
   return bos.toString();
  } catch (MessagingException e) {
   // TODO Auto-generated catch block
   throw new RuntimeException(e);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   throw new RuntimeException(e);
  }
   
 }
  public String byte2Hex(byte b[]){
   String temp=;
   String hex=;
   for(int i=0;i