> For the complete documentation index, see [llms.txt](https://docs.hex-rays.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hex-rays.com/developer/idc/idc-examples/analyzing-encrypted-code.md).

# Analyzing encrypted code

This small tutorial demonstrates how to use IDC to decrypt part of a program at analysis time. The sample file is a portion of the Ripper virus.

### 1st step

The binary image of the virus is loaded into IDA and analysis is started at the entry point

![](/files/hHXrW4p5OiAHq9mTFsld)

Obviously, the bytes right after the call don’t make sense, but the call gives us a clue: it is a decryption routine.

![](/files/FJ7dwgnCCqjDs0KGiyCV)

### 2nd step

We create a small IDC program that mimicks the decryption routine.

```
static decrypt(from, size, key ) {
  auto i, x;           // we define the variables
  for ( i=0; i < size; i=i+1 ) { 
    x = Byte(from);    // fetch the byte
    x = (x^key);       // decrypt it
    PatchByte(from,x); // put it back
    from = from + 1;   // next byte
  } 
}
            
```

Save it on disk and press F2 to load it into IDA's interpreter.

![](/files/JKv5WIaP0PG2x1elu0QU)

### 3rd step

Then, we press shift-F2 to call it with the appropriate values. Please note the linear address used for the starting point. Pressing OK executes the statement.

![](/files/VP5UJAqWvjjPRFbHMaBi)

The bytes are now decrypted

![](/files/b0E2ASMLNV08zaYUij6H)

### 4th step

We move the cursor to offset 0x50 and press C to inform IDA that there is now code at that location.

![](/files/rUsDP7OBPKDdqPHS57Nb)

And the code to allocate memory for the virus appears, along with a rather impolite message... The analysis may now resume.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.hex-rays.com/developer/idc/idc-examples/analyzing-encrypted-code.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
