資訊保安第一次作業,AES CBC加密,RSA金鑰簽名

鴨脖發表於2013-05-02
package com.yelbosh.first;


import it.sauronsoftware.base64.Base64;


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class AESOperator {
/*
* 加密用的Key 可以用26個字母和數字組成
* 此處使用AES-128-CBC加密模式,key需要為16位。
*/
private String sKey="0123456789abcdef";
private String ivParameter="0123456789abcdef";
private static AESOperator instance=null;
private AESOperator(){


}
public static AESOperator getInstance(){
if (instance == null)
instance =  new AESOperator();
return instance;
}
// 加密
public String encrypt(String sSrc) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());//使用CBC模式,需要一個向量iv,可增加加密演算法的強度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
return new String(Base64.encode(encrypted));//此處使用BASE64做轉碼。
}


// 解密
public String decrypt(String sSrc) throws Exception {
try {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = Base64.decode(sSrc.getBytes());//先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original,"utf-8");
return originalString;
} catch (Exception ex) {
return null;
}
}


}

















package com.yelbosh.first;


import it.sauronsoftware.base64.Base64;


import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;


public class KeyGenerater {
private byte[] priKey;
private byte[] pubKey;


public void generater() {
try {
java.security.KeyPairGenerator keygen = java.security.KeyPairGenerator
.getInstance("RSA");
SecureRandom secrand = new SecureRandom();
secrand.setSeed("syj".getBytes()); // 初始化隨機產生器
keygen.initialize(1024, secrand);
KeyPair keys = keygen.genKeyPair();


PublicKey pubkey = keys.getPublic();
PrivateKey prikey = keys.getPrivate();


pubKey = Base64.encode(pubkey.getEncoded());
priKey = Base64.encode(prikey.getEncoded());
System.out.println("成功生成金鑰!");
System.out.println("pubKey = " + new String(pubKey));
System.out.println("priKey = " + new String(priKey));
} catch (java.lang.Exception e) {
System.out.println("生成金鑰對失敗");
e.printStackTrace();
}
}


public byte[] getPriKey() {
return priKey;
}


public byte[] getPubKey() {
return pubKey;
}
}














package com.yelbosh.first;


import it.sauronsoftware.base64.Base64;


import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;


public class Signaturer {
//使用私鑰和明文生成傳送者對該檔案的簽名
public static byte[] sign(byte[] priKeyText, String plainText,boolean md5) {
 try {
 PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64
 .decode(priKeyText));
 KeyFactory keyf = KeyFactory.getInstance("RSA");
 PrivateKey prikey = keyf.generatePrivate(priPKCS8);


 // 用私鑰對資訊生成數字簽名
 java.security.Signature signet = null;
 if(md5){
 signet = java.security.Signature
 .getInstance("MD5withRSA");
 }else{
 signet = java.security.Signature
 .getInstance("SHA512withRSA");
 }
 signet.initSign(prikey);
 signet.update(plainText.getBytes());
 byte[] signed = Base64.encode(signet.sign());
 return signed;
 } catch (java.lang.Exception e) {
 System.out.println("簽名失敗");
 e.printStackTrace();
 }
 return null;
}
}











package com.yelbosh.first;


import it.sauronsoftware.base64.Base64;


public class SignProvider {
//使用公鑰,明文,簽名來驗證簽名是否合法
public static boolean verify(byte[] pubKeyText, String plainText,
  byte[] signText,boolean md5) {
 try {
  // 解密由base64編碼的公鑰,並構造X509EncodedKeySpec物件
  java.security.spec.X509EncodedKeySpec bobPubKeySpec = new java.security.spec.X509EncodedKeySpec(
    Base64.decode(pubKeyText));
  // RSA對稱加密演算法
  java.security.KeyFactory keyFactory = java.security.KeyFactory
    .getInstance("RSA");
  // 取公鑰匙物件
  java.security.PublicKey pubKey = keyFactory
    .generatePublic(bobPubKeySpec);
  // 解密由base64編碼的數字簽名
  byte[] signed = Base64.decode(signText);
  java.security.Signature signatureChecker = null;
  if(md5)
  signatureChecker = java.security.Signature
  .getInstance("MD5withRSA");
  else
  signatureChecker = java.security.Signature
    .getInstance("SHA512withRSA");
  signatureChecker.initVerify(pubKey);
  signatureChecker.update(plainText.getBytes());
  // 驗證簽名是否正常
  if (signatureChecker.verify(signed))
   return true;
  else
   return false;
 } catch (Throwable e) {
  System.out.println("校驗簽名失敗");
  e.printStackTrace();
  return false;
 }
}


}





