WDSsoft免費原始碼,java加密解密編程常用工具包 JCT 1.0 (整理)

來源:互聯網
上載者:User

package jct;

import java.security.*;

/**
* <p>計算字串和byte[]的數字摘要 </p>
* @Copyright:WDSsoft
* @ad:WDSsoft “企業多級數位簽章系統”- 最佳的企業電子文檔多級數位簽章方案
*/
public class Digest {

 

/**
* 計算字串的SHA數字摘要,以byte[]形式返回
**/
public static byte[] MdigestSHA(String source){
byte[] nullreturn={0} ;
try{
MessageDigest thisMD=MessageDigest.getInstance("SHA");
byte[] digest=thisMD.digest(source.getBytes("UTF8"));
return digest;
} catch(Exception e){return null;}
}
/**
* 計算byte[]的SHA數字摘要,以byte[]形式返回
**/
public static byte[] MdigestSHA(byte[] source){
byte[] nullreturn={0} ;
try{
MessageDigest thisMD=MessageDigest.getInstance("SHA");
byte[] digest=thisMD.digest(source);
return digest;
} catch(Exception e){return null;}
}
/**
* 計算字串的MD5數字摘要,以byte[]形式返回
**/
public static byte[] MdigestMD5(String source){
byte[] nullreturn={0} ;
try{
MessageDigest thisMD=MessageDigest.getInstance("MD5");
byte[] digest=thisMD.digest(source.getBytes("UTF8"));
return digest;
} catch(Exception e){return null;}
}
/**
* 計算byte[]的數MD5字摘要,以byte[]形式返回
**/
public static byte[] MdigestMD5(byte[] source){
byte[] nullreturn={0} ;
try{
MessageDigest thisMD=MessageDigest.getInstance("MD5");
byte[] digest=thisMD.digest(source);
return digest;
} catch(Exception e){return null;}
}
public static void main(String[] args){
String test="WDSsoft";

byte[] t1=MdigestSHA(test);
for(int i=0;i<t1.length;i++){
System.out.print(Integer.toString(Math.abs(t1[i])));
}
System.exit(0);
}

}

 

package jct;

import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.Key;
import java.security.KeyFactory;
import java.security.spec.EncodedKeySpec;
import java.security.Security;
import java.util.Random;
import java.util.Vector;

/**
* <p>封裝同PBE(基於口令的加密)演算法有關的方法,可用使用PBE演算法加密和解密</p>
* @Copyright:WDSsoft
*/

