【问题标题】:Conversion of Variable Value from String to Long in JavaJava中变量值从String到Long的转换
【发布时间】:2014-11-06 12:46:25
【问题描述】:

在 Java 中是否有将字符串的值转换为 long 的方法? 显然,String.format("%040x", new BigInteger(1, message.getBytes(message))); 只改变了它的格式而不是值。我需要更改该值,因为我将使用转换后的变量进行加密(PRINCE)过程,该过程将以长(十六进制)格式处理。

  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();
                }

【问题讨论】:

  • message的内容为例。
  • 它可以是任何支持 160 个字符的句子,因为这个应用程序用于发送短信 @Aaron Digulla
  • 在这种情况下你不认为你需要更多的long 值吗?描述一下 PRINCE 需要的输入值,然后问如何将某个类型的消息转换成输入值?
  • 您可能需要 二进制 值。你不应该说 hexadecimal 值,除非你指的是二进制值的人类可读字符串编码。在您的情况下,二进制值应表示为字节数组(通常用于加密)或长整数。
  • 您打算使用哪种加密模式? ECB(不安全)、CBC 还是 CTR?

标签: java android string cryptography type-conversion


【解决方案1】:

您可以为此使用Long.parseLong()

new BigInteger(1, message.getBytes(message)) 不应该工作。首先,message.getBytes() 针对未知字符集抛出异常。

此外,new BigInteger(1, message.getBytes("UTF-8")) 将针对大多数输入返回奇数结果。

我的猜测是你的问题真的是:我怎样才能将一个字符串转换成字节和那些转换成一个十六进制字符串?

解决方案:

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

从字符串中获取字节数组。然后这个问题告诉你如何将它转换为十六进制字符串:How to convert a byte array to a hex string in Java?

【讨论】:

    【解决方案2】:

    问题可能比人们想象的要多,所以我创建了一个使用特定编码(Latin-1,接近 SMS 接受的编码)的小代码示例,然后返回 LongBuffer包含编码为 long 值,使用 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);
        }
    }
    

    【讨论】:

    • 这当然可以使用CharsetEncoder 和更智能的缓冲区处理来优化,但是对于 SMS 这应该很好。
    • 请注意,您必须对密文进行编码以符合SMS character set
    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 2011-04-20
    • 2011-04-13
    • 2019-07-31
    • 2021-10-08
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多