[ prog / sol / mona ]

prog


SchemeBBS [part 2]

11 2020-06-22 13:31

Here is a compatibility experiment between mit-scheme encryption via medium-mcrypt >>10 and java's Cipher. First bbs.scm is encrypted to a file with medium-mcrypt:

$ mit-scheme --load test.scm
[...]
  Release 9.1.1     || Microcode 15.3 || Runtime 15.7 || SF 4.41
  LIAR/x86-64 4.118 || Edwin 3.116
;Loading "test.scm"... done

1 ]=> (mcrypt-available?)
;Loading "/usr/lib/x86_64-linux-gnu/mit-scheme/lib/prmcrypt.so"... done
;Value: #t
1 ]=> (define key (md5-string "secret key"))
;Loading "/usr/lib/x86_64-linux-gnu/mit-scheme/lib/prmhash.so"... done
;Value: key
1 ]=> (md5-sum->hexadecimal key)
;Value 13: "a7656fafe94dae72b1e1487670148412"
1 ]=> (define aes (medium-mcrypt "rijndael-128" "cbc" 16 16 padadd-pkcs7 paddel-pkcs7))
;Value: aes
1 ]=> (define enc (aes '(file "bbs.scm") '(file "bbs.scm.enc") key #t))
;Value: enc
1 ]=> enc
;Value 13: (file "bbs.scm.enc")
1 ]=> 

The same parameters are used in java to decrypt the file:

$ cat Test.java 
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Test {
   public static void transfer (InputStream is, OutputStream os) throws IOException {
      byte [] buf = new byte [65536];
      int got;

      while ((got = is.read (buf)) != -1) {
         os.write (buf, 0, got);
      }
   }
 
   static void work (String keyseed, String inpath, String outpath) throws GeneralSecurityException, IOException {
      MessageDigest md = MessageDigest.getInstance ("MD5");
      byte [] key = md.digest (keyseed.getBytes ());
      System.out.printf ("key %032x%n", new BigInteger (1, key));

      Cipher c = Cipher.getInstance ("AES/CBC/PKCS5Padding");
      SecretKeySpec sks = new SecretKeySpec (key, "AES");
      IvParameterSpec iv = new IvParameterSpec (new byte [16]);
      c.init (Cipher.DECRYPT_MODE, sks, iv);

      try (
         FileInputStream fis = new FileInputStream (inpath);
         FileOutputStream fos = new FileOutputStream (outpath);
         BufferedOutputStream bos = new BufferedOutputStream (fos);
         CipherOutputStream cos = new CipherOutputStream (bos, c);
      ) {
         transfer (fis, cos);
      }

      System.out.printf ("%s -> %s%n", inpath, outpath);
   }

   public static void main (String [] argv) {
      try {
         work ("secret key", "bbs.scm.enc", "bbs.scm.dec");
      } catch (GeneralSecurityException e) {
         e.printStackTrace ();
      } catch (IOException e) {
         e.printStackTrace ();
      }
   }
}

After decryption the original file has been recovered:

$ javac Test.java 
$ java Test
key a7656fafe94dae72b1e1487670148412
bbs.scm.enc -> bbs.scm.dec
$ cmp bbs.scm.dec bbs.scm
$ 
112


VIP:

do not edit these