public class PBETool
{

public PBETool() { }
/**
*測試
*/
public static void main(String[] args)
{
String tt=new String("java");
char[] password=tt.toCharArray();
byte[] pp;
PBETool pbe=new PBETool();
try{

pp=pbe.PBEunwrap(pbe.PBEwrap(tt.getBytes("UTF8"),password),password);
System.out.print(new String(pp));

}
catch(Exception e){}
}
/**
* 使用PBE加密byte[],隨即撒鹽,和unwrap方法配套使用
* @param byte[] target:PBE加密對象
* @param char[] password: PBE加密口令
* @return Object[] object[0]1:byte[]:密文; object[1]:byte[]:鹽
*/
public Object[] PBEwrap(byte[] target,char[] password){
try{

PBEKeySpec keySpec=new PBEKeySpec(password);
SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key=keyFactory.generateSecret(keySpec);
Cipher cipher=Cipher.getInstance("PBEWithMD5AndDES");
byte[] salt=new byte[8];
Random random=new Random();
random.nextBytes(salt);
PBEParameterSpec paramSpec=new PBEParameterSpec(salt,100);
cipher.init(cipher.ENCRYPT_MODE,key,paramSpec);
Object[] encryptedByte=new Object[2];
encryptedByte[0]=cipher.doFinal(target);
encryptedByte[1]=salt;
return encryptedByte;

}catch(Exception e){e.printStackTrace();
return null;
}
}
/**
* 使用PBE加密byte[],撒固定的鹽,和unwrape_2配套使用
* @param byte[] target:PBE加密對象
* @param char[] password: PBE加密口令
* @return byte[] :加密後的密文
*/
public byte[] PBEwrap_2(byte[] target,char[] password){
try{
PBEKeySpec keySpec=new PBEKeySpec(password);
SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key=keyFactory.generateSecret(keySpec);
Cipher cipher=Cipher.getInstance("PBEWithMD5AndDES");
//固定的鹽,實際試用時可自行更改鹽,但要和unwrape_2一致
byte[] salt=new byte[8];
salt[2]=salt[4]=salt[7]=11;
salt[1]=salt[3]=salt[5]=36;
salt[0]=10;
PBEParameterSpec paramSpec=new PBEParameterSpec(salt,100);
cipher.init(cipher.ENCRYPT_MODE,key,paramSpec);
return cipher.doFinal(target);

}catch(Exception e){e.printStackTrace();
return null;
}
}

 

/**
* PBE解密,和wrap配套使用
* @param Object[] encrypted: object[0] byte[]:密文 object[1]:byte[]:鹽
* @param char[] password:口令
* @return byte[] 明文
*/
public byte[] PBEunwrap(Object[] encrypted,char[] password){
try{

byte[] wraped=(byte[])encrypted[0];
byte[] salt=(byte[])encrypted[1];
PBEKeySpec keySpec=new PBEKeySpec(password);
SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key=keyFactory.generateSecret(keySpec);
Cipher cipher=Cipher.getInstance("PBEWithMD5AndDES");
PBEParameterSpec paramSpec=new PBEParameterSpec(salt,100);
cipher.init(cipher.DECRYPT_MODE,key,paramSpec);
return cipher.doFinal(wraped);

}catch(Exception e){

return null;}
}

/**
* PBE解密,和wrap_2配套使用
* @param byte[] encrypted:密文
* @param char[] password:口令
* @return byte[] 明文
*/

public byte[] PBEunwrap_2(byte[] encrypted,char[] password){
try{

byte[] wraped=encrypted;

//固定的鹽,實際試用時可自行更改鹽,但要和unwrap_2一致
byte[] salt=new byte[8];
salt[2]=salt[4]=salt[7]=11;
salt[1]=salt[3]=salt[5]=36;
salt[0]=10;
PBEKeySpec keySpec=new PBEKeySpec(password);
SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key=keyFactory.generateSecret(keySpec);
Cipher cipher=Cipher.getInstance("PBEWithMD5AndDES");
PBEParameterSpec paramSpec=new PBEParameterSpec(salt,100);
cipher.init(cipher.DECRYPT_MODE,key,paramSpec);
return cipher.doFinal(wraped);

}catch(Exception e){

return null;}
}
}

 

package jct;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.security.Security;
import javax.crypto.Cipher;

/**
* <p>封裝同RSA非對稱式加密演算法有關的方法,可用於數位簽章,RSA加密解密</p>
* @Copyright:WDSsoft
*/

