Ⅰ 如何利用java對文檔進行加密和解密處理,完整的java類
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.Cipher;
public class RSAEncrypt {
	static KeyPairGenerator keyPairGen;
	static KeyPair keyPair;
	static RSAPrivateKey privateKey;
	static RSAPublicKey publicKey;
	
	static{
		 {
			keyPairGen = KeyPairGenerator.getInstance("RSA");
			keyPairGen.initialize(512);
			keyPair = keyPairGen.generateKeyPair();
			// Generate keys
			privateKey = (RSAPrivateKey) keyPair.getPrivate();
			publicKey = (RSAPublicKey) keyPair.getPublic();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		RSAEncrypt encrypt = new RSAEncrypt();
		File file = new File(
				"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf.txt");
		File newFile = new File(
				"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf1.txt");
		encrypt.encryptFile(encrypt, file, newFile);
		File file1 = new File(
				"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf1.txt");
		File newFile1 = new File(
				"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf2.txt");
		encrypt.decryptFile(encrypt, file1, newFile1);
	}
	public void encryptFile(RSAEncrypt encrypt, File file, File newFile) {
		try {
			InputStream is = new FileInputStream(file);
			OutputStream os = new FileOutputStream(newFile);
			byte[] bytes = new byte[53];
			while (is.read(bytes) > 0) {
				byte[] e = encrypt.encrypt(RSAEncrypt.publicKey, bytes);
				bytes = new byte[53];
				os.write(e, 0, e.length);
			}
			os.close();
			is.close();
			System.out.println("write success");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void decryptFile(RSAEncrypt encrypt, File file, File newFile) {
		try {
			InputStream is = new FileInputStream(file);
			OutputStream os = new FileOutputStream(newFile);
			byte[] bytes1 = new byte[64];
			while (is.read(bytes1) > 0) {
				byte[] de = encrypt.decrypt(RSAEncrypt.privateKey, bytes1);
				bytes1 = new byte[64];
				os.write(de, 0, de.length);
			}
			os.close();
			is.close();
			System.out.println("write success");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/** */
	/**
	 * * Encrypt String. *
	 * 
	 * @return byte[]
	 */
	protected byte[] encrypt(RSAPublicKey publicKey, byte[] obj) {
		if (publicKey != null) {
			try {
				Cipher cipher = Cipher.getInstance("RSA");
				cipher.init(Cipher.ENCRYPT_MODE, publicKey);
				return cipher.doFinal(obj);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	/** */
	/**
	 * * Basic decrypt method *
	 * 
	 * @return byte[]
	 */
	protected byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) {
		if (privateKey != null) {
			try {
				Cipher cipher = Cipher.getInstance("RSA");
				cipher.init(Cipher.DECRYPT_MODE, privateKey);
				return cipher.doFinal(obj);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
加解密需要依靠以下四個屬性,你可以把它存文件,存資料庫都無所謂:
	static KeyPairGenerator keyPairGen;
	static KeyPair keyPair;
	static RSAPrivateKey privateKey;
	static RSAPublicKey publicKey;
以上只是示例,怎麼存放根據自己情況
Ⅱ 如何使用java進行sha1加密
使用下面的語句即可:
digestutils.shahex(要加密的字元);加密參數最好用位元組數組,畢竟sha1演算法是使用位元組為單位進行運算的,字元串轉位元組還與字元編碼有關。
Ⅲ Java 加密解密的方法都有哪些
加密解密並非java才有的,所有編程語言都有加密和解密。
目前的加密解密主要回可分為以下2大類:
對稱秘答鑰加密:如DES演算法,3DES演算法,TDEA演算法,Blowfish演算法,RC5演算法,IDEA演算法等。其主要特點是加密方和解密方都有同一個密碼,加密方和解密方可以使用秘鑰任意加密解密。
非對稱密碼加密:這種加密方式加密方僅有加密秘鑰,對加密後的密文無法反向解密,解密方僅有解密秘鑰,無法對明文進行加密。
另外還有一些摘要演算法,比如MD5和HASH此類演算法不可逆,但經常用來作為確認欄位或者對一些重要匹配信息簽名防止明文內容被修改。
Ⅳ php 如何實現 java的sha1加密
function
encryptTokey($data){
$apikey
=
'testapikey111';
$ps1
=
sha1($apikey
.
strtolower($data));
$ps1
=
strtoupper($ps1);
$s1
=
implode(str_split($ps1,
2),
'-');
$ps2
=
md5($s1
.
$apikey);
$ps2
=
strtoupper($ps2);
$token
=
implode(str_split($ps2,
2),
'-');
return
$token;
}
echo
encryptTokey('testdata');
運行結果:
68-10-98-74-4C-82-74-4B-CC-49-31-98-46-02-EE-8E
詳細你可以去後盾人看看,這些都是後盾人裡面的,哪裡有詳專細的視頻教學都是高質量,我屬自己就是在裡面學的。
Ⅳ 如何使用java進行sha1加密
簡單的做來法是
1、使用apache的codec jar包對string進行加密自,先下載並引入jar包:http://commons.apache.org/proper/commons-codec/
2、生成:
String sign = DigestUtils.shaHex(str);
 
3.也可以使用工具在線進行sha加密,參考 hash值(md5, sha1, sha256, sha512,crc32) 在線計算,http://www.it399.com/m/FileHash。望採納,謝謝。