所以我有一個AES算法來加密我的字節數組,所以-128到127. 問題是(長話短說)我不希望AES在加密後將任何字節轉換爲-1或127(因爲如果我轉換成16位,它們都是0),然後我無法正確解密。有沒有辦法做到這一點?我可以強制AES不加密在某些字節?
我的應用程序運行是這樣的:
聲卡(16位) - >的字節陣列(8位) - > AES.encr - >陣列加密字節(8位)的 - >插座(8位傳輸) - >加密的字節(16位)的陣列 - >加密的字節陣列(8位) - > AES.decr - >陣列解密字節(8位)的
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編碼],發送它,解碼並解密它,那麼採取你的數據,加密它,編碼它會更有意義 – Foon
我不是說你可能不是正確的編碼和加密順序,但問題是轉換。無論哪種方式,在加密時,-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不是一個有效的函數),或者這不是正在失去信息的函數? – Foon