Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package encryption;
- import jave.util.Scenner;
- import javax.crypto.Cipher;
- import javax.crypto. spec. IvParameterSpec;
- inport javax.crypto.spec.SecretKeySpec ;
- public class TextEncryption {]
- static Scanner console=new Scanner (System. tn);
- public static void main (String[] ares) {
- try {
- /iget input text to be encrypted
- Systen.out.println("Please enter your message to be encrypted:");
- byte[] input~ console.nextLine().getBytes();
- //specify the key and parameter used for encryption and decryption
- byte[] keyBytes=new byte[] {OxO1, 0x23, 0x45, 0x67, (byte)OxB9, (byte) @xab, (byte) Oxcd, (byte) Gxef};
- byte[] iveytes=new byte[] (0x00, 0x01,0x02, 0x3, 0x84, 0x05, 0x05, 0x07};
- SecretKeySpec key=new SecretKeySpec(keySytes ,"DES™);
- IWarameterSpec ivSpec= new IvParemeterSpec( ivOytes);
- ffinitialisetion of the cipher paranrcters
- Cipher cipher=Cipher.getInstonce("DES/CBC/PKCS5Padding™ );
- //Encryption
- cipher. init(Cipher .ENCRYPT_MODE, key, ivSpec);
- byte[] encrypted-cipher .doFinal(input);
- Systen.out.println("Encrypted:"+ encrypted);
- //Peceyption
- cipher. init( Cipher . DECRYPT_MODE, key, ivSpec);
- byte[] decrypted=cipher. doF inal(encrypted);
- Systen.out.println("Decrypted:” + new String(decrypted));
- }
- catch (Exception ex) {
- System.out.print ("Error catched" + ex.getMessage());
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement