bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

java密碼流代碼,java流亂碼

JAVA簡單文件加密 求JAVA源代碼

md5加密:

永靖網站制作公司哪家好,找創新互聯!從網頁設計、網站建設、微信開發、APP開發、成都響應式網站建設公司等網站項目制作,到程序開發,運營維護。創新互聯公司2013年成立到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創新互聯

package com.ncs.pki.util;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

public class MD5Test {

private static MessageDigest digest = null;

public synchronized static final String hash(String data) {

if (digest == null) {

try {

digest = MessageDigest.getInstance("MD5");

} catch (NoSuchAlgorithmException nsae) {

System.err.println(

"Failed to load the MD5 MessageDigest. "

+ "Jive will be unable to function normally.");

nsae.printStackTrace();

}

}

// Now, compute hash.

digest.update(data.getBytes());

return encodeHex(digest.digest());

}

public static final String encodeHex(byte[] bytes) {

StringBuffer buf = new StringBuffer(bytes.length * 2);

int i;

for (i = 0; i bytes.length; i++) {

if (((int) bytes[i] 0xff) 0x10) {

buf.append("0");

}

buf.append(Long.toString((int) bytes[i] 0xff, 16));

}

return buf.toString();

}

public static String test(){

return null;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println(MD5Test.hash("123456"));

}

}

3des加密:

package com.ncs.pki.util;

import java.security.Key;

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

public class DesEncrypt {

/**

*

* 使用DES加密與解密,可對byte[],String類型進行加密與解密 密文可使用String,byte[]存儲.

*

* 方法: void getKey(String strKey)從strKey的字條生成一個Key

*

* String getEncString(String strMing)對strMing進行加密,返回String密文 String

* getDesString(String strMi)對strMin進行解密,返回String明文

*

*byte[] getEncCode(byte[] byteS)byte[]型的加密 byte[] getDesCode(byte[]

* byteD)byte[]型的解密

*/

Key key;

/**

* 根據參數生成KEY

*

* @param strKey

*/

public void getKey(String strKey) {

try {

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

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

this.key = _generator.generateKey();

_generator = null;

} catch (Exception e) {

e.printStackTrace();

}

}

/**

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

*

* @param strMing

* @return

*/

public String getEncString(String strMing) {

byte[] byteMi = null;

byte[] byteMing = null;

String strMi = "";

BASE64Encoder base64en = new BASE64Encoder();

try {

byteMing = strMing.getBytes("UTF8");

byteMi = this.getEncCode(byteMing);

strMi = base64en.encode(byteMi);

} catch (Exception e) {

e.printStackTrace();

} finally {

base64en = null;

byteMing = null;

byteMi = null;

}

return strMi;

}

/**

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

*

* @param strMi

* @return

*/

public String getDesString(String strMi) {

BASE64Decoder base64De = new BASE64Decoder();

byte[] byteMing = null;

byte[] byteMi = null;

String strMing = "";

try {

byteMi = base64De.decodeBuffer(strMi);

byteMing = this.getDesCode(byteMi);

strMing = new String(byteMing, "UTF8");

} catch (Exception e) {

e.printStackTrace();

} finally {

base64De = null;

byteMing = null;

byteMi = null;

}

return strMing;

}

/**

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

*

* @param byteS

* @return

*/

private byte[] getEncCode(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) {

e.printStackTrace();

} finally {

cipher = null;

}

return byteFina;

}

/**

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

*

* @param byteD

* @return

*/

private byte[] getDesCode(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) {

e.printStackTrace();

} finally {

cipher = null;

}

return byteFina;

}

public static void main(String[] args) {

System.out.println("des demo");

DesEncrypt des = new DesEncrypt();// 實例化一個對像

des.getKey("MYKEY");// 生成密匙

System.out.println("key=MYKEY");

String strEnc = des.getEncString("111111");// 加密字符串,返回String的密文

System.out.println("密文=" + strEnc);

String strDes = des.getDesString(strEnc);// 把String 類型的密文解密

System.out.println("明文=" + strDes);

}

}

java加密解密代碼

package com.cube.limail.util;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;/**

* 加密解密類

*/

public class Eryptogram

