如何将IV(初始化矢量)添加到AES-256 ECB加密以创建AES-256 CBC模式?

时间:2018-02-18 18:44:02

标签: c encryption aes cbc-mode ecb

我使用一个简单的面向字节的AES-256库代码here,使用以下代码进行AES-256 ECB加密。

主:

#define DUMP(s, i, buf, sz)  {printf(s); \
for (i = 0; i < (sz);i++) \
printf("%02x ", buf[i]); \
printf("\n");}

int main (int argc, char *argv[])
{
aes256_context ctx; 
uint8_t key[32] = "39P8TXDMBCYF4C1NI1CDFJ1WL6P5TTKZ";
uint8_t buf[16] = "KUC7EWG6M2D1WW8F";
uint8_t i;

DUMP("txt: ", i, buf, sizeof(buf));
DUMP("key: ", i, key, sizeof(key));
printf("---\n");

aes256_init(&ctx, key);
aes256_encrypt_ecb(&ctx, buf);

DUMP("enc: ", i, buf, sizeof(buf));

aes256_init(&ctx, key);
aes256_decrypt_ecb(&ctx, buf);
DUMP("dec: ", i, buf, sizeof(buf));

aes256_done(&ctx);
return 0;
}

加密功能:

void aes256_encrypt_ecb(aes256_context *ctx, uint8_t *buf)
{
uint8_t i, rcon;
aes_addRoundKey_cpy(buf, ctx->enckey, ctx->key);
for(i = 1, rcon = 1; i < 14; ++i)
{
aes_subBytes(buf);
aes_shiftRows(buf);
aes_mixColumns(buf);
if( i & 1 ) aes_addRoundKey( buf, &ctx->key[16]);
else aes_expandEncKey(ctx->key, &rcon), aes_addRoundKey(buf, ctx->key);
}
aes_subBytes(buf);
aes_shiftRows(buf);
aes_expandEncKey(ctx->key, &rcon);
aes_addRoundKey(buf, ctx->key);
} /* aes256_encrypt */

我想在此程序中添加IV以创建AES-256 CBC模式。据我了解,IV实施如下:

  1. 使用IV。对第一个块进行异或。
  2. 使用上一个块的密文对所有后续块进行XOR。
  3. 我的问题是逻辑是什么样的?如何将其实现到我的代码中?

1 个答案:

答案 0 :(得分:0)

逻辑和解释可以在一些地方找到。例如:ECB vs CBCBlock cipher mode of operation

CBC =密码块链接是一种将块连接在一起的方法。

不仅仅是单独处理每个块,而是每个块都与加密的前一个块进行异或。这实际上意味着每个块都取决于前一个块的输出。

每个块与前一个块的密文进行异或,如引用文章中的图解所示。

实际上,一旦一个区块被ECB加密加密:

    public class QuestionCatalog extends AppCompatActivity {
        //TextView for displaying the selected subject
        TextView subjectLabel;
        //String to contain the subject selected by the user
        String selectedSubject;
        //Database Reference to questions
        DatabaseReference databaseReference;
        //ArrayList containing all the questions of a given subject
        ArrayList<Question> questions;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_question_catalog);

            //Get the data intent was launched with
             Bundle extras = getIntent().getExtras();
    //Determine which category the user wants to view questions for
    selectedSubject = extras.getString("Subject");
    //Initialize questions ArrayList
            questions = new ArrayList<Question>();
//Initialize DatabaseReference
            databaseReference = FirebaseDatabase.getInstance().getReference("questions");
            //Populate the array list with questions relevant to the subject
            populateQuestionArrayList();
            //Initialize the text view for displaying the subject
            subjectLabel = (TextView) findViewById(R.id.textViewSubjectLabel);
            //Try to get a Question object from the ArrayList for demo purposes
            questions.get(0);

    }

        private void populateQuestionArrayList() {
            databaseReference.child(selectedSubject).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot question : dataSnapshot.getChildren()){
                            Question q = question.getValue(Question.class);
                            questions.add(q);
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
        }
    }

,如

 Cipher((state_t*)buf, ctx->RoundKey);

通过XOR实现CBC,块上有IV,同一块上有ECB,沿着缓冲区中的块移动。

带IV的XOR示例:

void AES_ECB_encrypt(struct AES_ctx *ctx,const uint8_t* buf)
{
  // The next function call encrypts the PlainText with the Key using AES algorithm.
  Cipher((state_t*)buf, ctx->RoundKey);
}

使用XOR和IV和ECB的CBC的例子:

static void XorWithIv(uint8_t* buf, uint8_t* Iv)
{
  uint8_t i;
  for (i = 0; i < AES_BLOCKLEN; ++i) // The block in AES is always 128bit no matter the key size
  {
    buf[i] ^= Iv[i];
  }
}

上述实施来自tiny-AES,您可能希望对其进行研究并根据您的需求进行调整。我希望它有所帮助。