Implementation of Kamasutra cipher This is exercise 3.1.31. from the book Computer Science An Interdisciplinary Approach by Sedgewick &amp; Wayne:

Write a filter KamasutraCipher that takes two strings as command-line
argument (the key strings), then reads strings (separated by
whitespace) from standard input, substitutes for each letter as
specified by the key strings, and prints the result to standard
output. This operation is the basis for one of the earliest known
cryptographic systems. The condition on the key strings is that they
must be of equal length and that any letter in standard input must
appear in exactly one of them. For example, if the two keys are
THEQUICKBROWN and FXJMPSVLAZYDG, then we make the table
T H E Q U I C K B R O W N
F X J M P S V L A Z Y D G
which tells us that we should substitute F for T, T for F, H for X, X
for H, and so forth when filtering standard input to standard output.
The message is encoded by replacing each letter with its pair. For
example, the message MEET AT ELEVEN is encoded as QJJF BF JKJCJG. The
person receiving the message can use the same keys to get the message
back.

Here is my program:
public class KamasutraCipher 
{
    public static void encrypt(String s, String t)
    {
        int m = s.length();
        int n = t.length();
        if (m != n) 
        {
            throw new RuntimeException(&quot;The key lengths must be equal&quot;);
        }
        while (!StdIn.isEmpty())
        {
            String word = StdIn.readString();
            int wordLength = word.length();
            for (int i = 0; i &lt; wordLength; i++)
            {
                for (int j = 0; j &lt; m; j++)
                {
                    if (String.valueOf(word.charAt(i)).equals(String.valueOf(s.charAt(j))))
                    {
                        String temp = word;
                        word = temp.substring(0,i) + String.valueOf(t.charAt(j)) + temp.substring(i+1);
                    }
                    else if (String.valueOf(word.charAt(i)).equals(String.valueOf(t.charAt(j))))
                    {
                        String temp = word;
                        word = temp.substring(0,i) + String.valueOf(s.charAt(j)) + temp.substring(i+1);
                    }
                }
            }
            System.out.print(word + &quot; &quot;);
        }
    }
    public static void main(String[] args)
    {
        encrypt(args[0], args[1]);
    }
}

StdIn is a simple API written by the authors of the book. I checked my program and it works.
Is there any way that I can improve my program?
Thanks for your attention.
The implementation looks good, I just have a few suggestions.
Single-responsibility principle
The method encrypt seems to have a lot of responsibility:

Reads the input from the user
Encrypts the input
Outputs the result to the console

One definition of SRP is &quot;A class should have only one reason to change&quot;. But there are many reasons for KamasutraCipher to change:

The input may come from System.in, file, database, etc.
The library StdIn changes.
The output needs to go to a file, etc.
The output needs to be formatted nicely for the user
etc..

The only responsibility of KamasutraCipher should be to encrypt (or decrypt) a string and return the result.
The interface can be refactored from this:
public class KamasutraCipher {
    public static void encrypt(String s, String t)
}

To:
public class KamasutraCipher {
    public KamasutraCipher(String key1, String key2)
    public String encrypt(String s)
}

Now the only reason for KamasutraCipher to change is for optimizations or if the Kamasutra algorithm changes, but the latter is not going to happen soon.
All the logic for requesting the input and producing the output is pushed to the main.
Strings are immutable
In Java strings are immutable objects, and any modification to a string creates a new string. Therefore this part:
String temp = word;
word = temp.substring(0,i) + String.valueOf(t.charAt(j)) + temp.substring(i+1);

Can be changed to:
word = word.substring(0,i) + t.charAt(j) + word.substring(i+1);

Optimization
The complexity of the method encrypt is O(m*n) where m is the length of the input string and n is the length of the key. (ignoring the methods of String and the while loop).
A more efficient way would be to use a map to store the string keys. For example, given the string keys ABC and FGH, the map would contain:

A -&gt; F
B -&gt; G
C -&gt; H
F -&gt; A
G -&gt; B
H -&gt; C

The method encrypt then becomes a simple lookup on the map, reducing the complexity  to O(m):
public String encrypt(String s) {
    StringBuilder sb = new StringBuilder(s.length());
    for (int i = 0; i &lt; s.length(); i++) {
        Character c = s.charAt(i);
        sb.append(keyMap.get(c));
    }
    return sb.toString();
}

SpringBuilder lets us save some memory by creating the result string more efficiently. keyMap is created in the constructor because the keys don't change after initialization.
Input validation

any letter in standard input must appear in exactly one of them (keys)

As mentioned by others this is a requirement that needs to be handled, possibly in the method encrypt.
For the exceptions you can use IllegalArgumentException instead of RuntimeException.
Refactored code
public class KamasutraCipher {
    
    private final Map&lt;Character,Character&gt; keyMap;
    
    public KamasutraCipher(String key1, String key2) {
        if (key1.length() != key2.length()) {
            throw new IllegalArgumentException(&quot;The key lengths must be equal&quot;);
        }
        keyMap = new HashMap&lt;&gt;();
        for (int i = 0; i &lt; key1.length(); i++) {
            keyMap.put(key1.charAt(i), key2.charAt(i));
            keyMap.put(key2.charAt(i), key1.charAt(i));
        }
    }
    
    public String encrypt(String s) {
        StringBuilder sb = new StringBuilder(s.length());
        for (int i = 0; i &lt; s.length(); i++) {
            Character c = s.charAt(i);
            if(!keyMap.containsKey(c)) {
                throw new IllegalArgumentException(String.format(&quot;'%c' is not in the keys&quot;, c));
            }
            sb.append(keyMap.get(c));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        KamasutraCipher cipher = new KamasutraCipher(args[0], args[1]);
        while (!StdIn.isEmpty()) {
            String input = StdIn.readString();
            System.out.println(cipher.encrypt(input));
        }
    }
}