{

private static String Algorithm ="DES";

private String key="CB7A92E3D3491964";

//定義 加密算法,可用 DES,DESede,Blowfish

static boolean debug = false ;

/**

* 構造子注解.

*/

public Eryptogram ()

{

} /**

* 生成密鑰

* @return byte[] 返回生成的密鑰

* @throws exception 扔出異常.

*/

public static byte [] getSecretKey () throws Exception

{

KeyGenerator keygen = KeyGenerator.getInstance (Algorithm );

SecretKey deskey = keygen.generateKey ();

System.out.println ("生成密鑰:"+bytesToHexString (deskey.getEncoded ()));

if (debug ) System.out.println ("生成密鑰:"+bytesToHexString (deskey.getEncoded ()));

return deskey.getEncoded ();

} /**

* 將指定的數據根據提供的密鑰進行加密

* @param input 需要加密的數據

* @param key 密鑰

* @return byte[] 加密后的數據

* @throws Exception

*/

public static byte [] encryptData (byte [] input ,byte [] key ) throws Exception

{

SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );

if (debug )

{

System.out.println ("加密前的二進串:"+byte2hex (input ));

System.out.println ("加密前的字符串:"+new String (input ));

} Cipher c1 = Cipher.getInstance (Algorithm );

c1.init (Cipher.ENCRYPT_MODE ,deskey );

byte [] cipherByte =c1.doFinal (input );

if (debug ) System.out.println ("加密后的二進串:"+byte2hex (cipherByte ));

return cipherByte ;

} /**

* 將給定的已加密的數據通過指定的密鑰進行解密

* @param input 待解密的數據

* @param key 密鑰

* @return byte[] 解密后的數據

* @throws Exception

*/

public static byte [] decryptData (byte [] input ,byte [] key ) throws Exception

{

SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );

if (debug ) System.out.println ("解密前的信息:"+byte2hex (input ));

Cipher c1 = Cipher.getInstance (Algorithm );

c1.init (Cipher.DECRYPT_MODE ,deskey );

byte [] clearByte =c1.doFinal (input );

if (debug )

{

System.out.println ("解密后的二進串:"+byte2hex (clearByte ));

System.out.println ("解密后的字符串:"+(new String (clearByte )));

} return clearByte ;

} /**

* 字節碼轉換成16進制字符串

* @param byte[] b 輸入要轉換的字節碼

* @return String 返回轉換后的16進制字符串

*/

public static String byte2hex (byte [] b )

{

String hs ="";

String stmp ="";

for (int n =0 ;n b.length ;n ++)

{

stmp =(java.lang.Integer.toHexString (b [n ] 0XFF ));

if (stmp.length ()==1 ) hs =hs +"0"+stmp ;

else hs =hs +stmp ;

if (n b.length -1 ) hs =hs +":";

} return hs.toUpperCase ();

}

/**

* 字符串轉成字節數組.

* @param hex 要轉化的字符串.

* @return byte[] 返回轉化后的字符串.

*/

public static byte[] hexStringToByte(String hex) {

int len = (hex.length() / 2);

byte[] result = new byte[len];

char[] achar = hex.toCharArray();

for (int i = 0; i len; i++) {

int pos = i * 2;

result[i] = (byte) (toByte(achar[pos]) 4 | toByte(achar[pos + 1]));

}

return result;

}

private static byte toByte(char c) {

byte b = (byte) "0123456789ABCDEF".indexOf(c);

return b;

}

/**

* 字節數組轉成字符串.

* @param String 要轉化的字符串.

* @return 返回轉化后的字節數組.

*/

public static final String bytesToHexString(byte[] bArray) {

StringBuffer sb = new StringBuffer(bArray.length);

String sTemp;

for (int i = 0; i bArray.length; i++) {

sTemp = Integer.toHexString(0xFF bArray[i]);

if (sTemp.length() 2)

sb.append(0);

sb.append(sTemp.toUpperCase());

}

return sb.toString();

}

/**

* 從數據庫中獲取密鑰.

* @param deptid 企業id.

* @return 要返回的字節數組.

* @throws Exception 可能拋出的異常.

*/

public static byte[] getSecretKey(long deptid) throws Exception {

byte[] key=null;

String value=null;

//CommDao dao=new CommDao();

// List list=dao.getRecordList("from Key k where k.deptid="+deptid);

//if(list.size()0){

//value=((com.csc.sale.bean.Key)list.get(0)).getKey();

value = "CB7A92E3D3491964";

key=hexStringToByte(value);

//}

if (debug)

System.out.println("密鑰:" + value);

return key;

}

