# Using AI to perform Cryptanalysis

If given a large set of examples of cyphertext and corresponding plaintext, could AI be trained to decrypt a cyphertext as the examples provided?, and if so, are there any examples online demonstrating this?

For example, lets imagine out cypher is a simple 7-shift ceaser shift, and we have a list of thousands of examples like so

olssv,hello
ifl,bye
dvysk,world


Could a neural network be trained on these examples, and then shown a new cyphertext, not in the training set, such as "isbl" and correctly infer the result as "blue"?

• – forest Jul 26 at 9:08

Yes it could work for simple ciphers. Here's a quick example:

# dependencies
import numpy as np

# machine learning
from keras.models import Sequential
from keras.layers import Dense

# constants
BASE = 97
MAX = 26

# let's assign a = 1, z = 26, ciphertext x and decrypted value y
y = np.arange(0,MAX,1)
x = np.roll(y,-7)

model = Sequential()

history = model.fit(x, y, epochs = 500, verbose=0)

for x_test in [['o', 'l', 's', 's', 'v'], \
['i','f','l'], \
['d', 'v', 'y', 's', 'k']]:
x_test = list(map(lambda x: ord(x) - BASE, x_test))
# print (x_test)

pred = model.predict_classes(x_test)
print(list(map(lambda x: chr(x + BASE), pred)))


['h', 'e', 'l', 'l', 'o'] ['b', 'y', 'e'] ['w', 'o', 'r', 'l', 'd']

That depends on the encryption. But for all simple monoalphabetic substitutions the answer is yes. And to don't need a neutral net, but the most simple classifier works. You train it on the letters of the cipher-texts, with the cleartext-letters being the classes. To apply the decryption to an unknown text, just let it classify each letter of the cipher independently and then concatenate the results to get the cleartext.