这里有一个通过输入文件进行加密解密再输出文件保存的例子,我想把它改成从JTextArea里读取字符串加密后再输出到JTextArea怎么改
import java.io.*;
import java.security.*;
import javax.crypto.*;/**
   使用方法:
   java RSATest -genkey public private
   java RSATest -encrypt plaintext encrypted public
   java RSATest -decrypt encrypted decrypted private
*/
public class RSATest
{
   public static void main(String[] args)
   {
      try
      {
         if (args[0].equals("-genkey"))//产生密钥对
         {
            KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA");
            SecureRandom random = new SecureRandom();
            pairgen.initialize(KEYSIZE, random);
            KeyPair keyPair = pairgen.generateKeyPair();
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));
            out.writeObject(keyPair.getPublic());
            out.close();            
            out = new ObjectOutputStream(new FileOutputStream(args[2]));
            out.writeObject(keyPair.getPrivate());
            out.close();            
         }
         else if (args[0].equals("-encrypt"))//加密
         {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            SecureRandom random = new SecureRandom();
            keygen.init(random);
            SecretKey key = keygen.generateKey();            // wrap with RSA public key
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
            Key publicKey = (Key) keyIn.readObject();
            keyIn.close();                        Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.WRAP_MODE, publicKey);
            byte[] wrappedKey = cipher.wrap(key);
            DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2]));
            out.writeInt(wrappedKey.length);
            out.write(wrappedKey);            InputStream in = new FileInputStream(args[1]);
            cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            crypt(in, out, cipher);
            in.close();
            out.close();
         }
         else//解密
         {
            DataInputStream in = new DataInputStream(new FileInputStream(args[1]));
            int length = in.readInt();
            byte[] wrappedKey = new byte[length];
            in.read(wrappedKey, 0, length);
            
            // unwrap with RSA private key
            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
            Key privateKey = (Key) keyIn.readObject();
            keyIn.close();                        Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.UNWRAP_MODE, privateKey);
            Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);            OutputStream out = new FileOutputStream(args[2]);
            cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, key);            crypt(in, out, cipher);
            in.close();
            out.close();
         }
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
      catch (GeneralSecurityException e)
      {
         e.printStackTrace();
      }
      catch (ClassNotFoundException e)
      {
         e.printStackTrace();
      }
   }    public static void crypt(InputStream in, OutputStream out,
      Cipher cipher) throws IOException, GeneralSecurityException//加密方法
   {
      int blockSize = cipher.getBlockSize();
      int outputSize = cipher.getOutputSize(blockSize);
      byte[] inBytes = new byte[blockSize];
      byte[] outBytes = new byte[outputSize];      int inLength = 0;
      boolean more = true;
      while (more)
      {
         inLength = in.read(inBytes);
         if (inLength == blockSize)
         {
            int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
            out.write(outBytes, 0, outLength);
         }
         else more = false;         
      }
      if (inLength > 0)
         outBytes = cipher.doFinal(inBytes, 0, inLength);
      else
         outBytes = cipher.doFinal();
      out.write(outBytes);
   }   private static final int KEYSIZE = 512;
}     

解决方案 »

  1.   

    从JTextArea读取String:JTextArea.getText();
    输出String到JTextArea: JTextArea.replaceRange(String str, int start, int end);或者使用
    public void setText(String t),查看一下帮助,应该差不多了。http://www.java2s.com/Code/Java/Swing-JFC/Replacetextintextarea.htm这个例子有点用。
      

  2.   

    关键是getText()后怎么办
    这个是AES和RSA混和加密,加密使用字节流形式
    用RSA加密AES密钥,用RSA加密消息 再将加密的AES密钥和加密消息包装起来的 下面部分是像out流写入密钥
    DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2]));
    out.writeInt(wrappedKey.length);//向流中写入加密后密钥长度
    out.write(wrappedKey);//加密后密钥长度
    而解密时还要分离加密的AES密钥和加密消息,用RSA解密加密的AES密钥,再用AES密钥解密消息
    上面源程序已用文件形式实现,关键是getText()得到字符串怎么实现上面的过程
      

  3.   

    上面说错了个地方是 : 用RSA加密AES密钥,用AES加密消息