1

Is there anyway of converting value of string to long in Java? Apparently, String.format("%040x", new BigInteger(1, message.getBytes(message))); only changes the format of it instead of the value. I need to change the value as I will be using the converted variable for encryption (PRINCE) process which will be processed in long (hexadecimal) format.

  public void onClick(View v, String args) 
                {             
                 String phoneNo = txtPhoneNo.getText().toString();
                 String message = txtMessage.getText().toString();              
                    if (phoneNo.length()>0 && message.length()>0){
                        //prince pri = new prince();
                        //message = toLong();
                        String.format("%040x", new BigInteger(1, message.getBytes(message)));
                        prince.Encrypt (message, k0, kop, k1, t);
                        sendSMS(phoneNo, message); }               
                    else
                     Toast.makeText(getBaseContext(), 
                            "Please enter both phone number and message.", 
                            Toast.LENGTH_SHORT).show();
                }
Anon
  • 37
  • 1
  • 6
  • Give an example for the content of `message`. – Aaron Digulla Nov 06 '14 at 12:49
  • it could be any sentences that supports 160 characters as this app is used for sms @Aaron Digulla – Anon Nov 06 '14 at 12:52
  • Don't you think you need more `long` values in that case? What about describing the input values required for PRINCE, and then asking how to convert a message of a certain type to the input values? – Maarten Bodewes Nov 06 '14 at 13:07
  • You may need *binary* values. You should not say *hexadecimal* values unless you mean the human readable string encoding of the binary values. In your case the binary values should be either represented as arrays of bytes (common for crypto) or longs. – Maarten Bodewes Nov 06 '14 at 13:17
  • What kind of mode of encryption are you planning on using? ECB (unsafe), CBC or CTR? – Maarten Bodewes Nov 06 '14 at 13:23

2 Answers2

0

You can use Long.parseLong() for that.

new BigInteger(1, message.getBytes(message)) shouldn't work. First of all, message.getBytes() throws an exception for unknown charsets.

Also, new BigInteger(1, message.getBytes("UTF-8")) will return odd results for most input.

My guess is your question really is: How can I convert a String into bytes and those into a hex string?

Solution:

byte[] data = message.getBytes("UTF-8");

To get a byte array from the string. Then this question tells you how you can convert this to a hex String: How to convert a byte array to a hex string in Java?

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

There may be more to the question then people may think, so I've created a small code sample that uses a specific encoding (Latin-1, which comes close to what SMS accepts), and then returns a LongBuffer that contains the encoding as long values, padded using PKCS#5/7.

import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import java.nio.charset.StandardCharsets;

public class TooLong {

    private static int PRINCE_BLOCK_SIZE = 64 / Byte.SIZE;

    /**
     * Converts a message to a LongBuffer that is completely filled.
     * Latin encoding and PKCS#7 padding are applied to the message.
     * @param message the message
     * @return the message as converted 
     */
    private static LongBuffer messageToLongBuffer(String message) {
        byte[] messageData = message.getBytes(StandardCharsets.ISO_8859_1);
        int paddingSize = PRINCE_BLOCK_SIZE - messageData.length % PRINCE_BLOCK_SIZE;
        ByteBuffer messageBuf = ByteBuffer.allocate(messageData.length + paddingSize);
        messageBuf.put(messageData);
        for (int i = 0; i < paddingSize; i++) {
            messageBuf.put((byte) paddingSize);
        }
        messageBuf.flip();
        return messageBuf.asLongBuffer();
    }

    public static void main(String[] args) {
        String message = "owlstead gets your problem";
        LongBuffer messageBuf = messageToLongBuffer(message);
        // the long buffer will be completely filled, so you can
        // use longBuffer.array() as well if you prefer long[] instead
        while (messageBuf.hasRemaining()) {
            System.out.printf("%016X ", messageBuf.get());
        }

        // in case you need it as long[] (flip only needed because of the printf statement before)
        messageBuf.flip();
        long[] messageData = new long[messageBuf.remaining()];
        messageBuf.get(messageData);
    }
}
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • This can be optimized using a `CharsetEncoder` and a smarter handling of buffers of course, but for SMS this should do nicely. – Maarten Bodewes Nov 06 '14 at 13:42
  • Beware that you have to encode your ciphertext to comply with the [SMS character set](http://stackoverflow.com/questions/5186702/looking-for-a-list-of-valid-characters-that-can-be-sent-in-sms-text-messages) – Maarten Bodewes Nov 06 '14 at 13:43