-
-
Save pentium10/38b43ab0490a0dc9836f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Test { | |
public static byte[] decrypt(byte[] encrypted, byte[] key) | |
throws GeneralSecurityException { | |
SecretKeySpec skeySpec = new SecretKeySpec(key, "Blowfish"); | |
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding"); | |
String iv = "00000000"; | |
IvParameterSpec ivs = new IvParameterSpec(iv.getBytes()); | |
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivs); | |
byte[] decrypted = cipher.doFinal(encrypted); | |
return decrypted; | |
} | |
public static byte[] encrypt(byte[] messageBytes, byte[] key) | |
throws GeneralSecurityException { | |
SecretKeySpec skeySpec = new SecretKeySpec(key, "Blowfish"); | |
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding"); | |
String iv = "00000000"; | |
IvParameterSpec ivs = new IvParameterSpec(iv.getBytes()); | |
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivs); | |
byte[] encrypted = cipher.doFinal(messageBytes); | |
return encrypted; | |
} | |
public static void main(String[] args) throws UnsupportedEncodingException, IOException { | |
// encrypting | |
try { | |
String c = new String(Test.encrypt((new String("thevalue")).getBytes(), | |
(new String("mykey")).getBytes())); | |
System.out.println("Encrypting \"thevalue\": " + c); | |
System.out.println("Base64 encoded String:"+ new sun.misc.BASE64Encoder().encode(c.getBytes())); | |
} catch (GeneralSecurityException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
// Base64 encoded String:wTHzxfxLHdMm/JMFnoh0hciS/D8DvFFg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment