【问题标题】:Using java to encrypt integers使用java加密整数
【发布时间】:2010-09-24 05:18:42
【问题描述】:

我正在尝试使用 java.security 和 javax.crypto 在 java 中加密一些整数。

问题似乎是 Cipher 类只加密字节数组。我不能直接将整数转换为字节字符串(或者我可以吗?)。最好的方法是什么?

我应该将整数转换为字符串并将字符串转换为字节[]?这似乎效率太低了。

有人知道快速/简单或有效的方法吗?

请告诉我。

提前致谢。

jbu

【问题讨论】:

  • 嗯..你是在不同的字节序之间进行吗?如果是这样,当您将它们从字节数组转换回来时,会导致这些整数不正确..
  • 显然“Java 虚拟机总是使用 big-endian”,所以我想这不是问题。

标签: java encryption cryptography integer bytearray


【解决方案1】:

我的简单解决方案是通过您提供的密钥将整数的 ASCII 值转换为字符串,从而将整数加密为字符串。

解决方案如下:

public String encodeDiscussionId(int Id) {

    String tempEn = Id + "";
    String encryptNum ="";
    for(int i=0;i<tempEn.length();i++) {
        int a = (int)tempEn.charAt(i);
        a+=148113;
        encryptNum +=(char)a;
    }
    return encryptNum;
}

public Integer decodeDiscussionId(String encryptText) {

    String decodeText = "";
    for(int i=0;i<encryptText.length();i++) {
        int a= (int)encryptText.charAt(i);
        a -= 148113;
        decodeText +=(char)a;
    }
    int decodeId = Integer.parseInt(decodeText);
    return decodeId;
}

编码步骤:

  1. 在这里,首先将给定的整数转换为字符串:String temp = givenInt + ""
  2. 扫描String的每个字符,读取该字符的ASCII,并在本例中添加密钥为148113。
  3. 将移位的Integer 转换为Character 并连接到String encryptNum 最后返回它。

解码步骤:

  1. 扫描 String 的每个字符,读取该字符的 ASCII 并用之前的密钥减去它。
  2. 将该值转换为字符并与decodeText 连接。

由于之前的编码输出始终为String '???',并根据输入Id 的位数而有所不同。

【讨论】:

    【解决方案2】:

    只需使用:

        Integer.toString(int).getBytes();
    

    确保您使用原始 int 并且 getBytes() 将返回一个字节数组。不需要做任何其他复杂的事情。

    转换回来:

        Integer.parseInt(encryptedString);
    

    【讨论】:

      【解决方案3】:

      创建一个 4 字节数组,并分 4 步将 int 复制到数组中,使用按位与和位移,就像 Paulo 所说的那样。

      但请记住,诸如 AES 和 DES 之类的块算法使用 8 或 16 字节块,因此您需要将数组填充到算法需要的值。也许将 8 字节数组的前 4 个字节保留为 0,其他 4 个字节包含整数。

      【讨论】:

        【解决方案4】:

        只需使用 NIO。它是为这个特定目的而设计的。 ByteBuffer 和 IntBuffer 将快速、高效、优雅地完成您需要的工作。它将处理大/小端转换、高性能 IO 的“直接”缓冲区,您甚至可以将数据类型混合到字节缓冲区中。

        将整数转换为字节:

        ByteBuffer bbuffer = ByteBuffer.allocate(4*theIntArray.length);
        IntBuffer ibuffer = bbuffer.asIntBuffer(); //wrapper--doesn't allocate more memory
        ibuffer.put(theIntArray);                  //add your int's here; can use 
                                                   //array if you want
        byte[] rawBytes = bbuffer.array();         //returns array backed by bbuffer--
                                                   //i.e. *doesn't* allocate more memory
        

        将字节转换为整数:

        ByteBuffer bbuffer = ByteBuffer.wrap(rawBytes);
        IntBuffer ibuffer = bbuffer.asIntBuffer();
        while(ibuffer.hasRemaining())
           System.out.println(ibuffer.get());      //also has bulk operators
        

        【讨论】:

          【解决方案5】:

          也可以使用 BigInteger 进行转换:

           BigInteger.valueOf(integer).toByteArray();
          

          【讨论】:

          • 您在此过程中创建了一个不需要的中间字符串对象。
          【解决方案6】:

          我发现以下代码可能对您有所帮助,因为 Java 中的整数总是 4 个字节长。

          public static byte[] intToFourBytes(int i, boolean bigEndian) {  
              if (bigEndian) {  
                  byte[] data = new byte[4];  
                  data[3] = (byte) (i & 0xFF);  
                  data[2] = (byte) ((i >> 8) & 0xFF);  
                  data[1] = (byte) ((i >> 16) & 0xFF);  
                  data[0] = (byte) ((i >> 24) & 0xFF);  
                  return data;  
          
              } else {  
                  byte[] data = new byte[4];  
                  data[0] = (byte) (i & 0xFF);  
                  data[1] = (byte) ((i >> 8) & 0xFF);  
                  data[2] = (byte) ((i >> 16) & 0xFF);  
                  data[3] = (byte) ((i >> 24) & 0xFF);  
                  return data;  
              }  
          }  
          

          您可以在此处找到有关 bigEndian 参数的更多信息: http://en.wikipedia.org/wiki/Endianness

          【讨论】:

            【解决方案7】:

            您可以使用 DataOutputStream 将整数转换为 byte[],如下所示:

            ByteArrayOutputStream baos = new ByteArrayOutputStream ();
            DataOutputStream dos = new DataOutputStream (baos);
            dos.writeInt (i);
            byte[] data = baos.toByteArray();
            // do encryption
            

            然后再解密:

            byte[] decrypted = decrypt (data);
            ByteArrayInputStream bais = new ByteArrayInputStream (data);
            DataInputStream dis = new DataInputStream (bais);
            int j = dis.readInt();
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-08-22
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多