简体   繁体   中英

Encrypting and Decrypting in Java

I have 3 text boxes in my form.

1.To enter text- entered_txt

2.To show encrypted text - encrypted_txt

3.To show decrypted text - decrypted_txt

When I press encrypt_btn(button), get text from entered_txt and encrypt that and show the result in encrypted_txt

When I press decrypt_btn(button),get text from encrypted_txt and decrypt that and show the result in decrypted_txt

This functionality works fine but, When i use a clear button and try to use the form again Shows an exception,

Codes are shown below;

variable declaration --

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;

Encrypt button;

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();
  }
}

Decrypt button;

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();
   }

Clear button;

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

The exception;

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)

Line 140 is ctLength+= cipher.doFinal(cipherText, ctLength);

How can I correct this code for use the form multiple times using clear button?

Can you try to clear out instead of setting for null? May be your encryption method is executing again and causing the issue on clearing.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM