Decrypt cypher text
bcprov-ext-jdk15on-151-jar
Add this jar to library folder.
Start a java application.
Get a jframe and name it as main.java,
Add three text fields
type-To type the value
encypt-To display encyted code.
decrypt-To display decrypted cod.
public class Main extends javax.swing.JFrame {
/**
* encryption and decryption
*/
byte[] input;
byte[] keyBytes = "12345678".getBytes(); // our own key
byte[] ivBytes = "input123".getBytes();
SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
Cipher cipher;
byte[] chiperText;
int ctLenght;
/**
* Creates new form Main
*/
public Main() {
initComponents();
}
--------------------------------Encryption-----------------------------------------------------------------------------
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); //type of security
input = type.getText().getBytes();
SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
cipher = Cipher.getInstance("DES/CTR/NoPadding", "BC"); //CTR of encryption
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
chiperText = new byte[cipher.getOutputSize(input.length)];
ctLenght = cipher.update(input, 0, input.length, chiperText, 0);
ctLenght += cipher.doFinal(chiperText, ctLenght);
encrypt.setText(new String(chiperText));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
------------------------------------------------------------Decription------------------------------------
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
cipher.init(Cipher.DECRYPT_MODE, key,ivSpec);
byte[] plainText = new byte[cipher.getOutputSize(ctLenght)];
int ptLength = cipher.update(chiperText, 0,ctLenght,plainText,0);
ptLength += cipher.doFinal(plainText, ptLength);
decrypt.setText(new String(plainText));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
-------------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment