1

私はRSA Public-Key以下の形式でサーバー側に持っています:

<string xmlns="http://www.cherripik.com/">
<RSAKeyValue><Modulus>abc</Modulus><Exponent>abc</Exponent></RSAKeyValue>
</string>

encrypt string私はほとんどすべての可能な方法を試しましたが、この公開鍵を Android 側で使用することはできませんでした。"abc"この公開鍵を使用して任意の文字列を暗号化し、その暗号化された鍵を復号化して に戻す例を教えてください"abc"。とても参考になります。

よろしくお願いします。

以下は私が使用した方法ですが、成功しませんでした。それはいくらかの値を与えましたが、正しくありません。

public String encrypt(String message, String Modulus, String Exponent) {
        String outputEncrypted = "";
        try {
             byte[] modulusBytes = Base64Coder.decode(Modulus);
               byte[] exponentBytes = Base64Coder.decode(Exponent);
               BigInteger modulus = new BigInteger(modulusBytes );               
               BigInteger exponent = new BigInteger(exponentBytes);

               RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);
               KeyFactory fact = KeyFactory.getInstance("RSA");
               PublicKey pubKey = fact.generatePublic(rsaPubKey);

               Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
               cipher.init(Cipher.ENCRYPT_MODE, pubKey);

               byte[] plainBytes = new String("abc").getBytes("UTF-8");
               byte[] cipherData = cipher.doFinal( plainBytes );
               String encryptedString = new String(Base64Coder.encode(cipherData));
               Log.i(this.getClass().getSimpleName(), "encryptedString : "+encryptedString);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return outputEncrypted;
    }

上記の方法で暗号化された文字列を作成するときのもう1つのこと。346 文字の暗号化された文字列が得られます。しかし、私のサーバーでは、暗号化と復号化の方法しかありません。サーバー暗号化方式では、344 文字が生成されます。最後に、暗号化された文字列をサーバー メソッドに配置して、暗号化された文字列が正しいことを確認します。サーバーはこのエラーをスローします。

<string xmlns="http://www.Myserver.com/">Error occurred while decoding OAEP padding.</string>
4

0 に答える 0