public class RSATool {

public RSATool() { }

/**
* 用一個已打包成byte[]形式的私密金鑰加密資料,即數位簽章
* @param keyInByte 打包成byte[]的私密金鑰
* @param source 要簽名的資料,一般應是數字摘要
* @return 簽名 byte[]
*/
public static byte[] sign(byte[] keyInByte,byte[] source){
try{
PKCS8EncodedKeySpec priv_spec=new PKCS8EncodedKeySpec(keyInByte);
KeyFactory mykeyFactory=KeyFactory.getInstance("RSA");
PrivateKey privKey= mykeyFactory.generatePrivate(priv_spec);
Signature sig=Signature.getInstance("SHA1withRSA");
sig.initSign(privKey);
sig.update(source);
return sig.sign();
}catch(Exception e){ return null; }
}
/**
* 驗證數位簽章
* @param keyInByte 打包成byte[]形式的公開金鑰
* @param source 原文的數字摘要
* @param sign 簽名(對原文的數字摘要的簽名)
* @return 是否證實 boolean
*/
public static boolean verify(byte[] keyInByte,byte[] source,byte[] sign){
try{
KeyFactory mykeyFactory=KeyFactory.getInstance("RSA");
Signature sig=Signature.getInstance("SHA1withRSA");
X509EncodedKeySpec pub_spec=new X509EncodedKeySpec(keyInByte);
PublicKey pubKey=mykeyFactory.generatePublic(pub_spec);
sig.initVerify(pubKey);
sig.update(source);
return sig.verify(sign);
}catch(Exception e){
return false;
}
}
/**
* 建立新的金鑰組,返回打包的byte[]形式私密金鑰和公開金鑰
* @return 包含打包成byte[]形式的私密金鑰和公開金鑰的object[],其中,object[0]為私密金鑰byte[],object[1]為工要byte[]
*/
public static Object[] giveRSAKeyPairInByte(){
KeyPair newKeyPair=creatmyKey();
if(newKeyPair==null)return null;
Object[] re=new Object[2];
if(newKeyPair!=null){
PrivateKey priv=newKeyPair.getPrivate();
byte[] b_priv = priv.getEncoded();
PublicKey pub=newKeyPair.getPublic();
byte[] b_pub=pub.getEncoded();
re[0]=b_priv;
re[1]=b_pub;
return re;
}
return null;
}

/**
* 建立金鑰組
* @return KeyPair對象
*/
public static KeyPair creatmyKey(){
KeyPair myPair;
long mySeed;
mySeed=System.currentTimeMillis();
try{
KeyPairGenerator keyGen=KeyPairGenerator.getInstance("RSA");
SecureRandom random=SecureRandom.getInstance("SHA1PRNG","SUN");
random.setSeed(mySeed);
keyGen.initialize(1024,random);
myPair=keyGen.generateKeyPair();
}catch(Exception e1){ return null; }
return myPair;
}

/**
* 使用RSA公開金鑰加密資料
* @param pubKeyInByte 打包的byte[]形式公開金鑰
* @param data 要加密的資料
* @return 加密資料
*/
public static byte[] encryptByRSA(byte[] pubKeyInByte,byte[] data){
try{
KeyFactory mykeyFactory=KeyFactory.getInstance("RSA");
X509EncodedKeySpec pub_spec=new X509EncodedKeySpec(pubKeyInByte);
PublicKey pubKey=mykeyFactory.generatePublic(pub_spec);
Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE,pubKey);
return cipher.doFinal(data);
}catch(Exception e){ return null; }
}
/**
* 用RSA私密金鑰解密
* @param privKeyInByte 私密金鑰打包成byte[]形式
* @param data 要解密的資料
* @return 解密資料
*/
public static byte[] decryptByRSA(byte[] privKeyInByte,byte[] data){
try{
PKCS8EncodedKeySpec priv_spec=new PKCS8EncodedKeySpec(privKeyInByte);
KeyFactory mykeyFactory=KeyFactory.getInstance("RSA");
PrivateKey privKey= mykeyFactory.generatePrivate(priv_spec);
Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE,privKey);
return cipher.doFinal(data);
}catch(Exception e){ return null; }

}
/**
*測試
*/
public static void main(String[] args) {
try{

Object[] v=giveRSAKeyPairInByte();
byte[] source=Digest.MdigestSHA("haha");
byte[] sign=sign((byte[])v[0],source);
boolean yes=verify((byte[])v[1],source,sign);
if(yes) System.out.print("verify/n");
}catch(Exception e){e.printStackTrace();}

}
}

 

package jct;

import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.Key;
import java.security.KeyFactory;
import java.security.spec.EncodedKeySpec;
import java.security.Security;
import java.security.*;
import java.util.Random;
import java.util.Vector;

/**
* <p>封裝同Blowfish對稱式加密演算法有關的方法,包括了使用建立Blowfish密碼,使用Blowfish加密、解密, 使用PBE(基於口令的加密)存取blowfiwsh密碼 </p>
* @Copyright:WDSsoft
*/

 

public class BlowfishTool {

public BlowfishTool() {
}
/**
* 使用Blowfish加密
* @param Key key:密碼
* @param byte[] text:明文
* @return byte[]:密文
*/
public static byte[] encryptReturnByte(Key key,byte[] text){
try{
Cipher cipher=Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE,key);
return cipher.doFinal(text);
}catch(Exception e){ return null; }
}
/**
* Blowfish解密
* @param Key key:密碼
* @param byte[] text:密文
* @return byte[] : 明文
*/
public static byte[] decryptReturnByte(Key key,byte[] text){
try{
Cipher cipher=Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE,key);
return cipher.doFinal(text);
}catch(Exception e){ return null;}
}
/**
* 使用PBE保管Blowfish密碼
* @param Key targetKey: Blowfish密碼
* @param char[] password: 口令
* @return Vector-element1:byte[]:密文-element2:byte[]:鹽
*/

public static Vector wrapKey(Key targetKey,char[] password){
try{
PBEKeySpec keySpec=new PBEKeySpec(password);
SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key=keyFactory.generateSecret(keySpec);
Cipher cipher=Cipher.getInstance("PBEWithMD5AndDES");
byte[] salt=new byte[8];
Random random=new Random();
random.nextBytes(salt);
PBEParameterSpec paramSpec=new PBEParameterSpec(salt,100);
cipher.init(cipher.WRAP_MODE,key,paramSpec);
Vector encryptedKey=new Vector();
encryptedKey.addElement(cipher.wrap(targetKey));
encryptedKey.addElement(salt);
return encryptedKey;
}catch(Exception e){e.printStackTrace();
return null;
}
}
/**
* 取出PBE加密的密鑰
* @param Vector encryptedKey-element1:byte[]:密文-element2:byte[]:鹽
* @param char[] password:口令
* @return Key:Blowfish密碼
*/
public static Key unwrapKey(Vector encryptedKey,char[] password){
try{

byte[] wrapedKey=(byte[])encryptedKey.elementAt(0);
byte[] salt=(byte[])encryptedKey.elementAt(1);
PBEKeySpec keySpec=new PBEKeySpec(password);
SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key=keyFactory.generateSecret(keySpec);
Cipher cipher=Cipher.getInstance("PBEWithMD5AndDES");
PBEParameterSpec paramSpec=new PBEParameterSpec(salt,100);
cipher.init(cipher.UNWRAP_MODE,key,paramSpec);
return cipher.unwrap(wrapedKey,"Blowfish",cipher.SECRET_KEY);
}catch(Exception e){ return null; }
}
/**
* 產生一個Blowfish密碼
* @return Key Blowfish 密碼
*/
public static Key creatKey(){
try{
KeyGenerator keyGenerator=KeyGenerator.getInstance("Blowfish");
keyGenerator.init(128);
Key key=keyGenerator.generateKey();
return key;
}catch(Exception e){return null;}
}

/**
* 將Blowfish密碼Key 對象轉為byte[] 形式
* @param Key Blowfish密碼Key 對象
* @return byte[]形式Blowfish密碼
*/
public static byte[] encodeKey(Key key){
return key.getEncoded();
}

/**
* 將byte[] 形式Blowfish密碼轉為Key 對象
* @param byte[] 形式Blowfish密碼
* @return Key Blowfish密碼Key 對象
*/
public static Key decodeKey(byte[] keyByteCode){
SecretKeySpec myKey=new SecretKeySpec(keyByteCode,"Blowfish");
return myKey;
}

}

 

package jct;

/**
*<p>WDSsoft免費原始碼,java加密解密編程常用工具包 JCT 1.0<p><br>
* WDSsoft的企業多級數位簽章系統(MDSS)-目前最佳的企業文檔數位簽章系統<br>
*/

public class _Readme {

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.