Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Chiper
import java.io.*; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; class Rextester { private static final String ENCRYPTION = "PBEWithMD5AndDES"; private static final String SALT = "07162534"; private static final int COUNT = 20; public static String encrypt(String pass, String val) { try { byte[] ciphertext; SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ENCRYPTION); PBEKeySpec keySpec = new PBEKeySpec(pass.toCharArray()); SecretKey key = keyFactory.generateSecret(keySpec); PBEParameterSpec paramSpec = new PBEParameterSpec(SALT.getBytes(), COUNT); Cipher cipher = Cipher.getInstance(ENCRYPTION); cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); ciphertext = cipher.doFinal(val.getBytes()); return toHexString(ciphertext); } catch (Exception e) { e.printStackTrace(); } return null; } public static String decrypt(String pass, String val) { try { byte[] decryptedtext; SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ENCRYPTION); PBEKeySpec keySpec = new PBEKeySpec(pass.toCharArray()); SecretKey key = keyFactory.generateSecret(keySpec); PBEParameterSpec paramSpec = new PBEParameterSpec(SALT.getBytes(), COUNT); Cipher cipher = Cipher.getInstance(ENCRYPTION); cipher.init(Cipher.DECRYPT_MODE, key, paramSpec); decryptedtext = cipher.doFinal(toByteArray(val)); return new String(decryptedtext); } catch (Exception e) { e.printStackTrace(); } return null; } private static String toHexString(byte[] in) { byte ch = 0x00; int i = 0; if (in == null || in.length <= 0) { return null; } String pseudo[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" }; StringBuffer out = new StringBuffer(in.length * 2); while (i < in.length) { ch = (byte) (in[i] & 0xF0); ch = (byte) (ch >>> 4); ch = (byte) (ch & 0x0F); out.append(pseudo[(int) ch]); ch = (byte) (in[i] & 0x0F); out.append(pseudo[(int) ch]); i++; } String rslt = new String(out); return rslt; } private static byte[] toByteArray(String hexstring) throws Exception { byte[] result = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); char[] charArray = hexstring.toCharArray(); for (int i=0; i<charArray.length; i+=2) { baos.write(new byte[]{ Integer.decode("0x"+charArray[i]+charArray[i+1]).byteValue() }); } result = baos.toByteArray(); baos.close(); return result; } public static void main(String args[]) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String pass = reader.readLine(); System.out.println( Rextester.encrypt(pass, "mypass") ); System.out.println( Rextester.decrypt(pass, "3C884B1D9B026565") ); } catch (Exception e) { e.printStackTrace(); } } }
run
|
edit
|
history
|
help
0
Only I love u
3e
right swastik
Print Count Strings
Min Quadrato
m-primes
3e
Find Median in Large File of Integers
Thread counter
static