⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmrijndael.frm

📁 Rjindeal加密算法
💻 FRM
📖 第 1 页 / 共 3 页
字号:
        Next
        FPrintF FileNumber, "\n"
    Next
    FPrintF FileNumber, "\nIntermediate Ciphertext Values (Encryptions)\n\n"
    FPrintF FileNumber, "PT="
    For i = 0 To 3
        FPrintF FileNumber, "%s", PadHexStr(Hex(PT(i)), 8)
    Next
    FPrintF FileNumber, "\n"
    For r = 1 To KeyInst.mNr
        KeyInst.cipherUpdateRounds PT, 4, CT, r
        FPrintF FileNumber, "CT%s=", r
        For i = 0 To 3
            FPrintF FileNumber, "%s", PadHexStr(Hex(CT(i)), 8)
        Next
        FPrintF FileNumber, "\n"
    Next
    '    /* decryption: */
    KeyInst.makeKey KeyLength, Decrypt, KeyMateria
    KeyInst.CipherInit ECB
    FPrintF FileNumber, "\nRound Subkey Values (Decryption)\n\n"
    For r = 0 To KeyInst.mNr
        FPrintF FileNumber, "RK%s=", r
        For i = 0 To 3
            w = KeyInst.rk((4 * r) + i)
            FPrintF FileNumber, "%s", PadHexStr(Hex(w), 8)
        Next
        FPrintF FileNumber, "\n"
    Next
    FPrintF FileNumber, "\nIntermediate Ciphertext Values (Decryption)\n\n"
    FPrintF FileNumber, "CT="
    For i = 0 To 3
        FPrintF FileNumber, "%s", PadHexStr(Hex(CT(i)), 8)
    Next
    FPrintF FileNumber, "\n"
    For r = 1 To KeyInst.mNr
        KeyInst.cipherUpdateRounds CT, 4, PT, r
        FPrintF FileNumber, "PT%s=", r
        For i = 0 To 3
            FPrintF FileNumber, "%s", PadHexStr(Hex(PT(i)), 8)
        Next
        FPrintF FileNumber, "\n"
    Next
    ' end of 256
    
    Close #FileNumber
#If TRACE_KAT_MCT Then
    Debug.Print "Done"
#End If
End Sub

' Original C-Source for MakeFIPSTestVectors()
'static void makeFIPSTestVectors(const char *fipsFile) {
'    int i, keyLength, r;
'    keyInstance keyInst;
'    cipherInstance cipherInst;
'    BYTE keyMaterial[320];
'    u8 pt[16], ct[16];
'    char format[64];
'    FILE *fp;
'
'#ifdef TRACE_KAT_MCT
'    printf("Generating FIPS test vectors...");
'#endif /* ?TRACE_KAT_MCT */
'
'    fp = fopen(fipsFile, "w");
'    fprintf(fp,
'        "\n"
'        "================================\n\n"
'        "FILENAME:  \"%s\"\n\n"
'        "FIPS Test Vectors\n",
'        fipsFile);
'
'    /* 128-bit key: 00010103...0e0f: */
'    keyLength = 128;
'    memset(keyMaterial, 0, sizeof (keyMaterial));
'    for (i = 0; i < keyLength/8; i++) {
'        sprintf(&keyMaterial[2*i], "%02X", i);
'    }
'
'    fprintf(fp, "\n================================\n\n");
'    fprintf(fp, "KEYSIZE=128\n\n");
'    fprintf(fp, "KEY=%s\n\n", keyMaterial);
'
'    /* plaintext is always 00112233...eeff: */
'    for (i = 0; i < 16; i++) {
'        pt[i] = (i << 4) | i;
'    }
'
'    /* encryption: */
'    makeKey(&keyInst, DIR_ENCRYPT, keyLength, keyMaterial);
'    cipherInit(&cipherInst, MODE_ECB, NULL);
'    fprintf(fp, "Round Subkey Values (Encryption)\n\n");
'    for (r = 0; r <= keyInst.Nr; r++) {
'        fprintf(fp, "RK%d=", r);
'        for (i = 0; i < 4; i++) {
'            u32 w = keyInst.rk[4*r + i];
'            fprintf(fp, "%02X%02X%02X%02X", w >> 24, (w >> 16) & 0xff, (w >> 8) & 0xff, w & 0xff);
'        }
'        fprintf(fp, "\n");
'    }
'    fprintf(fp, "\nIntermediate Ciphertext Values (Encryption)\n\n");
'    blockPrint(fp, pt, "PT");
'    for (i = 1; i < keyInst.Nr; i++) {
'        cipherUpdateRounds(&cipherInst, &keyInst, pt, 16, ct, i);
'        sprintf(format, "CT%d", i);
'        blockPrint(fp, ct, format);
'    }
'    cipherUpdateRounds(&cipherInst, &keyInst, pt, 16, ct, keyInst.Nr);
'    blockPrint(fp, ct, "CT");
'
'    /* decryption: */
'    makeKey(&keyInst, DIR_DECRYPT, keyLength, keyMaterial);
'    cipherInit(&cipherInst, MODE_ECB, NULL);
'    fprintf(fp, "\nRound Subkey Values (Decryption)\n\n");
'    for (r = 0; r <= keyInst.Nr; r++) {
'        fprintf(fp, "RK%d=", r);
'        for (i = 0; i < 4; i++) {
'            u32 w = keyInst.rk[4*r + i];
'            fprintf(fp, "%02X%02X%02X%02X", w >> 24, (w >> 16) & 0xff, (w >> 8) & 0xff, w & 0xff);
'        }
'        fprintf(fp, "\n");
'    }
'    fprintf(fp, "\nIntermediate Ciphertext Values (Decryption)\n\n");
'    blockPrint(fp, ct, "CT");
'    for (i = 1; i < keyInst.Nr; i++) {
'        cipherUpdateRounds(&cipherInst, &keyInst, ct, 16, pt, i);
'        sprintf(format, "PT%d", i);
'        blockPrint(fp, pt, format);
'    }
'    cipherUpdateRounds(&cipherInst, &keyInst, ct, 16, pt, keyInst.Nr);
'    blockPrint(fp, pt, "PT");
'
'    /* 192-bit key: 00010103...1617: */
'    keyLength = 192;
'    memset(keyMaterial, 0, sizeof (keyMaterial));
'    for (i = 0; i < keyLength/8; i++) {
'        sprintf(&keyMaterial[2*i], "%02X", i);
'    }
'
'    fprintf(fp, "\n================================\n\n");
'    fprintf(fp, "KEYSIZE=192\n\n");
'    fprintf(fp, "KEY=%s\n\n", keyMaterial);
'
'    /* plaintext is always 00112233...eeff: */
'    for (i = 0; i < 16; i++) {
'        pt[i] = (i << 4) | i;
'    }
'
'    /* encryption: */
'    makeKey(&keyInst, DIR_ENCRYPT, keyLength, keyMaterial);
'    cipherInit(&cipherInst, MODE_ECB, NULL);
'    fprintf(fp, "\nRound Subkey Values (Encryption)\n\n");
'    for (r = 0; r <= keyInst.Nr; r++) {
'        fprintf(fp, "RK%d=", r);
'        for (i = 0; i < 4; i++) {
'            u32 w = keyInst.rk[4*r + i];
'            fprintf(fp, "%02X%02X%02X%02X", w >> 24, (w >> 16) & 0xff, (w >> 8) & 0xff, w & 0xff);
'        }
'        fprintf(fp, "\n");
'    }
'    fprintf(fp, "\nIntermediate Ciphertext Values (Encryption)\n\n");
'    blockPrint(fp, pt, "PT");
'    for (i = 1; i < keyInst.Nr; i++) {
'        cipherUpdateRounds(&cipherInst, &keyInst, pt, 16, ct, i);
'        sprintf(format, "CT%d", i);
'        blockPrint(fp, ct, format);
'    }
'    cipherUpdateRounds(&cipherInst, &keyInst, pt, 16, ct, keyInst.Nr);
'    blockPrint(fp, ct, "CT");
'
'    /* decryption: */
'    makeKey(&keyInst, DIR_DECRYPT, keyLength, keyMaterial);
'    cipherInit(&cipherInst, MODE_ECB, NULL);
'    fprintf(fp, "\nRound Subkey Values (Decryption)\n\n");
'    for (r = 0; r <= keyInst.Nr; r++) {
'        fprintf(fp, "RK%d=", r);
'        for (i = 0; i < 4; i++) {
'            u32 w = keyInst.rk[4*r + i];
'            fprintf(fp, "%02X%02X%02X%02X", w >> 24, (w >> 16) & 0xff, (w >> 8) & 0xff, w & 0xff);
'        }
'        fprintf(fp, "\n");
'    }
'    fprintf(fp, "\nIntermediate Ciphertext Values (Decryption)\n\n");
'    blockPrint(fp, ct, "CT");
'    for(i = 1; i < keyInst.Nr; i++) {
'        cipherUpdateRounds(&cipherInst, &keyInst, ct, 16, pt, i);
'        sprintf(format, "PT%d", i);
'        blockPrint(fp, pt, format);
'    }
'    cipherUpdateRounds(&cipherInst, &keyInst, ct, 16, pt, keyInst.Nr);
'    blockPrint(fp, pt, "PT");
'
'    /* 256-bit key: 00010103...1e1f: */
'    keyLength = 256;
'    memset(keyMaterial, 0, sizeof (keyMaterial));
'    for (i = 0; i < keyLength/8; i++) {
'        sprintf(&keyMaterial[2*i], "%02X", i);
'    }
'
'    fprintf(fp, "\n================================\n\n");
'    fprintf(fp, "KEYSIZE=256\n\n");
'    fprintf(fp, "KEY=%s\n\n", keyMaterial);
'
'    /* plaintext is always 00112233...eeff: */
'    for (i = 0; i < 16; i++) {
'        pt[i] = (i << 4) | i;
'    }
'
'    /* encryption: */
'    makeKey(&keyInst, DIR_ENCRYPT, keyLength, keyMaterial);
'    cipherInit(&cipherInst, MODE_ECB, NULL);
'    fprintf(fp, "\nRound Subkey Values (Encryption)\n\n");
'    for (r = 0; r <= keyInst.Nr; r++) {
'        fprintf(fp, "RK%d=", r);
'        for (i = 0; i < 4; i++) {
'            u32 w = keyInst.rk[4*r + i];
'            fprintf(fp, "%02X%02X%02X%02X", w >> 24, (w >> 16) & 0xff, (w >> 8) & 0xff, w & 0xff);
'        }
'        fprintf(fp, "\n");
'    }
'    fprintf(fp, "\nIntermediate Ciphertext Values (Encryption)\n\n");
'    blockPrint(fp, pt, "PT");
'    for(i = 1; i < keyInst.Nr; i++) {
'        cipherUpdateRounds(&cipherInst, &keyInst, pt, 16, ct, i);
'        sprintf(format, "CT%d", i);
'        blockPrint(fp, ct, format);
'    }
'    cipherUpdateRounds(&cipherInst, &keyInst, pt, 16, ct, keyInst.Nr);
'    blockPrint(fp, ct, "CT");
'
'    /* decryption: */
'    makeKey(&keyInst, DIR_DECRYPT, keyLength, keyMaterial);
'    cipherInit(&cipherInst, MODE_ECB, NULL);
'    fprintf(fp, "\nRound Subkey Values (Decryption)\n\n");
'    for (r = 0; r <= keyInst.Nr; r++) {
'        fprintf(fp, "RK%d=", r);
'        for (i = 0; i < 4; i++) {
'            u32 w = keyInst.rk[4*r + i];
'            fprintf(fp, "%02X%02X%02X%02X", w >> 24, (w >> 16) & 0xff, (w >> 8) & 0xff, w & 0xff);
'        }
'        fprintf(fp, "\n");
'    }
'    fprintf(fp, "\nIntermediate Ciphertext Values (Decryption)\n\n");
'    blockPrint(fp, ct, "CT");
'    for(i = 1; i < keyInst.Nr; i++) {
'        cipherUpdateRounds(&cipherInst, &keyInst, ct, 16, pt, i);
'        sprintf(format, "PT%d", i);
'        blockPrint(fp, pt, format);
'    }
'    cipherUpdateRounds(&cipherInst, &keyInst, ct, 16, pt, keyInst.Nr);
'    blockPrint(fp, pt, "PT");
'
'    fprintf(fp, "\n");
'    fclose(fp);
'#ifdef TRACE_KAT_MCT
'    printf(" done.\n");
'#endif /* ?TRACE_KAT_MCT */
'}

Private Sub Command2_Click()
    MakeKats "ecb_vk2.txt", "ecb_vt2.txt", "ecb_tbl2.txt", "ecb_iv2.txt"
End Sub

Public Sub rijndaelVKKAT(FileNumber As Long, KeyLength As Long)
Dim i As Long, j As Long, r As Long
Dim Block(4) As Long
Dim KeyMaterial As String
Dim byteVal As Byte
Dim KeyInst As rijndaelKeyInstance
#If TRACE_KAT_MCT Then
    PrintF "Executing Variable-Key KAT(Key %s): ", KeyLength
#End If
    byteVal = 8
    FPrintF FileNumber, "\n============\n\nKEYSIZE=%s\n\n", KeyLength
    FPrintF FileNumber, "PT="
    For i = 0 To 3
        Block(i) = Block(0)
        FPrintF FileNumber, "%s", PadHexStr(Hex(Block(i)), 8)
    Next
    FPrintF FileNumber, "\n"
    Set KeyInst = New rijndaelKeyInstance
    KeyMaterial = RepeatChar("0", KeyLength \ 4)
    For i = 0 To KeyLength - 1
        KeyMaterial = RepeatChar("0", i \ 4) & Hex(byteVal) & RepeatChar("0", (KeyLength \ 4) - (i \ 4) - 1)
        r = KeyInst.makeKey(KeyLength, Encrypt, KeyMaterial)
        If r <> True Then
            PrintF "makeKey error %s\n", r
            End
        End If
        FPrintF FileNumber, "\nI=%s\n", i + 1
        FPrintF FileNumber, "KEY=%s\n", KeyMaterial
        Block(0) = 0: Block(1) = 0: Block(2) = 0: Block(3) = 0
        r = KeyInst.CipherInit(ECB)
        If r <> 0 Then
            PrintF "cipherInit error %s\n", r
            End
        End If
        r = KeyInst.blockEncrypt(Block, 128, Block)
        If r <> 128 Then
            PrintF "blockEncrypt error %s\n", r
            End
        End If
        FPrintF FileNumber, "CT="
        For j = 0 To 3
            FPrintF FileNumber, "%s", PadHexStr(Hex(Block(j)), 8)
        Next
        FPrintF FileNumber, "\n"
        '        /* now check decryption: */
        KeyInst.makeKey KeyLength, Decrypt, KeyMaterial
        KeyInst.BlockDecrypt Block, 128, Block
        For j = 0 To 3
            If Block(j) <> 0 Then
                PrintF "Assert!  Encrypt/Decrypt Failed! %s\n", j
                End
            End If
        Next
        '        /* undo changes for the next iteration: */
        KeyMaterial = ""
        Select Case byteVal
            Case 8
                byteVal = 4
            Case 4
                byteVal = 2
            Case 2
                byteVal = 1
            Case 1
                byteVal = 8
        End Select
    Next
