· 7 years ago · Aug 01, 2018, 01:02 AM
1Convert byte[] to String, Send as SMS, return byte[] to String
2sMessage = "hello this is a message"
3
4 byte[] byteArray = EncryptBlowfish(sMessage);
5 //Convert the byte[] into a String which can be sent over SMS
6 StringBuffer strb = new StringBuffer();
7
8 for( int x = 0; x<byteArray.length; x++){
9 strb.append(byteArray[x]).append(",");
10 }//for loop
11
12 sMessage = strb.toString();
13
14//Convert the String back to a byte[] which can be decrypted
15 String[] strArray = sMessage.split(",");
16 byte[] byteArray = new byte[strArray.length];
17 int hold;
18
19 for (int x = 0; x < strArray.length; x++) {
20 hold = Integer.parseInt(strArray[x]);
21 byteArray[x] = (byte) hold;
22 }//for loop
23
24 sMessage = DecryptBlowfish(byteArray);
25
26public static byte[] EncryptBlowfish(String msg){
27
28 byte[] encrypted =null;
29
30 try {
31
32
33 Cipher cipher = Cipher.getInstance("Blowfish");
34
35 cipher.init(Cipher.ENCRYPT_MODE, secretkey);
36
37 encrypted = cipher.doFinal(msg.getBytes());
38
39 } catch (){ //NoSuchAlgorithmException, NoSuchPaddingException..etc
40 }
41
42 return encrypted;
43}
44
45public static String DecryptBlowfish(byte[] msg){
46 byte[] decrypted =null;
47 try {
48
49
50 Cipher cipher = Cipher.getInstance("Blowfish");
51
52 cipher.init(Cipher.DECRYPT_MODE, secretkey);
53
54
55 decrypted = cipher.doFinal(msg);
56
57 } catch (){ //NoSuchAlgorithmException, NoSuchPaddingException..etc
58 }
59
60 return decrypted;
61}
62
63// String => byte []
64byte[] bytes = message.getBytes(Charset.forName("ISO-8859-1"));
65
66// byte [] => String
67String foo = new String(bytes, Charset.forName("ISO-8859-1"));
68
69for (String c : Charset.availableCharsets().keySet()) {
70 System.out.println(c);
71}