mbedlts库和android库之间的加密/解密

我正在为手机更新新版本,其中包含一些rsa加密/解密,我的旧手机版本使用嵌入式(C代码),该代码使用mbedlts库(https://github.com/ARMmbed/mbedtls)进行rsa加密/解密。现在,我更新为使用android,因此我希望它可以与旧手机版本兼容进行加密/解密。

我阅读了mbedlts API,并且知道使用RSA PCKS#1 v1.5,我尝试尝试使用android使用KeyPairGenerator.getInstance(“ RSA”)从Android生成密钥,并在旧版本上使用,有时它会成功,有些时间失败。我从此处(https://tools.ietf.org/html/rfc2313)阅读了pkcs#1 v1.5,但找不到任何好的信息。

这是我创建的用于测试的应用程序,我在此处生成指数,模数并在旧版本上使用,我使用一些实例,例如“ RSA”,“ RSA / NONE / pkcs1Padding”,但未成功。请注意,旧手机使用Little Endian,我尝试将RSA密钥重新格式化为Little Endian,但仍不稳定(有时成功,有时失败)

public class Mainactivity extends AppCompatactivity {
    public static final String TAG = "CryptographyMyApp";

    private byte[] testByte = {1,2,3,4,5,6,7,8,9};
    private KeyPairGenerator kpg;
    private KeyGenParameterSpec spec;
    private EditText mEncryptedEditText;
    private Button mDecryptButton;

    private PublicKey mPublicKey;
    private PrivateKey mPrivateKey;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mEncryptedEditText = (EditText) findViewById(R.id.encrypted_edit_text);
        mDecryptButton = (Button) findViewById(R.id.decrypt_button);

        try {
            spec = new KeyGenParameterSpec
                    .Builder("test",KeyProperties.PURPOSE_ENCRYPT|KeyProperties.PURPOSE_DECRYPT)
                    .setKeySize(1024)
                    .build();

            kpg = KeyPairGenerator.getInstance("RSA");
            kpg.initialize(1024);
            kpg.generateKeyPair();
            KeyPair keyPair = kpg.generateKeyPair();
            mPublicKey = keyPair.getPublic();
            mPrivateKey = keyPair.getPrivate();

            RSAPublicKey rsaPublicKey = (RSAPublicKey) mPublicKey;

            Log.i(TAG,"exponent: len=" + rsaPublicKey.getPublicExponent().toByteArray().length);
            Log.i(TAG,"exponent: byte=" + rsaPublicKey.getPublicExponent().toString(16));
            Log.i(TAG,"modulus: len=" + rsaPublicKey.getmodulus().toByteArray().length);
            Log.i(TAG,"modulus: byte=" + rsaPublicKey.getmodulus().toString(16));
            Log.i(TAG,"modulus: byte=" + Arrays.toString(rsaPublicKey.getmodulus().toByteArray()));

            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE,mPublicKey);
            Log.i(TAG,"encrypted_msg=" + Arrays.toString(cipher.doFinal(testByte)));
        } catch (Exception e) {
            Log.e(TAG,"onCreate:",e);
        }
    }

    public void decrypt(View view) {
        String s = mEncryptedEditText.getText().toString().trim();
        byte[] output;

        try {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE,mPrivateKey);
            output = cipher.doFinal(hexStringToByteArray(s));
            Log.i(TAG,"output=" + Arrays.toString(output));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

xichenhuahua 回答:mbedlts库和android库之间的加密/解密

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3168409.html

大家都在问