Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String EncryptString(String plaintext, String password) throws Exception{
- // Create Key
- byte key[] = password.getBytes();
- DESKeySpec desKeySpec = new DESKeySpec(key);
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
- SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
- // Create Cipher
- Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
- desCipher.init(Cipher.ENCRYPT_MODE, secretKey);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- CipherOutputStream cos = new CipherOutputStream(baos, desCipher);
- cos.write(plaintext.getBytes());
- cos.flush();
- cos.close();
- return baos.toString();
- }
- public static String DecryptString(String encryptedText, String password) throws Exception{
- // Create Key
- byte key[] = password.getBytes();
- DESKeySpec desKeySpec = new DESKeySpec(key);
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
- SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
- // Create Cipher
- Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
- desCipher.init(Cipher.DECRYPT_MODE, secretKey);
- ByteArrayInputStream bais = new ByteArrayInputStream(encryptedText.getBytes());
- CipherInputStream cis = new CipherInputStream(bais, desCipher);
- byte[] read = new byte[desCipher.getOutputSize(encryptedText.getBytes().length)];
- cis.read(read, 0, read.length);
- cis.close();
- return new String(read);
- }
- public static void main(String args[]) throws Exception {
- String plain = "Some DUDE thought he had swagger, but he didn't!";
- System.out.println("Encrypting: " + plain);
- String enc = EncryptString(plain, SymmetricCrypt.password);
- System.out.println("Result: " + enc);
- String dec = Decrypt.DecryptString(enc, SymmetricCrypt.password);
- System.out.println("Decrypted to: " + dec);
- }
Add Comment
Please, Sign In to add comment