使用AES 256文件解密

时间:2019-10-19 09:04:26

标签: android openssl

我遵循此主题来加密计算机上的文件:

https://gist.github.com/dreikanter/c7e85598664901afae03fedff308736b

所以我执行这些命令:

openssl genrsa -aes256 -out private.pem 8912
openssl rsa -in private.pem -pubout -out public.pem


openssl req -x509 -new -days 100000 -key private.pem -out certificate.pem



ENCRYPTION------------------------------ :
  openssl smime -encrypt -binary -aes-256-cbc -in config_auto.json -out config_auto.dat -outform   DER certificate.pem

然后我实现了这段代码,但是它不起作用:

公共字节[]解密RSA(字节[]数据)抛出NoSuchAlgorithmException,NoSuchPaddingException,             InvalidKeyException,IllegalBlockSizeException,BadPaddingException,ClassNotFoundException,InvalidKeySpecException {

    BigInteger m = new BigInteger(getKeyParams()[0],16);
    BigInteger e = new BigInteger(getKeyParams()[1],16);

    KeyFactory keyFactory2 = null;
    RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(m, e);
    RSAPrivateKey privatekey = null;

    try {
    keyFactory2 = KeyFactory.getInstance("RSA");


        privatekey = (RSAPrivateKey) keyFactory2.generatePrivate(privateKeySpec);


    Cipher c2 = Cipher.getInstance("AES/CBC/PKCS5Padding")); 
    c2.init(Cipher.DECRYPT_MODE,privatekey); 

    byte[] decryptedData = c2.doFinal(data);

    return decryptedData;
    } catch (IllegalBlockSizeException | BadPaddingException | InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException err) {
        err.printStackTrace();
    }
    return null;
}

我的文件.cpp:

  extern "C"
  JNIEXPORT jobjectArray JNICALL
  Java_mypackage_JNIHelper_getKeyParams(JNIEnv *env, jobject instance) {
  jobjectArray valueArray = (jobjectArray) env->NewObjectArray(2, env->FindClass("java/lang/String"), 0);
  const char *hash[2];
  //MODULUS
  hash[0] =   "00b55984ae86b1ff2276802ea8eca62a68848c3cb7606ae2143ba652c3b9312fc2733f4fdb1e338ceb38cb9cecc6fcdd312d5661bba359c152f1ef5feab2db3b70f145dc29803242dabf66";

   //EXPONENT

    hash[1] = "7c1df677546cc2879dbf0130503731d332d4a0418955ddc1a749a05fa0820f83f048a6a8fb34a0ffe47146c7af31255a21037c04663a69dc11059a0a11bb8625389c6da061c3048a9e70c1";
   for (int i = 0; i < 2; i++) {
        jstring value = env->NewStringUTF(hash[i]);
        env->SetObjectArrayElement(valueArray, i, value);
   }
   return valueArray;
  }

模数和指数在我的计算机上通过以下命令获取:

 PBKEY_INFO=`openssl rsa -in private.pem -text -noout`
  MODULUS=`echo "$PBKEY_INFO" | grep Modulus: -A 5 | tail -5`
  echo `echo $MODULUS | tr -cd [:alnum:]`

 PREXP=`echo "$PRKEY_INFO" | grep privateExponent: -A 5 | tail -5`
 echo `echo $PREXP | tr -cd [:alnum:]`

请问我的代码有什么问题?

byte [] unlockedData在结尾处为空

我得到了这些踪迹:java.security.InvalidKeyException:算法RSA的密钥不适合对称加密。

谢谢。

0 个答案:

没有答案