資訊保安第一次作業,AES CBC加密,RSA金鑰簽名
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);
}
}
相關文章
- AES CBC 加密解密加密解密
- python實現aes加密解密,RSA簽名和驗籤,RSA加密解密,並呼叫介面Python加密解密
- AES-CBC 模式加密模式加密
- golang AES-CBC 加密解密Golang加密解密
- python AES-CBC 加密解密Python加密解密
- C# Rsa加密(私鑰加密、公鑰解密、金鑰格式轉換、支援超大長度分段加密)C#加密解密
- C# 的RSA加密解密簽名,就為了支援 PEM PKCS#8 格式金鑰對的匯入匯出C#加密解密
- nodejs常用加密方式 RSA & AESNodeJS加密
- 安全篇-AES/RSA加密機制加密
- java使用RSA加密方式,實現數字簽名Java加密
- RSA加密解密及數字簽名Java實現加密解密Java
- 支付介面教程——公鑰與私鑰,雙鑰加密和數字簽名加密
- php中aes加密和rsa加密的區別PHP加密
- 公鑰加密、數字簽名、訊息認證加密
- RSA公開金鑰系統
- OpenSSL工具生成RSA金鑰對
- # RSA 公鑰加密演算法加密演算法
- 隱藏Android簽名檔案和金鑰資訊Android
- RSA金鑰的跨平臺通用
- 一篇搞定RSA加密與SHA簽名|與Java完全同步加密Java
- C#和JAVA的RSA金鑰、公鑰轉換C#Java
- RSA簽名的PSS模式模式
- python RSA簽名和解籤Python
- java程式設計之:生成rsa金鑰Java程式設計
- SSH-keygen rsa 金鑰對根據私鑰生成公鑰
- PHP 實現 AES-128-CBC-PKCS5Padding 加密PHPpadding加密
- RSA非對稱加密演算法中的金鑰對生成與傳輸加密演算法
- 蘋果ios簽名的公鑰與私鑰蘋果iOS
- PHP 做 RSA 簽名 生成訂單PHP
- SHA256withRSA金鑰加簽
- 公開金鑰演算法-RSA-note演算法
- 10 建立SSL與RSA證書與金鑰
- AES線上加密解密-附AES128,192,256,CBC,CFB,ECB,OFB,PCBC各種加密解密原始碼加密解密原始碼
- iOS端基於RSA公鑰加密和解密iOS加密解密
- 蘋果企業簽名多少錢-ios企業簽名-企業簽名蘋果iOS
- PHP RSA2 簽名演算法PHP演算法
- RSA der加密 p12解密以及配合AES使用詳解加密解密
- iOS資料加密(Base64,雜湊函式,AES,RSA)iOS加密函式