2015-06-20 40 views
0

所以我有一個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); 
} 
+2

請發佈您的代碼,將代碼從8位轉換爲16位。底線:你不是試圖以正確的方式解決問題;如果你真的需要[編碼如base64編碼],發送它,解碼並解密它,那麼採取你的數據,加密它,編碼它會更有意義 – Foon

+0

我不是說你可能不是正確的編碼和加密順序,但問題是轉換。無論哪種方式,在加密時,-1和/或127將出現,127將始終轉換爲-1,這對解密不利。 (我編輯了第一篇文章並把代碼) –

+0

首先,請注意(使用2的補碼)-1是0x11111111和127是0x01111111(即他們都有7低位設置爲1,唯一的區別是高/符號位)。另外,當我調用linear_2ulaw(-1)和linear_2ualaw(127)時,既不返回0 ...一個返回127,一個返回239.你在DSP上運行這個還是某個int是16位的東西? [做一個查找/替換int的簡稱,我得到127/215(和一個錯誤,prshortf不是一個有效的函數),或者這不是正在失去信息的函數? – Foon

回答

0

好像我弄清楚如何做到這一點。 -1和127從來都不是問題,因爲加密從不輸出兩個0的順序,儘管可能發生的情況很少。 問題是烏拉密碼(但我不知道爲什麼)。我切換到法律,現在它工作得很好。我有一個小背景噪音(有和沒有加密),但它是可以接受的。

1

AES加密旨在產生隨機輸出,因此您不喜歡的那些字節將總是出現在每個平均每個256字節的字節輸出中。你不能改變它,否則你不會使用AES。

你可以做的是轉換AES輸出,所以不需要的字節不會引起任何問題。通常的方式做到這一點是使用Base64,作爲@Foon建議:

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

你可以把中的Base64的字節流是絕對不包含-1或128所有的Base64的是在可打印的ASCII範圍。您只需記住在解密之前從Base64中檢索您的原始字節。

如果Base64不是解決方案,那麼還有其他可能性,但它們要複雜得多,而且必須手工編程。使用Base64,您可以導入庫方法。

+0

糾正我,如果我錯了,但AES將生成介於-128和+127(與ulaw格式相同)和base64從0-63生成的數字。它不會使更多的信息丟失?另外,因爲在接待處ais等待範圍-128-> 127所以我認爲我不能這樣做,所以,8bit ulaw能夠轉換16bit pcm,是不是搞砸了?它將如下所示:捕獲 - > 16位pcm - > 8位ulaw(讀取緩衝區) - > AES - > base64 - >傳輸 - >(讀取緩衝區)16位pcm(來自base64,這是不對的) - > base64 - > AES - > 16bit pcm - >播放 –

+0

Base64佔用更多空間。 3個字節的AES輸出(3 x 8 = 24位)映射到Base64中的4個6位數字(4 x 6 = 24位)。如果轉換正確完成,沒有信息丟失。 – rossum