2011-09-29 43 views
-1
public class RSAAPI extends JFrame implements ActionListener{ 
JButton encrypt,decrypt,clear,exit; 
JLabel plainlbl,encrlbl,decrlbl; 
JTextArea plainText,encrText,decrText; 

static KeyPair kp; 
static KeyPairGenerator kpg; 
static Cipher cipher; 
static PublicKey publicKey; 
static PrivateKey privateKey; 

public RSAAPI() 
{ 
    setLayout(null); 

    encrypt=new JButton("ENCRYPT"); 
    decrypt=new JButton("DECRYPT"); 
    clear=new JButton("CLEAR"); 
    exit=new JButton("EXIT"); 

    plainText=new JTextArea(); 
    encrText=new JTextArea(); 
    decrText=new JTextArea(); 

    plainlbl=new JLabel("PLAIN-TEXT"); 
    encrlbl=new JLabel("ENCRYPTED-TEXT"); 
    decrlbl=new JLabel("DECRYPTED-TEXT"); 

    plainlbl.setBounds(30,20,120,15); 
    plainText.setBounds(30,40,400,150); 

    encrypt.setBounds(180,195,90,15); 
    encrlbl.setBounds(30,215,120,15); 
    encrText.setBounds(30,235,400,150); 

    decrypt.setBounds(180,390,90,15); 
    decrlbl.setBounds(30,410,120,15); 
    decrText.setBounds(30,430,400,150); 

    clear.setBounds(80,585,90,15); 
    exit.setBounds(250,585,90,15);  

    add(encrypt); 
    add(decrypt); 
    add(clear); 
    add(plainlbl); 
    add(decrlbl); 
    add(encrlbl); 
    add(exit); 
    add(plainText); 
    add(encrText); 
    add(decrText); 

    encrypt.addActionListener(this); 
    decrypt.addActionListener(this); 
    clear.addActionListener(this); 
    exit.addActionListener(this); 

    setSize(500,650); 
    setVisible(true); 
    decrypt.setEnabled(false); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

public void actionPerformed(ActionEvent ae) 
{ 
    byte[] inData,outData; 
    if(ae.getSource()==encrypt) 
    { 
     try{ 
      inData= plainText.getText().getBytes(); 
      outData=RSAAPI.encryptData(inData); 
      encrText.setLineWrap(true); 
      encrText.setText(new String(outData)); 
      decrypt.setEnabled(true); 
     } 
     catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
    else if(ae.getSource()==decrypt) 
    { 
     try{ 
      inData= encrText.getText().getBytes(); 
      outData=RSAAPI.decryptData(inData); 
      decrText.setLineWrap(true); 
      decrText.setText(new String(outData)); 
      decrypt.setEnabled(false); 
     } 
     catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
    else if(ae.getSource()==clear) 
    { 
     plainText.setText(null); 
     encrText.setText(null); 
     decrText.setText(null); 
     decrypt.setEnabled(false); 
    } 
    else if(ae.getSource()==exit) 
    { 
     System.exit(0); 
    } 
} 

public static byte[] encryptData(byte[] inData) throws Exception 
{ 
    publicKey=kp.getPublic(); 
    cipher=Cipher.getInstance("RSA"); 
    cipher.init(Cipher.ENCRYPT_MODE,publicKey); 
    return cipher.doFinal(inData); 
} 

public static byte[] decryptData(byte[] inData) throws Exception 
{ 
    privateKey=kp.getPrivate(); 
    cipher=Cipher.getInstance("RSA"); 
    cipher.init(Cipher.DECRYPT_MODE,privateKey); 
    return cipher.doFinal(inData);  
} 

public static void main(String[] args) throws Exception 
{ 
    kpg=KeyPairGenerator.getInstance("RSA"); 
    kpg.initialize(1024); 
    kp=kpg.genKeyPair(); 
    new RSAAPI(); 
} 

}RSA加密使用Java API /解密产生BadPaddingException:数据必须以零

+0

-1:无任何描述周围粘贴你​​的程序是不是要问问题的好办法。 – kdgregory

+0

这不是一个问题,它只是一个很少上下文的代码转储。如果您可以改进它,请将其标记为主持人审查。 –

回答

3

你会发现,你所得到的byte阵列之间的差异,您已经加密,并且被设置为您尝试解密的JTextAreabyte阵列中的字符串。

尝试使用Base64编码显示你的加密数据这样:

encrText.setText(new String(new BASE64Encoder().encode(outData))); 

inData = new BASE64Decoder().decodeBuffer(encrText.getText()); 
+0

+1解决方案。问题很可能发生,因为OP在字符串和字节之间转换时依赖于默认编码,并且加密过程产生的字节无法用默认编码转换(很可能使用UTF-8)。 – kdgregory