public String encryptData2(String data) {

String en = null;

try {

byte[] key=hexStringToByte(this.key);

en = bytesToHexString(encryptData(data.getBytes(),key));

} catch (Exception e) {

e.printStackTrace();

}

return en;

}

public String decryptData2(String data) {

String de = null;

try {

byte[] key=hexStringToByte(this.key);

de = new String(decryptData(hexStringToByte(data),key));

} catch (Exception e) {

e.printStackTrace();

}

return de;

}

} 加密使用: byte[] key=Eryptogram.getSecretKey(deptid); //獲得鑰匙(字節數組)

byte[] tmp=Eryptogram.encryptData(password.getBytes(), key); //傳入密碼和鑰匙,獲得加密后的字節數組的密碼

password=Eryptogram.bytesToHexString(tmp); //將字節數組轉化為字符串,獲得加密后的字符串密碼解密與之差不多

急求:流密碼的算法,用JAVA

Java中的IO流使用的是Decorator設計模式

所以只要寫兩個裝飾者類

覆蓋write和read方法

在write前和read后對原數據進行一些處理(比如異或操作)就可以了

我吃過飯寫個貼上來……

--------------------------------------------------------

// EncryptStream.java

import java.io.IOException;

import java.io.OutputStream;

/**

*

* 類型描述 加密流

*

* @since 2009-5-22

* @author 何智剛

*

*/

public class EncryptStream extends OutputStream {

private byte key;

private OutputStream out;

/**

*

* @param key 密鑰

* @param in 需要加密的流

*/

public EncryptStream(byte key, OutputStream out) {

this.key = key;

this.out = out;

}

@Override

public void write(int b) throws IOException {

out.write(b ^ key);

}

}

// DecryptStream.java

import java.io.IOException;

import java.io.InputStream;

/**

*

* 類型描述 解密流

*

* @since 2009-5-22

* @author 何智剛

*

*/

public class DecryptStream extends InputStream {

private byte key;

private InputStream in;

/**

*

* @param key 密鑰

* @param in 需要解密的流

*/

public DecryptStream(byte key, InputStream in) {

this.key = key;

this.in = in;

}

@Override

public int read() throws IOException {

return in.read() ^ key;

}

@Override

public int read(byte[] b, int off, int len) throws IOException {

byte[] temp = new byte[b.length];

int c = in.read(temp, off, len);

for (int i = 0; i b.length; i++) {

b[i] = (byte) (temp[i] ^ key);

}

return c;

}

}

// Client.java

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

public class Client {

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

byte key = 25;

encryptFile("要加密的文件.dat", "加密過的文件.dat", key);

decryptFile("加密過的文件.dat", "解密出來的文件.dat", key);

}

/**

*

* 方法描述 加密文件

*

* @param src 要加密的文件的路徑

* @param des 加密過后的文件的存放路徑

* @param key 密鑰

* @throws Exception

*

* @變更記錄 2009-5-22 下午12:42:25 何智剛 創建

*

*/

public static void encryptFile(String src, String des, byte key)

throws Exception {

InputStream in = new FileInputStream(src);

OutputStream out = new EncryptStream(key, new FileOutputStream(des));

byte[] buf = new byte[8192];

int c;

while ((c = in.read(buf)) 0) {

out.write(buf, 0, c);

}

in.close();

out.flush();

out.close();

}

/**

*

* 方法描述 解密文件

*

* @param src 要解密的文件的路徑

* @param des 解密過后的文件的存放路徑

* @param key 密鑰

* @throws Exception

*

* @變更記錄 2009-5-22 下午12:43:04 何智剛 創建

*

*/

public static void decryptFile(String src, String des, byte key)

throws Exception {

InputStream in = new DecryptStream(key, new FileInputStream(src));

OutputStream out = new FileOutputStream(des);

byte[] buf = new byte[8192];

int c;

while ((c = in.read(buf)) 0) {

out.write(buf, 0, c);

}

in.close();

out.flush();

out.close();

}

}

-----------------------------------------------

我在例子里沒有用BufferedStream,而是自己創建了個byte[]做緩沖區,因為這樣性能更好。用BufferedStream的話代碼會更簡單。

本文名稱:java密碼流代碼,java流亂碼
網頁鏈接:http://vcdvsql.cn/article0/hecgoo.html

成都網站建設公司_創新互聯,為您提供網站收錄搜索引擎優化網站策劃服務器托管做網站品牌網站建設

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都app開發公司