#If TRACE_KAT_MCT Then
    Debug.Print "Done"
#End If
End Sub

Private Sub rijndaelVTKAT(FileNumber As Long, KeyLength As Long)
Dim i As Long, j As Long
Dim Block(4) As Long
Dim KeyMaterial As String
Dim KeyInst As rijndaelKeyInstance
Dim tmpStr As String
#If TRACE_KAT_MCT Then
    PrintF "Executing Variable-Text KAT (Key %s): ", KeyLength
#End If
    FPrintF FileNumber, "\n===========\n\nKEYSIZE=%s\n\n", KeyLength
    KeyMaterial = RepeatChar("0", KeyLength / 4)
    Set KeyInst = New rijndaelKeyInstance
    KeyInst.makeKey KeyLength, Encrypt, KeyMaterial
    FPrintF FileNumber, "KEY=%s\n", KeyMaterial
    For i = 0 To 127
        Block(0) = 0: Block(1) = 0: Block(2) = 0: Block(3) = 0
        ' I hate clever C programmers sometimes... i.e.
        '        block[i/8] |= 1 << (7 - i%8); /* set only the i-th bit of the i-th test block */
        Block(i \ 32) = HexStrToLong(RepeatChar("0", ((i Mod 32) \ 4)) & (2 ^ (3 - (i Mod 4))) & RepeatChar("0", 7 - ((i Mod 32) \ 4)))
        ' Revenge is sweet.  (Don't ask me to explain this.  I won't know by the time I look at it again!).  It works ok?
        ' Who says only C programmers can write incomprehensible code eh?
        FPrintF FileNumber, "\nI=%s\n", i + 1
        FPrintF FileNumber, "PT="
        For j = 0 To 3
            FPrintF FileNumber, "%s", PadHexStr(Hex(Block(j)), 8)
        Next
        FPrintF FileNumber, "\n"
        KeyInst.CipherInit ECB
        KeyInst.blockEncrypt Block, 128, Block
        FPrintF FileNumber, "CT="
        For j = 0 To 3
            FPrintF FileNumber, "%s", PadHexStr(Hex(Block(j)), 8)
        Next
        FPrintF FileNumber, "\n"
    Next
#If TRACE_KAT_MCT Then
    Debug.Print "Done"
#End If
End Sub

Private Sub rijndaelTKAT(FileNumber As Long, KeyLength As Long, FileNumber2 As Long)
Dim i As Long, j As Long
Dim s As Long
Dim Block(4) As Long
Dim Block2(4) As Long
Dim KeyMaterial As String
Dim KeyInst As rijndaelKeyInstance

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -