Java中的加密和解密

时间:2015-08-06 10:32:48

标签: java encryption

我的表格中有3个文本框。

1.输入text-entered_txt

2.显示加密文本 - encrypted_txt

3.显示解密文本 - decrypted_txt

当我按下encrypt_btn(按钮)时,从entered_txt获取文本并对其进行加密并在encrypted_txt中显示结果

当我按下decrypt_btn(按钮)时,从encrypted_txt获取文本并对其进行解密并在decrypted_txt中显示结果

此功能正常但是,当我使用清除按钮并尝试再次使用该表单时显示异常,

代码如下所示;

变量声明 -

byte [] input ;
byte [] keyBytes = "12345678".getBytes();
byte [] ivBytes ="input123".getBytes();

SecretKeySpec key = new SecretKeySpec(keyBytes,"DES");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
Cipher cipher;
byte[] cipherText;
int ctLength;

加密按钮;

private void encrypt_btnActionPerformed(java.awt.event.ActionEvent evt) {                                            
  try{
      Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
      input = entered_txt.getText().getBytes();
      SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
      IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
      cipher = Cipher.getInstance("DES/CTR/NoPadding","BC");

      cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
      cipherText = new byte[cipher.getOutputSize(input.length)];

      ctLength+=cipher.update(input, 0, input.length, cipherText, 0);

      ctLength+= cipher.doFinal(cipherText, ctLength);
      encrypted_txt.setText(new  String(cipherText));

  }catch(NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e){
      JOptionPane.showMessageDialog(null, e);
      e.printStackTrace();
  }
}

解密按钮;

private void decrypt_btnActionPerformed(java.awt.event.ActionEvent evt) {                                            
   try{
       cipher.init(cipher.DECRYPT_MODE, key, ivSpec);

       byte[] plainText = new byte[cipher.getOutputSize(ctLength)];

       int ptLength =cipher.update(cipherText,0,ctLength,plainText);
       ptLength+=cipher.doFinal(plainText, ptLength);

       decypted_txt.setText(new String(plainText));

   }catch(InvalidKeyException | InvalidAlgorithmParameterException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e){

       e.printStackTrace();
   }

清除按钮;

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    entered_txt.setText(null);
    encrypted_txt.setText(null);
    decypted_txt.setText(null);
}  

例外;

javax.crypto.ShortBufferException: output buffer too short for doFinal()
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(Cipher.java:2068)
at com.bit.project.NewJFrame.enActionPerformed(NewJFrame.java:140)
at com.bit.project.NewJFrame.access$100(NewJFrame.java:23)
at com.bit.project.NewJFrame$2.actionPerformed(NewJFrame.java:68)

第140行是ctLength+= cipher.doFinal(cipherText, ctLength);

如何使用清除按钮多次使用表单更正此代码?

1 个答案:

答案 0 :(得分:0)

您可以尝试清除而不是设置为null吗?可能是您的加密方法再次执行并导致清除问题。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    entered_txt.setText("");
    encrypted_txt.setText("");
    decypted_txt.setText("");
}