Java KeyPair

import java.io.FileInputStream;
import java.io.IOException;
import java.security.*;
import java.security.cert.CertificateFactory;
import java.security.spec.EncodedKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class Rsa001
{

public static void main(String[] args) throws Exception
{
Rsa001 rsa=new Rsa001();
KeyPair key=rsa.create();
byte[] pub=key.getPublic().getEncoded();
byte[] pri=key.getPrivate().getEncoded();

byte[] data="abc".getBytes();

byte[] sign=rsa.sign(data, pri);

boolean bl=rsa.check(sign, pub, data);

System.out.println("test --- "+bl);

}





public static KeyPair create() throws Exception{




KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);

KeyPair keys = keyGen.generateKeyPair();

System.out.println( "create() okay " );
return keys;
}


public static byte[] sign(byte[] data,byte[] pri) throws Exception{

Signature sig = Signature.getInstance("MD5WithRSA");
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(pri);
PrivateKey privateKey2 = keyFactory.generatePrivate(privateKeySpec);

sig.initSign(privateKey2);

sig.update(data);

byte[] signature = sig.sign();



PrintHex(signature,signature.length);
System.out.println( "sign() oaky " );
return signature;


}



public static byte[] sign2(byte[] data,PrivateKey pri) throws Exception{

Signature sig = Signature.getInstance("MD5WithRSA");

sig.initSign(pri);

sig.update(data);

byte[] signature = sig.sign();

PrintHex(signature,signature.length);
System.out.println( "sign() oaky " );
return signature;
}


public static boolean check(byte[] sign,PublicKey pub,byte[] data) throws Exception{
Signature sig = Signature.getInstance("MD5WithRSA");

KeyFactory keyFactory = KeyFactory.getInstance("RSA");
sig.initVerify(pub);
sig.update(data);

if (sig.verify(sign))
{
return true;
}
else
{
return false;
}
}





public static boolean check(byte[] sign,byte[] pub,byte[] data) throws Exception{
Signature sig = Signature.getInstance("MD5WithRSA");

KeyFactory keyFactory = KeyFactory.getInstance("RSA");
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pub);
PublicKey publicKey2 = keyFactory.generatePublic(publicKeySpec);
sig.initVerify(publicKey2);

sig.update(data);

if (sig.verify(sign))
{
return true;
}
else
{
return false;
}
}




public static void PrintHex(byte data[],int len)
{
int i;
int tmp;
String Tmp="";
for(i=0; i {
if(i%16 == 0)
{
//System.out.println("");

if(i<0x10)
Tmp = "0x000";
if((i<0x100) && (i>=0x10))
Tmp = "0x00";
if((i>=0x100)&&(i<0x1000))
Tmp = "0x0";
if(i>=0x1000)
Tmp = "0x";
//System.out.print(Tmp+Integer.toHexString(i)+"h: ");
}
tmp = data[i];
if(tmp < 0)
tmp = 256 + tmp;
if(tmp <0x10)
System.out.print("0");
else
System.out.print("1");
}
System.out.println("");
}
public String encode(byte[] bytes){
return new sun.misc.BASE64Encoder().encode(bytes);
}
public byte[] decode(String str){
byte[] bt=null;
try{
sun.misc.BASE64Decoder decoder=new sun.misc.BASE64Decoder();
bt=decoder.decodeBuffer(str);

}catch(IOException e){
e.printStackTrace();
}
return bt;
}

}

你可能感兴趣的:(Java,Security)