Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DataOutputStream dOut = newDataOutputStream(socket.getOutputStream());
- byte[] s2 = Encrypt3.encrypt2(myString);
- dOut.writeInt(s2.length); // write length of the message
- dOut.write(s2);
- import java.security.Key;
- import javax.crypto.Cipher;
- import javax.crypto.spec.SecretKeySpec;
- public class Encrypt3 {
- public static String key = "mykey";
- public static byte[] encrypt2(String text ){
- String encrypt ="";
- try{
- // Create key and cipher
- Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
- Cipher cipher = Cipher.getInstance("AES");
- // encrypt the text
- cipher.init(Cipher.ENCRYPT_MODE, aesKey);
- byte[] encrypted = cipher.doFinal(text.getBytes());
- return encrypted ;
- }catch(Exception e){
- System.out.println(e);
- }
- return null ;
- }
- public static String decrypt2(byte[] encrypted2){
- String decrypt ="";
- try{
- Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
- Cipher cipher = Cipher.getInstance("AES");
- // decrypt the text
- cipher.init(Cipher.DECRYPT_MODE, aesKey);
- decrypt = new String(cipher.doFinal(encrypted2));
- }catch(Exception e){
- System.out.println(e);
- }
- return decrypt ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement