Beispiel #1
0
        public void Test_02DiverseK0()
        {
            this.esKmsApi.Authentication("APP_001", "12345678");

            //
            secret_key_t key = new secret_key_t();
            key.key_label = "2ICH3F000010A";
            key.key_type = (int)KMS_KEY_TYPE_CODE.KMS_KEY_TYPE_KEY;
            key.slot = 0;
            //
            mechanism_param_t mechanism = new mechanism_param_t();
            data_blob_t data_in = new data_blob_t();
            data_blob_t data_out = new data_blob_t();
            result_t result = new result_t();
            byte[] out_data_buf = new byte[1024];
            //byte[] iv = this.byteWorker.Fill(16, 0x00);
            //this.hexConverter.Hex2Bytes("4B6E6F4BF5539AB5BFE5D679EA1ED16E");
            byte[] data = this.byteWorker.Fill(16, 0x00);
            //Encoding.ASCII.GetBytes( this.hexConverter.Bytes2Hex( this.byteWorker.Fill(16, 0x00) ) );
            //var hd1 = GCHandle.Alloc( iv, GCHandleType.Pinned);
            //規劃一塊Unmanaged的記憶體空間提供給Dll寫入並固定住記憶體位置,避免.NET整理記憶體空間時,把目前Address轉移到其他Address,造成Dll寫入或讀取時弄錯記憶體Address
            var hd1 = GCHandle.Alloc(data, GCHandleType.Pinned);//放置要給Dll取得的參數資料
            var hd2 = GCHandle.Alloc(out_data_buf, GCHandleType.Pinned);//放置Dll計算完後回傳的資料
            try
            {
                //取得記憶體位置
                key.div_param = hd1.AddrOfPinnedObject();//
                //設定加密的區塊模式
                mechanism.mechanism = EsKmsApi.CKM_AES_ECB;
                //mechanism.parameter = hd1.AddrOfPinnedObject();
                //mechanism.parameter_len = (uint)iv.Length;
                //
                data_in.name = null;
                data_in.type = (int)(KMS_DATA_TYPE_CODE.KMS_DATA_BLOB_TYPE_BINARY);//BLOB傳輸方式使用Binary
                data_in.value = hd1.AddrOfPinnedObject();//取得放input資料的記憶體Address
                data_in.value_len = data.Length;
                //設定密碼運算的結果存放處
                data_out.value = hd2.AddrOfPinnedObject();//取得放output資料的記憶體Address
                data_out.value_len = out_data_buf.Length;

                //密碼運算
                this.esKmsApi.Cipher(key, (uint)KMS_CIPHER_CODE.KMS_CIPHER_METHOD_ENCRYPT, mechanism, data_in, ref data_out, ref result);

                byte[] k0 = this.byteWorker.SubArray(out_data_buf, 0, data_out.value_len);

                Debug.WriteLine("K0:[" + this.hexConverter.Bytes2Hex(k0) + "]");
                Debug.WriteLine(String.Format("{0}:{1}:{2}:{3}:{4}", result.error_code, result.error_message, result.return_code, result.return_message, result.trans_no));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.StackTrace);
            }
            finally
            {
                hd1.Free();//釋放Unmanaged的記憶體空間
                hd2.Free();//釋放Unmanaged的記憶體空間
            }
        }
Beispiel #2
0
 public void Cipher(secret_key_t key, uint cipher_method, mechanism_param_t mechanism, data_blob_t dbin, ref data_blob_t dbout, ref result_t result)
 {
     uint r = 0;
     r = cipher(this.ctx, ref key, cipher_method, ref mechanism, ref dbin, ref dbout, ref result);
     if (r != KMS_NO_ERROR)
     {
         string errStr = String.Format("Cipher fail:[{0:X8}]", r);
         Debug.WriteLine(errStr);
         throw new Exception(errStr);
     }
     return;
 }
Beispiel #3
0
 public static extern uint cipher(uint ctx, ref secret_key_t key, uint cipher_method, ref mechanism_param_t mechanism, ref data_blob_t db_in, ref data_blob_t db_out, ref result_t result);