arXiv:2610.25133cs.CROct 2026

Manuscript Offprint

arXiv:2610.25133

Many Repairs, One Direction: Steering Code Models Toward Safer Cryptographic API Use

Tianhe Lu, Sakuna Harinda Jayasundara, Eric Spero, Ewan Tempero, Robert Biddle, Giovanni Russello

Abstract

Cryptographic API misuse in large language model-generated code can compromise software security. While many existing approaches improve code security by modifying model parameters or inputs, how representation changes associated with different cryptographic API repairs can support a shared intervention for safer code generation remains underexplored. To investigate this, we construct 48,000 matched program pairs across four programming languages and ten cryptographic API misuse categories. Each pair changes a specified API decision while preserving the programming task. Subtracting the misuse program's internal representation from its secure counterpart's gives a repair vector. Across five models, comparisons of mean repair vectors reveal stronger organization by misuse category than by language. These category-specific patterns also support a shared intervention. We introduce CrypSteer, which extracts a shared direction by normalizing repair differences and selecting a principal component aligned with repairs across misuse categories. With model weights fixed, adding one shared direction per model to its internal activations during generation improves aggregate target security rates by 5.28–15.00 percentage points over unsteered generation across languages and misuse categories. These findings connect the shared structure of cryptographic repair representations to a reusable intervention for safer code generation.

1 Introduction

Large language models (LLMs) assist developers with code generation, but they also introduce security vulnerabilities in generated programs (Pearce et al., 2025; Perry et al., 2023). Software applications rely on cryptographic application programming interfaces (APIs) to protect data, and their secure use requires appropriate algorithms, parameters, and randomness (Krüger et al., 2021). These choices are difficult for developers (Nadi et al., 2016), and LLM-generated programs also contain cryptographic API misuses (Mousavi et al., 2024).

Existing approaches improve the security of LLM-generated programs through fine-tuning (He and Vechev, 2023; He et al., 2024), prompt engineering (Tony et al., 2025a), retrieval-augmented generation (RAG) (Zhang et al., 2024), and feedback repair (Le et al., 2024). However, these gains leave open how models represent security-sensitive choices and whether the associated representation changes can guide generation. Activation steering modifies a model's internal activations during generation while keeping model weights and prompts fixed (Rimsky et al., 2024; Li et al., 2023; Zou et al., 2025; Turner et al., 2024; Hernandez et al., 2024). This allows us to study how representations associated with different API misuses can be used to influence the API choices in newly generated programs.

Recent studies show that activation steering directions constructed for individual Common Weakness Enumeration (CWE) categories can reduce vulnerabilities in generated programs (Yan and Yao, 2026; Sandoval et al., 2026; Yu et al., 2025). However, different uses of cryptographic APIs involve different security requirements. Replacing an unsuitable source of randomness, increasing a key length, and changing an algorithm configuration require different code changes (Mousavi et al., 2024; Krüger et al., 2021). We investigate how their representation changes vary across languages and misuse categories, and whether these different changes can support one reusable generation intervention.

In this study, we use matched secure and misuse program pairs that differ only in a specified cryptographic API decision for the same task. Repairs may alter an algorithm, a parameter value, or a source of randomness, providing distinct edits whose representation changes can be compared (Mousavi et al., 2024; Krüger et al., 2021). We construct 48,000 matched secure/misuse pro-

(a) From cryptographic API repairs to representations 48,000 pairs · 4 languages · 10 misuses

Figure

(b) Analyze shared repair structure

40 language-misuse vectors

(c) Reuse the shared direction on new tasks

One direction per model

Figure

Figure 1: Matched repairs support representation analysis and CrypSteer's shared generation intervention. Secure-minus-misuse program representations provide repair vectors for analysis and direction construction. The three-dimensional inset visualizes Llama's 40 repair directions for each language-misuse category combination, using uncentered principal component analysis (PCA). CrypSteer selects a repair-aligned principal component (security PC) and adds a scaled version to token activations during generation, reusing one direction per model across languages and misuse categories.

gram pairs across four programming languages, ten misuse categories, and four code structures. We use repair to describe the change from a misuse program to its secure counterpart, and use repair vector to describe the difference between their program representations. By matching the task and surrounding implementation, each vector can be connected to a specified repair. Varying language, misuse category, and program structure then allows us to compare these changes across controlled sources of variation.

Across five models, repair vectors are strongly grouped by misuse category, and their shared changes support safer code generation. We introduce CrypSteer, which normalizes repair vectors and selects a component aligned across misuse categories. Using CrypSteer in activation steering improves target security in generated programs. Comparisons show consistent gains from normalization, with the additional benefit of component selection.

Figure 1 summarizes our study. We first construct matched secure/misuse programs to support comparisons of repair vectors between them across languages, misuse categories, and code structures (Section 2). We then examine how these repair vectors are organized, construct a shared direction (Section 3), and test its effects on generation (Section 4). This paper has the following contributions.

• A controlled resource for studying repairs. A collection of 48,000 secure/misuse program pairs that contain matched API edits and structural variants enables comparisons of representation changes across programming languages, misuse categories, and program structures.

• Evidence connecting repair structure to shared control. Across five models, repairs differ by misuse category, but they support a shared generation intervention, including when the target misuse category is excluded from direction fitting.

• A shared steering method for safer generation. CrypSteer, which normalizes repair vectors and selects a principal component (PC) aligned across misuse categories, improves aggregate target security in generated programs across all five models using one shared direction per model.