0

I'm trying to recreate an (old) C# program in something that will run on Linux. So far I'm struggling to find an implementation that's configurable enough to make it happen. These are the configuration options:

Padding=ISO10126
CipherMode=CBC
KeySize=256
BlockSize=256
IV=(IV goes here)
Key=(Key goes here)

It seems OpenSSL and other implementations make assumptions about where the IV is stored and the padding.

For reference on the padding, look here.

Ideally it would be a command line utility.

migrated from crypto.stackexchange.com Jun 20 '17 at 23:43

This question came from our site for software developers, mathematicians and others interested in cryptography.

  • Well, using the same IV for different messages encrypted with the same key has security issues; are you sure that's what you want to do? – poncho Jun 20 '17 at 20:24
  • @galvatron Perhaps I need to make this more clear. I just need the ability to specify the IV so I can unencrypt it. Many implementations make the assumption that it's hidden in the key or something. Every entry does have its own IV. – JustGage Jun 20 '17 at 20:57
  • You could always leave it in C# and run it using Mono, which has a RijndaelManaged implementation. – Xander Jun 20 '17 at 21:09
  • Interesting @Xander I'll have to look into that – JustGage Jun 20 '17 at 21:14
2

This is rather specific so I don't think there is a ready made solution. With the right libraries it is however simple to create one.

package nl.owlstead.crypto;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.bouncycastle.crypto.engines.RijndaelEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.ISO10126d2Padding;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.util.encoders.Hex;

public class RijndaelStrangeCLI {

    private static final int RIJNDAEL_256 = 256;
    private static final boolean FOR_ENCRYPTION = true;
    private static final boolean FOR_DECRYPTION = false;
    private static final int BUFFER_SIZE = 1_024;
    private static final int EOS = -1;

    public static void main(String[] args) throws Exception {
        int argOff = 0;

        boolean encryptOrDecrypt;
        String choice = args[argOff++];
        if (choice.matches("(?i)-e")) {
            encryptOrDecrypt = FOR_ENCRYPTION;
        } else if (choice.matches("(?i)-d")) {
            encryptOrDecrypt = FOR_DECRYPTION;
        } else {
            throw new IllegalArgumentException("Try either -e or -d");
        }

        byte[] keyData = Hex.decode(args[argOff++]);
        byte[] ivData = Hex.decode(args[argOff++]);
        KeyParameter key = new KeyParameter(keyData);
        ParametersWithIV keyAndIV = new ParametersWithIV(key, ivData);

        RijndaelEngine rijndael = new RijndaelEngine(RIJNDAEL_256);
        ISO10126d2Padding almostPKCS7Padding = new ISO10126d2Padding();
        CBCBlockCipher cbcMode = new CBCBlockCipher(rijndael);
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(cbcMode,
                almostPKCS7Padding);
        cipher.init(encryptOrDecrypt, keyAndIV);

        byte[] inBuf = new byte[BUFFER_SIZE];
        byte[] outBuf = new byte[cipher.getOutputSize(BUFFER_SIZE)];
        try (FileInputStream fis = new FileInputStream(args[argOff++]);
                FileOutputStream fos = new FileOutputStream(args[argOff++])) {
            while (true) {
                int read = fis.read(inBuf);
                if (read == EOS) {
                    break;
                }
                int outLen = cipher.processBytes(inBuf, 0, read, outBuf, 0);
                fos.write(outBuf, 0, outLen);
            }
            int last = cipher.doFinal(outBuf, 0);
            fos.write(outBuf, 0, last);
        }
    }
}

Usage:

Compilation: only requires Java, say Java 8 and the Bouncy Castle provider or lightweight API.

Running:

java nl.owlstead.crypto.RijndaelStrangeCLI <"-e" or "-d"> <key in hex> <IV in hex> <input filename> <output filename>
  • If it didn't exist it exists now. – Maarten Bodewes Jun 20 '17 at 23:54
  • Thanks! This really helped me get it to work. – JustGage Jun 22 '17 at 20:23
0

BouncyCastle can do Rijdael in Java and C#.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.