package com.yelbosh.first;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class Main {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.out.println("請輸入您要加密的檔案的路徑:\n");
Scanner scanner = new Scanner(System.in);
String fpath = scanner.next();
File file = new File(fpath);
FileReader fReader = new FileReader(file);
char[] tmp = new char[(int) file.length()];
fReader.read(tmp);
//讀取的明文!!!!!!!!!!!
String content = new String(tmp);
//System.out.println(content);
System.out.println("檔案讀取中……");
Thread.currentThread().sleep(500);
System.out.println("檔案讀取完畢!");
Thread.currentThread().sleep(500);
System.out.println("按任意鍵對檔案進行AES的CBC模式加密:\n");
scanner.next();
AESOperator aesoptr = AESOperator.getInstance();
long lStart = System.currentTimeMillis();
String step1Str = aesoptr.encrypt(content);
long lUseTime = System.currentTimeMillis() - lStart;
//System.out.println(step1Str);
System.out.println("加密完成!耗時:" + lUseTime+"ms");
FileWriter fWriter = new FileWriter("./step1.txt");
fWriter.write(step1Str);
fWriter.flush();
fWriter.close();
Thread.currentThread().sleep(500);
System.out.println("密文成功寫入當前目錄下step1.txt檔案中!");
Thread.currentThread().sleep(500);
System.out.println("按任意鍵對密文進行公鑰簽名:\n");
scanner.next();
System.out.println("選擇hash演算法:");
System.out.println("1.MD5\n2.SHA512");
boolean md5 = (scanner.nextInt()==1?true:false);
KeyGenerater keyGen = new KeyGenerater();
keyGen.generater();
fWriter = new FileWriter("./step2PubKey.txt");
fWriter.write(new String(keyGen.getPubKey()));
fWriter.flush();
fWriter.close();
System.out.println("公鑰成功寫入當前目錄下step2PubKey.txt檔案中!");
Thread.currentThread().sleep(500);
fWriter = new FileWriter("./step2PriKey.txt");
fWriter.write(new String(keyGen.getPriKey()));
fWriter.flush();
fWriter.close();
System.out.println("私鑰成功寫入當前目錄下step2PriKey.txt檔案中!");
Thread.currentThread().sleep(500);
System.out.println("按任意鍵對密文生成簽名:\n");
scanner.next();
byte[] sig = Signaturer.sign(keyGen.getPriKey(), step1Str, md5);
Thread.currentThread().sleep(500);
fWriter = new FileWriter("./step2Sig.txt");
fWriter.write(new String(sig));
fWriter.flush();
fWriter.close();
System.out.println("簽名成功!成功寫入當前目錄下step2Sig.txt檔案中!");

Thread.currentThread().sleep(1000);
System.out.println("讓我們來驗證與解密一下吧!");
Thread.currentThread().sleep(500);
System.out.println("簽名驗證中……");
Thread.currentThread().sleep(500);
if(SignProvider.verify(keyGen.getPubKey(), step1Str, sig, md5))
System.out.println("驗證通過!");
Thread.currentThread().sleep(500);
System.out.println("解密中……");
Thread.currentThread().sleep(500);
long lStart2 = System.currentTimeMillis();
String step3Str = aesoptr.decrypt(step1Str);
long lUseTime2 = System.currentTimeMillis() - lStart2;
//System.out.println(step1Str);
System.out.println("解密完成!耗時:" + lUseTime2+"ms");
System.out.println("明文為:"+step3Str);
}


}

相關文章