Convert byte[] to String, Send as SMS, return byte[] to String

convert []byte to string golang
golang byte array to string utf-8
golang byte array to string array
java convert string to unicode value
golang byte array to ascii string
golang create byte from string
convert string to unicode java
golang byte array to hex string

I have some code that is working correctly in Java but when I try to use it in Android it is having problems.

I am attempting to encrypt an SMS text message with the Blowfish algorithm.

The problem with this code (on android) is that it will not accept the byte[] and will not decrypt the message.

SENDING THE SMS

     sMessage = "hello this is a message"

     byte[] byteArray = EncryptBlowfish(sMessage);  
       //Convert the byte[] into a String which can be sent over SMS
      StringBuffer strb = new StringBuffer();

      for( int x = 0; x<byteArray.length; x++){
      strb.append(byteArray[x]).append(",");
      }//for loop

      sMessage = strb.toString();

(sMessage is then sent via SMS)

RECIVING THE SMS

         //Convert the String back to a byte[] which can be decrypted
        String[] strArray = sMessage.split(",");
        byte[] byteArray = new byte[strArray.length];
        int hold;

        for (int x = 0; x < strArray.length; x++) {
        hold = Integer.parseInt(strArray[x]);
        byteArray[x] = (byte) hold;
    }//for loop

        sMessage = DecryptBlowfish(byteArray);

Encryption Method

    public static byte[] EncryptBlowfish(String msg){

    byte[] encrypted =null;

    try {


    Cipher cipher = Cipher.getInstance("Blowfish");

    cipher.init(Cipher.ENCRYPT_MODE, secretkey);

    encrypted = cipher.doFinal(msg.getBytes());

    } catch (){ //NoSuchAlgorithmException, NoSuchPaddingException..etc
   }  

    return encrypted;
}

Decryption Method

        public static String DecryptBlowfish(byte[] msg){
      byte[] decrypted =null;
      try {


       Cipher cipher = Cipher.getInstance("Blowfish");

          cipher.init(Cipher.DECRYPT_MODE, secretkey);


     decrypted = cipher.doFinal(msg);

        } catch (){ //NoSuchAlgorithmException, NoSuchPaddingException..etc
     }  

    return decrypted;
}

The message is being encrypted, this creates a byte[], I have then converted the byte[] to a string, the string's output will look something like this...

46,77,52,11,-108,91,-106,88,-81,-43,14,111,-118,-128,-92,-50,69,-44,100,-94,71,92,-49,116,

this output is then sent over SMS. The string is then convert back into a byte[] but this byte array is not decrypting.

Questions:

  1. Why would this code work in a Java app, but not Android?
  2. Is there a way of making this work in Android?
  3. Is there a better method of converting the byte[] to a String and back.

(Please comment if anymore information is require, Thanks)


I think the answer involves what the default character encoding is on Android vs standard Java. What happens if you specify the character encoding using msg.getBytes(Charset c), and for decoding new String(byte [], Charset c).

Example:

// String => byte []
byte[] bytes = message.getBytes(Charset.forName("ISO-8859-1"));

// byte [] => String 
String foo = new String(bytes, Charset.forName("ISO-8859-1"));

You can find what character sets are available from:

for (String c : Charset.availableCharsets().keySet()) {
    System.out.println(c);
}

JMS message types and conversion, Sending and receiving a JMSObjectMessage shows a String object being The queue manager converts the text message from the JMS_IBM_CHARACTER_SET specified the item returns a different type to the type that was sent, the type is converted. intValue(); byte[] textBytes = new byte[ TEXT_LENGTH ]; message. you can use System.Text.Encoding.Unicode.GetBytes() to retrieve the set of bytes that Microsoft.Net would using to represent the characters. Using UTF8 conversion


I think there is a problem when you make byte -> string -> byte conversion. Try to send an unencrypted string and retrieve it and check if it is correct.

You should probably specify the encoding at each step.

Pro .NET 1.1 Network Programming, UnicodeFncoding Encodes each Unicode character in 2 bytes (16-bit). characters through e-mail systems optimized for US-ASCII text messages. aim of these methods is to convert between byte arrays and character arrays or strings. of bytes and returns the calculated number of characters produced by decoding. this output is then sent over SMS. The string is then convert back into a byte[] but this byte array is not decrypting. Questions: Why would this code work in a Java app, but not Android? Is there a way of making this work in Android? Is there a better method of converting the byte[] to a String and back.


To convert from a byte array to a string use this

Base64.encodeToString(byteArray, Base64.NO_WRAP);

To convert from a string to a byte array use this

Base64.decode(string, Base64.DEFAULT);

The Base64 class is in the android.util package.

Different ways to convert Byte Array into String in Golang, func BytesToString(b []byte) string {. bh := (*reflect.SliceHeader)(unsafe.Pointer(​&b)). sh := reflect.StringHeader{bh.Data, bh.Len}. return *(*string)(unsafe. How to convert a bytes32 to string: pragma solidity ^0.4.15; contract Bytes32ToString { function bytes32ToStr(bytes32 _bytes32) public pure returns (string) { // string memory str = string(_bytes32); // TypeError: Explicit type conversion not allowed from "bytes32" to "string storage pointer" // thus we should fist convert bytes32 to bytes (to dynamically-sized byte array) bytes memory


Pro ASP.NET Web API Security: Securing ASP.NET Web API, BlockSize / 8]; // Converting 128 bits to bytes byte[] key = new byte[provider. My plain text message of “ 1234 5678 9012 3456 06/13" contains the credit card data that I want to send Alice string creditCard = "1234 5678 9012 3456 06/13"; byte[] For encryption, I pass the encryptor object returned by the CreateEncryptor  The idea here is to create the byte stream from specified byte array. Then read all characters from the byte stream and return stream as a string. The following code example shows how to implement this. The solution automatically tries to determine the encoding used using byte order mark (BOM) in the byte stream. If not found, UTF-8 encoding is


Programming Python: Powerful Object-Oriented Programming, X, though, because base64 returns bytes, the normal mail encoder in email also as a Base64encoded str string, not bytes, and is converted to bytes only when In short, it's impossible to make text message parts today without specializing  The sample works fine, until I send the encrypted byte[] to a string and then back a new byte[] (simulating the recieving page's process). Once the calls string s1 = Encoding.Unicode.GetString(encryptedData);


Java Internationalization: Converting to and from Unicode, byte[] bytes = new byte[10]; String str = new String(bytes, Charset. Convert C# Byte Array To String. This code snippet is an example of how to convert a byte array into a string. String conversion includes two types. First, conversion and display of C# byte array into a string format, and second, conversion of C# bytes into actual characters of string.