【问题标题】:Can I force AES not to crypt in certain bytes?我可以强制 AES 不加密某些字节吗?
【发布时间】:2015-06-23 08:56:52
【问题描述】:

所以我有一个 AES 算法来加密我的字节数组,所以 -128 到 127。 问题是(长话短说)我不希望 AES 在加密后将我的任何字节转换为 -1 或 127(因为如果我转换为 16 位,由于某种原因它们都是 0),然后我无法正确解密.有什么办法吗?

我的应用程序运行如下:

声卡(16bit) -> 字节数组(8bit) -> AES.encr -> 加密字节数组(8bit) -> 套接字(8bit 传输) -> 加密字节数组(16bit) -> 数组加密字节(8bit) -> AES.decr -> 解密字节数组(8bit)

public static int linear2ulaw(int pcm_val){ // 2's complement (16-bit range)              
  int mask;
  int seg;
  //unsigned char uval;
  int uval;

  // Get the sign and the magnitude of the value.
  if (pcm_val<0){
    pcm_val=BIAS-pcm_val;
    mask=0x7F;
  }
  else{
     pcm_val+=BIAS;
     mask=0xFF;
  }
  // Convert the scaled magnitude to segment number.
  seg=search(pcm_val,seg_end);

  // Combine the sign, segment, quantization bits; and complement the code word.

  if (seg>=8) return (0x7F^mask); // out of range, return maximum value.
  else{
     uval=(seg<<4) | ((pcm_val>>(seg+3)) & 0xF);
     return (uval^mask);
  }
  }

static int search(int val,  int[] table){  
  for (int i=0; i<table.length; i++) 
    if (val<=table[i]) return i;
     return table.length;
}

 static final int SIGN_BIT=0x80;    // Sign bit for a A-law byte.
 static final int QUANT_MASK=0xf;   // Quantization field mask.
 static final int NSEGS=8;          // Number of A-law segments.
 static final int SEG_SHIFT=4;      // Left shift for segment number.
 static final int SEG_MASK=0x70;    // Segment field mask.
 public static final int BIAS=0x84;
 static final int[] seg_end={ 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF };

 public static int ulaw2linear(int u_val){
    int t;
    // Complement to obtain normal u-law value.
    u_val=~u_val;
    // Extract and bias the quantization bits. Then shift up by the segment number and subtract out the bias.
    t=((u_val&QUANT_MASK)<<3) + BIAS;
    //t<<=((unsigned)u_val&SEG_MASK)>>SEG_SHIFT;
    t<<=(u_val&SEG_MASK)>>SEG_SHIFT;

    return ((u_val&SIGN_BIT)!=0)? (BIAS-t) : (t-BIAS);
 }

【问题讨论】:

  • 请发布将代码从 8 位转换为 16 位的代码。底线:您没有尝试以正确的方式解决问题;如果你真的需要[像base64编码一样编码]、发送、取消编码然后解密,那么获取你的数据、对其进行加密、对其进行编码会更有意义
  • 我并不是说您可能对编码和加密的顺序不正确,但问题在于转换。因为无论哪种方式,在加密时都会出现 -1 和/或 127,并且 127 将始终转换为 -1,这对解密不利。 (我编辑了第一篇并放了代码)
  • 首先,请注意(使用 2 的补码)-1 是 0x11111111,127 是 0x01111111(即它们的 7 个低位都设置为 1,唯一的区别是高/符号位)。此外,当我调用 linear_2ulaw(-1) 和 linear_2ualaw(127) 时,两者都不返回 0... 一个返回 127,一个返回 239。您是在 DSP 上运行它还是在 int 为 16 位的东西上运行它? [对 int 进行查找/替换缩写,我得到 127 / 215(以及 prshortf 不是有效函数的错误)或者这不是丢失信息的函数?
  • 好吧,在我触摸它之前信息就丢失了,因为我通过套接字发送 8 位(出于速度原因,16 位是滞后的),我必须执行 ais = AudioSystem.getAudioInputStream(lineFormat, ais) at接收者,意思是 ais.read(buffer,0,buffer.length) 将返回 16 位的缓冲区,-1 和 127 的转换问题是 0x0 和 0x0。所以我发送 8 位缓冲区示例: buffer[1]= -1, buffer[2]=100, buffer[3]=127 我收到 buffer[1]=0, buffer[2]=0, buffer[3]= (-52),缓冲区[4]=(-2),缓冲区[5]=0,缓冲区[6]=0。原因是如果前两个 0,0 代表 -1 或 127,我无法做出任何改变。
  • 但不是 ais = AudioSystem.getAudioInputStream(lineFormat, ais) 是问题所在,因为我发布了 linear=ulaw2linear(-1) 和 linear=ulaw2linear(127) 的“外部转换”使得linear=0,它是一个 int,没有 DSP。

标签: java sockets encryption byte aes


【解决方案1】:

AES 加密旨在生成外观随机的输出,因此您不喜欢的那些字节将始终平均每 256 个字节出现在字节输出中。您无法更改它,否则您将不会使用 AES。

您可以做的是转换 AES 输出,这样不需要的字节就不会造成任何问题。执行此操作的通常方法是使用Base64,正如@Foon 建议的那样:

AES bytes -> to Base64 -> transmit -> from Base64 -> AES bytes.

您可以将 Base64 视为绝对不包含 -1 或 128 的字节流。所有 Base64 都在可打印的 ASCII 范围内。您只需要记住在解密之前从 Base64 中检索原始字节即可。

如果 Base64 不是解决方案,那么还有其他可能性,但它们要复杂得多,您必须手动编程。使用 Base64,您可以只导入库方法。

【讨论】:

  • 如果我错了,请纠正我,但 AES 会生成介于 -128 和 +127 之间的数字(与 ulaw 格式相同),并且 base64 从 0-63 生成。这不是让更多的信息丢失吗?另外,我不认为我可以这样做,因为在接待处 ais 等待范围 -128->127 所以,8bit ulaw,能够转换 16bit pcm,不是事情搞砸了吗?它将类似于:捕获 -> 16bit pcm -> 8bit ulaw(read buffer) -> AES -> base64 ->transmit -> (read buffer)16bit pcm(from base64,这是错误的) -> base64 -> AES -> 16 位 pcm -> 播放
  • Base64 占用更多空间。 AES 输出的 3 个字节(3 x 8 = 24 位)映射到 Base64 中的四个 6 位数字(4 x 6 = 24 位)。只要转换正确,就不会丢失信息。
【解决方案2】:

看来我知道该怎么做了。 -1 和 127 从来都不是问题,因为加密从不按顺序输出两个 0,尽管这种情况可能很少发生。 问题是 ulaw 编码(但我不知道为什么)。我切换到 alaw,现在它工作得很好。我的背景噪音很小(加密和不加密),但这是可以接受的。

【讨论】:

    猜你喜欢
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 2019-05-08
    • 1970-01-01
    相关资源
    最近更新 更多