java加密MD5,DES

des,可以直接拿过去用的,,,不用深入了解算法的整个过程

package com.hotel.EncryptionAndDecryption;


import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.security.InvalidKeyException;

import java.security.Key;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import java.security.spec.InvalidKeySpecException;


import javax.crypto.Cipher;

import javax.crypto.CipherInputStream;

import javax.crypto.CipherOutputStream;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESKeySpec;


import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;


/**

 * @author jxj

 * @category DES加密,原理未懂,直接拿来用。

 * */

public class DES {

Key key;


public DES() {


}


public DES(String str) {

setKey(str); // 生成密匙

}


public Key getKey() {

return key;

}


public void setKey(Key key) {

this.key = key;

}


/**

* 根据参数生成 KEY

*/

public void setKey(String strKey) {

try {

KeyGenerator _generator = KeyGenerator.getInstance("DES");

_generator.init(new SecureRandom(strKey.getBytes()));

this.key = _generator.generateKey();

_generator = null;

} catch (Exception e) {

System.out.print("error in generator key!!");

}

}


/**

* 使用SecretKeyFactory根据参数生成 KEY

*/

// private SecretKey generateKey(String secretKey)

// throws NoSuchAlgorithmException, InvalidKeyException,

// InvalidKeySpecException {

// SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

// DESKeySpec keySpec = new DESKeySpec(secretKey.getBytes());

// keyFactory.generateSecret(keySpec);

// return keyFactory.generateSecret(keySpec);

// }


/**

* 加密 String 明文输入 ,String 密文输出

*/

public String encryptStr(String strMing) {

byte[] byteMi = null;

byte[] byteMing = null;

String strMi = "";

BASE64Encoder base64en = new BASE64Encoder();

try {

byteMing = strMing.getBytes("UTF-8");

byteMi = this.encryptByte(byteMing);

strMi = base64en.encode(byteMi);

} catch (Exception e) {


} finally {

base64en = null;

byteMing = null;

byteMi = null;

}

return strMi;

}


/**

* 解密 以 String 密文输入 ,String 明文输出

* @param strMi

* @return

*/

public String decryptStr(String strMi) {

BASE64Decoder base64De = new BASE64Decoder();

byte[] byteMing = null;

byte[] byteMi = null;

String strMing = "";

try {

byteMi = base64De.decodeBuffer(strMi);

byteMing = this.decryptByte(byteMi);

strMing = new String(byteMing, "UTF-8");

} catch (Exception e) {

System.out.print("error in decryptStr!!");

} finally {

base64De = null;

byteMing = null;

byteMi = null;

}

return strMing;

}


/**

* 加密以 byte[] 明文输入 ,byte[] 密文输出

* @param byteS

* @return

*/

private byte[] encryptByte(byte[] byteS) {

byte[] byteFina = null;

Cipher cipher;

try {

cipher = Cipher.getInstance("DES");

cipher.init(Cipher.ENCRYPT_MODE, key);

byteFina = cipher.doFinal(byteS);

} catch (Exception e) {


} finally {

cipher = null;

}

return byteFina;

}


/**

* 解密以 byte[] 密文输入 , 以 byte[] 明文输出

* @param byteD

* @return

*/

private byte[] decryptByte(byte[] byteD) {

Cipher cipher;

byte[] byteFina = null;

try {

cipher = Cipher.getInstance("DES");

cipher.init(Cipher.DECRYPT_MODE, key);

byteFina = cipher.doFinal(byteD);

} catch (Exception e) {

System.out.print("error in decryptByte!!");

} finally {

cipher = null;

}

return byteFina;

}


/**

* 文件 file 进行加密并保存目标文件 destFile 中

* @param file

*            要加密的文件 如 c:/test/srcFile.txt

* @param destFile

*            加密后存放的文件名 如 c:/ 加密后文件 .txt

*/

public void encryptFile(String file, String destFile) throws Exception {

Cipher cipher = Cipher.getInstance("DES");

// cipher.init(Cipher.ENCRYPT_MODE, getKey());

cipher.init(Cipher.ENCRYPT_MODE, this.key);

InputStream is = new FileInputStream(file);

OutputStream out = new FileOutputStream(destFile);

CipherInputStream cis = new CipherInputStream(is, cipher);

byte[] buffer = new byte[1024];

int r;

while ((r = cis.read(buffer)) > 0) {

out.write(buffer, 0, r);

}

cis.close();

is.close();

out.close();

}


/**

* 文件采用 DES 算法解密文件

* @param file

*            已加密的文件 如 c:/ 加密后文件 .txt *

* @param destFile

*            解密后存放的文件名 如 c:/ test/ 解密后文件 .txt

*/

public void decryptFile(String file, String dest) throws Exception {

Cipher cipher = Cipher.getInstance("DES");

cipher.init(Cipher.DECRYPT_MODE, this.key);

InputStream is = new FileInputStream(file);

OutputStream out = new FileOutputStream(dest);

CipherOutputStream cos = new CipherOutputStream(out, cipher);

byte[] buffer = new byte[1024];

int r;

while ((r = is.read(buffer)) >= 0) {

cos.write(buffer, 0, r);

}

cos.close();

out.close();

is.close();

}


public String getDecryptFile(String file) throws Exception {

InputStream is = new FileInputStream(file);

byte[] buffer = new byte[1024];

is.read(buffer);

is.close();

return new String(buffer, "UTF-8");

}


public static void main(String[] args) throws Exception {

DES des = new DES("1234567");

// DES 加密文件

// des.encryptFile("G:/test.doc", "G:/ 加密 test.doc");

//DES 解密文件

// des.decryptFile("G:/ 加密 test.doc", "G:/ 解密 test.doc");

String str1 = " 要加密的字符串 test";

// DES 加密字符串

String str2 = des.encryptStr(str1);

// DES 解密字符串

String deStr = des.decryptStr(str2);

System.out.println(" 加密前: " + str1);

System.out.println(" 加密后: " + str2);

System.out.println(" 解密后: " + deStr);

}

}


MD5加密

package test.md5;

import java.security.MessageDigest;

public class MD5Util {

    public final static String MD5(String s) {

        char hexDigits[]={‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘};       

        try {

            byte[] btInput = s.getBytes();

            // 获得MD5摘要算法的 MessageDigest 对象

            MessageDigest mdInst = MessageDigest.getInstance("MD5");

            // 使用指定的字节更新摘要

            mdInst.update(btInput);

            // 获得密文

            byte[] md = mdInst.digest();

            // 把密文转换成十六进制的字符串形式

            int j = md.length;

            char str[] = new char[j * 2];

            int k = 0;

            for (int i = 0; i < j; i++) {

                byte byte0 = md[i];

                str[k++] = hexDigits[byte0 >>> 4 & 0xf];

                str[k++] = hexDigits[byte0 & 0xf];

            }

            return new String(str);

        } catch (Exception e) {

            e.printStackTrace();

            return null;

        }

    }

    public static void main(String[] args) {

        System.out.println(MD5Util.MD5("20121221"));

        System.out.println(MD5Util.MD5("加密"));

    }

}


本文出自 “7439523” 博客,请务必保留此出处http://7449523.blog.51cto.com/7439523/1426554

java加密MD5,DES,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。