import java.security.*;
import java.io.*;
class javasecurity
{
String hello = new String("math.exe");
//static KeyPair pair;
//static MessageDigest sha;
//static byte [] testhash;
//static SecureRandom random;
//static KeyPairGenerator keyGen;
//static Signature rsa;
//static byte [] signature;
public static void main(String[] args) //main
{
javasecurity test =new javasecurity();
byte [] hellobyte=test.hello.getBytes();
try
{
FileOutputStream fos= new FileOutputStream("test.txt"); //Instance file object
DataOutputStream dos= new DataOutputStream(fos);
MessageDigest sha = MessageDigest.getInstance("SHA-1");//Instance a Message object
sha.update(hellobyte); //set input to sha
byte [] testhash=sha.digest(); //generate digest
System.out.println(testhash.toString());
KeyPairGenerator keyGen =KeyPairGenerator.getInstance("RSA"); //Instance KeypairGenerator object
SecureRandom random=SecureRandom.getInstance("SHA1PRNG","SUN"); //Instance SecureRandom object
random.setSeed(1000); //set seed
keyGen.initialize(1024,random); //initial KeypairGenerator object
KeyPair pair = keyGen.generateKeyPair(); //make keypair
Signature rsa = Signature.getInstance("SHA1withRSA");
//Initial the object with a private key
PrivateKey priv=pair.getPrivate();
rsa.initSign(priv);
//Update and verify the data
rsa.update(hellobyte);
byte [] sign=rsa.sign();
System.out.println(sign);
//VERIFY a SIGNATURE
//Initial the object with the public key
PublicKey pub = pair.getPublic();
rsa.initVerify(pub);
//update and verify the data
rsa.update(hellobyte);
boolean verifies=rsa.verify(sign);
System.out.println("Signature verifies: " verifies);
}
catch(SignatureException SE)
{}
catch(InvalidKeyException IE)
{}
catch(NoSuchAlgorithmException NSA) //error catch
{}
catch(NoSuchProviderException NSP) //error catch
{}
catch (IOException IOE)
{}
}
} 我想請問的是為何我把加密的東西(byte)轉成String格式print出來之後
發現不論我原文怎麼改...出來加密後印出來的東西也都依樣
這是什麼原因??還是我處理byte[]的方式不對,應該要怎麼處理才可以印出
加密後的字串,show出給user看??