2012-05-23 27 views
0

我做RSA加密並遇到問題。我想加密一個字符串。要轉換字符串,我已經有rsHex數組來轉換它..我運行源代碼,但它給我錯誤說「系統找不到指定的文件」這是我的源代碼。我如何解決他的問題?感謝您幫助我:)RSA加密(java)讀取字符串輸入

import de.flexiprovider.api.keys.PrivateKey; 
import de.flexiprovider.api.keys.PublicKey; 
import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.BufferedReader; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.math.BigInteger; 
import java.security.InvalidKeyException; 
import java.security.Key; 
import java.security.KeyFactory; 
import java.security.KeyPair; 
import java.security.KeyPairGenerator; 
import java.security.NoSuchAlgorithmException; 
import java.security.spec.InvalidKeySpecException; 
import java.security.spec.RSAPrivateKeySpec; 
import java.security.spec.RSAPublicKeySpec; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.crypto.BadPaddingException; 
import javax.crypto.Cipher; 
import javax.crypto.IllegalBlockSizeException; 
import javax.crypto.NoSuchPaddingException; 

public class RSA { 

private String str, s; 
private String chipertext; 
private byte[] cipherData; 


public RSA(String string) throws Exception { 

    try { 


     String input = string; 
     FileReader read = new FileReader(input); 
     BufferedReader reader = new BufferedReader(read); 
     while ((s = reader.readLine()) != null) { 
     byte[] theByteArray = s.getBytes(); 
      setUserinput(string); 
      rsHex(theByteArray); 
} 


    } catch (Exception ex) { 
     Logger.getLogger(RSA.class.getName()).log(Level.SEVERE, null, ex); 
    } 

    //Creating an RSA key pair in Java 
      KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); //instance of KeyPairGenerator 
      kpg.initialize(1024);//bit length of the modulus that required 
      KeyPair kp = kpg.genKeyPair();//returns a KeyPair object 
      Key publicKey = kp.getPublic(); //pull out the public and private keys 
      Key privateKey = kp.getPrivate(); 

      //Saving the public and private key 
      //private key will be placed on our server, and the public key distributed to clients. 
      KeyFactory fact = KeyFactory.getInstance("RSA"); 
      RSAPublicKeySpec pub = (RSAPublicKeySpec) fact.getKeySpec(publicKey, RSAPublicKeySpec.class); 
      RSAPrivateKeySpec priv = (RSAPrivateKeySpec) fact.getKeySpec(privateKey, RSAPrivateKeySpec.class); 

      // Save the file to local drive 
      saveToFile("c:\\public.key", pub.getModulus(), pub.getPublicExponent()); 
      saveToFile("c:\\private.key", priv.getModulus(),priv.getPrivateExponent()); 

    } 
private void rsHex(byte[] bytes) throws Exception { 
    StringBuilder hex = new StringBuilder(); 
    for (byte b : bytes) { 
     String hexString = Integer.toHexString(0x00FF & b); 
     hex.append(hexString.length() == 1 ? "0" + hexString : hexString); 
    } 
setChipertext(hex.toString()); 
} 

//save the moduli and exponents to file, we can just use boring old serialisation 
public void saveToFile(String fileName, BigInteger mod, BigInteger exp) throws IOException { 
    FileOutputStream f = new FileOutputStream(fileName); 
    ObjectOutputStream oos = new ObjectOutputStream(f); 
    oos.writeObject(mod); 
    oos.writeObject(exp); 
    oos.close(); 
} 

////Encryption 
//initialise the cipher with the public key that we previously saved to file. 
    PublicKey readKeyFromFile(String keyFileName) throws IOException { 
    PublicKey key = null; 

    try { 
    FileInputStream fin = new FileInputStream(keyFileName); 
    ObjectInputStream ois = new ObjectInputStream(fin); 
    BigInteger m = (BigInteger) ois.readObject(); 
    BigInteger e = (BigInteger) ois.readObject(); 
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e); 
    KeyFactory fact = KeyFactory.getInstance("RSA"); 
     java.security.PublicKey pubKey = fact.generatePublic(keySpec); 
    ois.close(); 
    } 
    catch (Exception e) { 
    e.printStackTrace(); 
    } 
return key; 
    } 


public void rsaEncrypt(String str)throws Exception { 

PublicKey pubKey = readKeyFromFile(str); 
    Cipher cipher = Cipher.getInstance("RSA"); 
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);//initialise the cipher 
    cipherData = cipher.doFinal(str.getBytes());//passing in the data to be encrypted 


    rsHex(cipherData); 


    } 


public String getUserinput() { 
    return str; 
    } 

    public String getChipertext() { 
    return chipertext; 
    } 

    public void setUserinput(String input) { 
    this.str = input; 
    } 

    public void setChipertext(String chipertext) throws Exception { 
    this.chipertext = chipertext; 

    } 



    } 


    ----main Program------ 
    import java.util.Scanner; 

public class TWO{ 
    public static void main(String[] args) throws Exception{ 


    Scanner scan = new Scanner(System.in); 

    System.out.println("Insert your string"); 
    String str = scan.nextLine(); 

      RSA two = new RSA(str); 


    System.out.println("Encrypted: "+ two.getChipertext()); 



} 
} 
+1

我看到,對於實際的RSA gubbinry,您使用我建議的代碼從這裏http://www.javamex.com/tutorials/cryptography/rsa_encryption.shtml哪一個實際行是你得到的錯誤 - 我想象這是一些本地安裝問題? –

+0

我已經使用該頁面的編碼。現在我面臨的問題是要讀取輸入值 – chilora7

+0

是的,所以也許您可以發佈您正在獲取的Actual Exception,並指出您發佈的代碼中的哪一行。 –

回答

3

的問題是,你正在做一個輸入字符串從用戶,但隨後你的代碼是治療這種好像它是一個通過構建的FileReader與字符串。

而不是所有的廢話與FileReader和BufferedReader,有沒有什麼原因,你不只是使用string.getBytes()?你也似乎讓自己變得非常複雜:你正在接受一個字符串,轉換成一個字節數組,然後再把它轉換成一個字符串(用十六進制表示),然後再把它轉換成一個字節數組。 。當你真的只需要獲取原始字符串的字節表示(getBytes()給出的)並將其直接傳遞給RSA加密時,這會非常麻煩。

+0

我已經完成了閱讀價值的一部分。現在真正的問題出現了。編碼部分InputStream in = RSA.class.getResourceAsStream(keyFileName); System.out.println(「Hello2」); ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in)); System.out.println(「Hello3」);你好3不能打印。我能解決問題嗎?謝謝你。 – chilora7