KeyStore를 이용하여 Key Generation하고, 그 Key를 가지고 암호화.
SHA1withRSA, SHA1withDSA 완성[Sha1withRSA]p[Sha1withDSA]
============================================================================================
import java.io.*;
import java.security.*;
public class DSA {
private PublicKey publickey;
private PrivateKey privatekey;
public DSA (PublicKey puk, PrivateKey pik) {
publickey = puk;
privatekey = pik;
}
public DSA (String keystorefile, String alias, String storepass, String keypass) throws Exception {
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(keystorefile), storepass.toCharArray());
privatekey = (PrivateKey)ks.getKey(alias, keypass.toCharArray());
publickey = (PublicKey)ks.getCertificate(alias).getPublicKey();
}
public byte[] Sign (byte[] data, int length) throws Exception{
//sign 생성
Signature signature = Signature.getInstance("SHA1withDSA");
signature.initSign(privatekey);
signature.update(data, 0, length);
//생성한 sign을 돌려 준다.
byte result[] = signature.sign();
return result;
}
public boolean Verify (byte[] data, int length, byte rawsign[]) throws Exception {
//public key로 서명 검증
Signature signature = Signature.getInstance("SHA1withDSA");
signature.initVerify(publickey);
signature.update(data, 0, length);
if (signature.verify(rawsign))
return true;
else
return false;
}
}
============================================================================================
import java.io.*;
import java.security.*;
public class Test {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print ("Choose type(Keystore, KeyGene): ");
String type = in.readLine();
String alias = new String();
String keypass = new String();
String storepass = new String();
String keystorefile = new String();
String option = new String();
if (type.equals("Keystore"))
{
System.out.print("Enter alias : ");
alias = in.readLine();
// keystore 파일명
System.out.print("Enter keystore file : ");
keystorefile = in.readLine();
// keystore 패스워드
System.out.print("Enter storepass : ");
storepass = in.readLine();
//key pass
System.out.print ("Enter keypass: ");
keypass = in.readLine();
}
// 서명을 생성할 파일
System.out.print("Enter message file : ");
String msgfile = in.readLine();
// 생성한 서명을 저장할 파일
System.out.print("Enter signature file : ");
String signfile = in.readLine();
if (type.equals("Keystore"))
{
// 서명을 생성할 것인지 검증할 것인지
System.out.print("Enter Signature(S) / Verify(V) : ");
option = in.readLine();
}
in.close();
// 서명을 생성할 타입을 결정한다.
Signature signature = Signature.getInstance("DSA");
if (type.equals("KeyGene"))
{
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
kpg.initialize(1024);
KeyPair pair = kpg.genKeyPair();
DSA dsa = new DSA(pair.getPublic(), pair.getPrivate());
FileInputStream ins = new FileInputStream(msgfile); // 서명을 생성할 파일을 연다
byte[] buffer = new byte[8192];
int length;
length = ins.read(buffer);
ins.close();
FileOutputStream out = new FileOutputStream(signfile);
byte[] raw = dsa.Sign(buffer, length);
out.write(raw);
System.out.println("OK");
out.close();
if(dsa.Verify(buffer, length, raw))
System.out.println("The signature is good.");
else
System.out.println("The signature is badd.");
}
else
{
DSA dsa = new DSA(keystorefile, alias, storepass, keypass);
FileInputStream ins = new FileInputStream(msgfile); // 서명을 생성할 파일을 연다
byte[] buffer = new byte[8192];
int length = ins.read(buffer);
if (option.equals("S"))
{
FileOutputStream out = new FileOutputStream(signfile);
byte[] raw = dsa.Sign(buffer, length);
out.write(raw);
System.out.println("OK");
out.close();
}
else
{
FileInputStream fin = new FileInputStream(signfile);
byte[] raw2 = new byte[fin.available()];
fin.read(raw2);
fin.close();
if(dsa.Verify(buffer, length, raw2))
System.out.println("The signature is good.");
else
System.out.println("The signature is badd.");
}
}
}
}
============================================================================================
import java.io.*;
import java.security.*;
public class S_RSA {
private PublicKey publickey;
private PrivateKey privatekey;
public S_RSA (PublicKey puk, PrivateKey pik) {
publickey = puk;
privatekey = pik;
}
public S_RSA (String keystorefile, String alias, String storepass, String keypass) throws Exception {
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(keystorefile), storepass.toCharArray());
privatekey = (PrivateKey)ks.getKey(alias, keypass.toCharArray());
publickey = (PublicKey)ks.getCertificate(alias).getPublicKey();
}
public byte[] Sign (byte[] data, int length) throws Exception{
//sign 생성
Signature signature = Signature.getInstance("SHA1withRSA");
signature.initSign(privatekey);
signature.update(data, 0, length);
//생성한 sign을 돌려 준다.
byte result[] = signature.sign();
return result;
}
public boolean Verify (byte[] data, int length, byte rawsign[]) throws Exception {
//public key로 서명 검증
Signature signature = Signature.getInstance("SHA1withRSA");
signature.initVerify(publickey);
signature.update(data, 0, length);
if (signature.verify(rawsign))
return true;
else
return false;
}
}
============================================================================================
import java.io.*;
import java.security.*;
public class Test_RSA {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print ("Choose type(Keystore, KeyGene): ");
String type = in.readLine();
String alias = new String();
String keypass = new String();
String storepass = new String();
String keystorefile = new String();
String option = new String();
if (type.equals("Keystore"))
{
System.out.print("Enter alias : ");
alias = in.readLine();
// keystore 파일명
System.out.print("Enter keystore file : ");
keystorefile = in.readLine();
// keystore 패스워드
System.out.print("Enter storepass : ");
storepass = in.readLine();
//key pass
System.out.print ("Enter keypass: ");
keypass = in.readLine();
}
// 서명을 생성할 파일
System.out.print("Enter message file : ");
String msgfile = in.readLine();
// 생성한 서명을 저장할 파일
System.out.print("Enter signature file : ");
String signfile = in.readLine();
if (type.equals("Keystore"))
{
// 서명을 생성할 것인지 검증할 것인지
System.out.print("Enter Signature(S) / Verify(V) : ");
option = in.readLine();
}
in.close();
if (type.equals("KeyGene"))
{
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair pair = kpg.genKeyPair();
S_RSA rsa = new S_RSA(pair.getPublic(), pair.getPrivate());
FileInputStream ins = new FileInputStream(msgfile); // 서명을 생성할 파일을 연다
byte[] buffer = new byte[8192];
int length;
length = ins.read(buffer);
ins.close();
FileOutputStream out = new FileOutputStream(signfile);
byte[] raw = rsa.Sign(buffer, length);
out.write(raw);
System.out.println("OK");
out.close();
if(rsa.Verify(buffer, length, raw))
System.out.println("The signature is good.");
else
System.out.println("The signature is badd.");
}
else
{
S_RSA rsa = new S_RSA(keystorefile, alias, storepass, keypass);
FileInputStream ins = new FileInputStream(msgfile); // 서명을 생성할 파일을 연다
byte[] buffer = new byte[8192];
int length = ins.read(buffer);
if (option.equals("S"))
{
FileOutputStream out = new FileOutputStream(signfile);
byte[] raw = rsa.Sign(buffer, length);
out.write(raw);
System.out.println("OK");
out.close();
}
else
{
FileInputStream fin = new FileInputStream(signfile);
byte[] raw2 = new byte[fin.available()];
fin.read(raw2);
fin.close();
if(rsa.Verify(buffer, length, raw2))
System.out.println("The signature is good.");
else
System.out.println("The signature is badd.");
}
}
}
}
'암호화 알고리즘' 카테고리의 다른 글
쉽게보는 ARIA 알고리즘 - 라운드 (0) | 2018.12.26 |
---|---|
(3-DES) 간단한 대칭 알고리즘 예제 (0) | 2018.12.26 |
DES 알고리즘 C (0) | 2018.12.26 |
DB암호화 방식 비교 (0) | 2018.12.26 |
국산 블럭암호 ARIA 기술 (2) | 2016.11.17 |