Cryptography Reference
In-Depth Information
add(entryShiftValue);
add(encipherButton);
encipherButton.addActionListener(this);
add(decipherButton);
decipherButton.addActionListener(this);
decipherButton.setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==encipherButton) {
try {
shift=Integer.parseInt(entryShiftValue.getText());
} catch (NumberFormatException nfe) {
shift=0;
}
msgArray=msg.getText().getBytes();
encmsgArray=caesarEncipher(msgArray,shift);
encmsg.setText(new String(encmsgArray));
msg.setText(“”);
encipherButton.setEnabled(false);
decipherButton.setEnabled(true);
} else if (e.getSource()==decipherButton) {
msgArray=caesarDecipher(encmsgArray,shift);
msg.setText(new String(msgArray));
encmsg.setText(“”);
decipherButton.setEnabled(false);
encipherButton.setEnabled(true);
}
}
//The enciphering method.
private static byte[] caesarEncipher(byte[] message,int shift) {
byte[] m2=new byte[message.length];
for (int i=0;i<message.length;i++) {
m2[i]=(byte)((message[i]+shift)%256);
}
return m2;
}
//The deciphering method.
private static byte[] caesarDecipher(byte[] message,int shift) {
byte[] m2=new byte[message.length];
for (int i=0;i<message.length;i++) {
m2[i]=(byte)((message[i]+(256-shift))%256);
}
return m2;
}
}
Search WWH ::




Custom Search