文件加密解密

代码参考:http://www.apkbus.com/forum.php?mod=viewthread&tid=52121&ordertype=1

核心代码如下:
public class SymEncrypt {

/**
* 获得key
* @param arrBTmp 秘钥字节
* @param alg 加密方式名称
* @return
*/
public static Key getKey(byte[] arrBTmp, String alg){
if(!(alg.equals("DES")||alg.equals("DESede")||alg.equals("AES"))){
System.out.println("alg type not find: "+alg);
return null;
}
byte[] arrB;
if(alg.equals("DES")){
arrB = new byte[8];
}
else if(alg.equals("DESede")){
arrB = new byte[24];
}
else{
arrB = new byte[16];
}
int i=0;
int j=0;
while(i < arrB.length){
if(j>arrBTmp.length-1){
j=0;
}
arrB[i] = arrBTmp[j];
i++;
j++;
}
Key key = new javax.crypto.spec.SecretKeySpec(arrB, alg);
return key;
}

/**
* 加密
* @param s 文件字符串
* @param strKey 密钥字符串
* @param alg 加密方式
* @return
*/
public static byte[] encrypt(String s,String strKey,String alg){
if(!(alg.equals("DES")||alg.equals("DESede")||alg.equals("AES"))){
System.out.println("alg type not find: "+alg);
return null;
}
byte[] r=null;
try {
Key key = getKey(strKey.getBytes(),alg);
Cipher c;  
c = Cipher.getInstance(alg);
c.init(Cipher.ENCRYPT_MODE, key);
r = c.doFinal(s.getBytes());
//System.out.println("加密后的二进串:" + FileDigest.byte2Str(r));
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return r;
}

/**
* 解密
* @param code 文件字节
* @param strKey 密钥
* @param alg 加密方式
* @return
*/
public static String decrypt(byte[] code,String strKey,String alg){
if(!(alg.equals("DES")||alg.equals("DESede")||alg.equals("AES"))){
System.out.println("alg type not find: "+alg);
return null;
}
String r=null;
try {
Key key = getKey(strKey.getBytes(),alg);
Cipher c;
c = Cipher.getInstance(alg);
c.init(Cipher.DECRYPT_MODE,key);
byte[] clearByte=c.doFinal(code);
r=new String(clearByte);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
System.out.println("not padding");
r=null;
}
//System.out.println("解密后的信息:"+r);
return r;
}

}

文件签名如下:

public class RsaVerify {

/**
* 生成密钥
* @param size  长度
* @param prifile 私钥
* @param pubfile 公钥
* @return
*/
public static boolean creatKey(int size, String prifile, String pubfile) {
if ((new java.io.File(prifile)).exists() == false && (new java.io.File(pubfile)).exists() == false) {
if (generatekey(size, prifile, pubfile) == false) {
return false;
} else
return true;
} else
return false;
}

/**
*
* @param s
*                源文件
* @param prifile
*                秘钥文件
* @param sign
*                目标文件
* @return
*/
public static boolean sign(String s, String prifile, String sign) {
boolean r = false;
try {
java.io.ObjectInputStream in = new java.io.ObjectInputStream(new java.io.FileInputStream(
prifile));
PrivateKey myprikey = (PrivateKey) in.readObject();
in.close();

Signature signet = Signature.getInstance("SHA1WithRSA");
signet.initSign(myprikey);
signet.update(s.getBytes());
byte[] signed = signet.sign();

// System.out.println("signed(签名内容)="+FileDigest.byte2Str(signed));

java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(new java.io.FileOutputStream(
sign));
out.writeObject(signed);
out.close();
r = true;
// System.out.println("签名并生成文件成功");
} catch (java.lang.Exception e) {
e.printStackTrace();
// System.out.println("签名并生成文件失败");
r = false;
}
return r;
}

/**
* 签名验证
* @param s 源文件
* @param pubfile  公钥文件
* @param sign  签名文件
* @return
*/
public static boolean checkSign(String s, String pubfile, String sign) {
boolean r = false;
try {
java.io.ObjectInputStream in = new java.io.ObjectInputStream(new java.io.FileInputStream(
pubfile));
PublicKey pubkey = (PublicKey) in.readObject();
in.close();

// System.out.println(pubkey.getFormat());

in = new java.io.ObjectInputStream(new java.io.FileInputStream(sign));
byte[] signed = (byte[]) in.readObject();
in.close();

Signature signetcheck = Signature.getInstance("SHA1WithRSA");
signetcheck.initVerify(pubkey);
signetcheck.update(s.getBytes());
if (signetcheck.verify(signed)) {
// System.out.println("info=" + s);
// System.out.println("签名正常");
r = true;
} else {
// System.out.println("非签名正常");
r = false;
}
} catch (java.lang.Exception e) {
e.printStackTrace();
System.out.println("Error: checkSign failed");
}
return r;
}

/**
* 生成密钥
* @param size 长度
* @param prifile 私钥
* @param pubfile 公钥
* @return
*/
public static boolean generatekey(int size, String prifile, String pubfile) {
try {
KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
keygen.initialize(size);
KeyPair keys = keygen.generateKeyPair();
PublicKey pubkey = keys.getPublic();
PrivateKey prikey = keys.getPrivate();

java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(new java.io.FileOutputStream(
prifile));
out.writeObject((Object) prikey);
out.close();
// System.out.println("写入对象 prikeys ok");
out = new java.io.ObjectOutputStream(new java.io.FileOutputStream(pubfile));
out.writeObject((Object) pubkey);
out.close();
// System.out.println("写入对象 pubkeys ok");
return true;
} catch (java.lang.Exception e) {
e.printStackTrace();
// System.out.println("生成密钥对失败");
return false;
}
}
}

报文鉴别如下:
public class FileDigest {

/**
* SHA1报文鉴别
* @param s 资源文件
* @return 鉴别码文件
*/
public static byte[] digestSHA1(String s) {
byte[] digesta=null;
try {
MessageDigest alg = MessageDigest.getInstance("SHA-1");
alg.update(s.getBytes());
digesta = alg.digest();
}catch (NoSuchAlgorithmException e) {
System.out.println("Error: digestSHA1 failed");
}
return digesta;
}

/**
* MD5报文鉴别
* @param s 资源文件
* @return 鉴别码文件
*/
public static byte[] digestMD5(String s) {
byte[] digesta=null;
MessageDigest alg;
try {
alg = MessageDigest.getInstance("MD5");
alg.update(s.getBytes());
digesta = alg.digest();
} catch (NoSuchAlgorithmException e) {
System.out.println("Error: digestMD5 failed");
}
return digesta;
}

/**
*
* @param b 鉴别码文件
* @param s 资源文件
* @param algo
* @return
*/
public static boolean isEqual(byte[] b,String s,int algo){
MessageDigest alg;
boolean r=false;
try{
if(algo==0){
alg = MessageDigest.getInstance("MD5");
}
else{
alg = MessageDigest.getInstance("SHA-1");
}
alg.update(s.getBytes());
if (MessageDigest.isEqual(b, alg.digest())) {
r=true;
} else {
r=false;
}
}catch (NoSuchAlgorithmException e) {
System.out.println("Error: isEqual failed");
}
return r;
}

/**
* 字节到字符串
* @param b
* @return
*/
public static String byte2Str(byte[] b)
{
String str = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
str = str + "0" + stmp;
else
str = str + stmp;
}
return str;
}

}

你可能感兴趣的:(加密解密)