3

Java で記述されたサーバーと通信する単純な nodejs アプリを作成するには、nodejs に次の機能を実装する必要があります。

public class Crypto {
  Cipher decipher;

  byte[] salt = {
      (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
      (byte) 0x0A, (byte) 0x0B, (byte) 0x0C, (byte) 0x0D
  };
  int iterationCount = 10;

  public Crypto(String pass) {
    try {
      KeySpec keySpec = new PBEKeySpec(pass.toCharArray(), salt, iterationCount);

      SecretKey key = SecretKeyFactory.getInstance(
          "PBEWithMD5AndTripleDES").generateSecret(keySpec);

      ecipher = Cipher.getInstance("PBEWithMD5AndTripleDES/CBC/PKCS5Padding");

      AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

      decipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

    } catch (Exception ex) {
    }
  }
}

nodejsのモジュールを使用していますcrypto

var crypto = require('crypto'),
      pass = new Buffer(wek),
      salt = new Buffer([0x01, 0x02, 0x03, 0x04, 0x0A, 0x0B, 0x0C, 0x0D])
      password = 'mySecretPassword'
      key = crypto.pbkdf2(pass, salt, 10, 256)
      cipher, 
      encrypted;

cipher = crypto.createCipher('des-ede-cbc', key);
encrypted = cipher.update(new Buffer('the very secred information'));

decipher暗号化された情報をサーバーに送信した後、上記の Java コード サンプルにリストされているように、オブジェクトを使用してメッセージを復号化できません。md5一番の問題はパーツだと思います。crypto nodejsモジュールでそれを実装する方法がわかりません。この問題を解決する方法を知っている人はいますか? または、それを達成するための他のモジュールまたはライブラリはありますか?

編集: nodejs の別のモジュールを試しました:node-forge

forge = require('node-forge')

var numIterations = 10,
      keyLength = 24,
      password = forge.util.createBuffer('mySecretPassword'),
      salt = new forge.util.ByteBuffer(new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x0A, 0x0B, 0x0C, 0x0D])),
      derivedKey = forge.pkcs5.pbkdf2(password, salt.getBytes(), numIterations, keyLength, forge.md.md5.create())
      iv = {}; // TODO... ???

var cipher = forge.des.createEncryptionCipher(derivedKey);
cipher.start(iv);
cipher.update('the very secred information');
cipher.finish();
var encrypted = cipher.output;

しかし、いくつかの問題/質問があります。

  • JavaScript で正しいアルゴリズムを使用していますか?
  • 計算はsaltJava の実装と一致していますか?
  • keyLengthJava実装で使用されているものをどのように判断できますか?
  • initialization vectorJava実装でどのように生成されますか? 最後のコード サンプルでは、​​ onnode-forgeを指定する必要があります。Javaコードでは、これがどのように行われるかわかりません。私の意見では、クライアントとサーバーで同じでなければなりませんか、それとも間違っていますか?ivcipher.start(iv)iv
4

2 に答える 2