当前位置: 首页 > 知识库问答 >
问题:

Android:使用存储在文件中的公钥解密RSA文本

魏誉
2023-03-14

我试了好几天都没有成功。

在StackOverflow中有很多类似的问题,甚至有两个问题与我的问题完全相同,但没有得到回答和解决:1)将PHP RSA公钥转换为Android公钥;2)Android:如何用RSA密钥解密openssl加密文件?

我看到了很多关于如何解密RSA文本的示例,如下所示:

public static byte[] decryptRSA( PublicKey key, byte[] text) throws Exception
      { 
          byte[] dectyptedText = null;

          Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
          cipher.init(Cipher.DECRYPT_MODE, key);
          dectyptedText = cipher.doFinal(text);
          return dectyptedText;
      }
    InputStream is = getResources().openRawResource(R.raw.public);
    DataInputStream dis = new DataInputStream(is);
    byte [] keyBytes = new byte [(int) is.available()];
    dis.readFully(keyBytes);
    dis.close();
    X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    return keyFactory.generatePublic(spec);

共有1个答案

孙成化
2023-03-14

终于解决了!!!鼓声、号角声和美妙的交响乐!!!

public static byte[] decryptRSA(Context mContext, byte[] message) throws Exception { 

    // reads the public key stored in a file
    InputStream is = mContext.getResources().openRawResource(R.raw.sm_public);
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    List<String> lines = new ArrayList<String>();
    String line = null;
    while ((line = br.readLine()) != null)
        lines.add(line);

    // removes the first and last lines of the file (comments)
    if (lines.size() > 1 && lines.get(0).startsWith("-----") && lines.get(lines.size()-1).startsWith("-----")) {
        lines.remove(0);
        lines.remove(lines.size()-1);
    }

    // concats the remaining lines to a single String
    StringBuilder sb = new StringBuilder();
    for (String aLine: lines)
        sb.append(aLine);
    String keyString = sb.toString();
    Log.d("log", "keyString:"+keyString);

    // converts the String to a PublicKey instance
    byte[] keyBytes = Base64.decodeBase64(keyString.getBytes("utf-8"));
    X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PublicKey key = keyFactory.generatePublic(spec);

    // decrypts the message
    byte[] dectyptedText = null;
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, key);
    dectyptedText = cipher.doFinal(Base64.decodeBase64(message));
    return dectyptedText;
}

解决方案是Base64不仅解码从文件中读取的公钥,而且解码加密的消息本身!

顺便说一下,我按照@Nikolay建议的方式从文件中读取公钥(又是tnx男)。

 类似资料:
  • 我使用RSA_public_encrypt函数发送加密数据到套接字。我正在读取公钥。使用"pkey=PEM_read_PUBKEY(f, NULL, NULL, NULL);"函数的PEM文件。从上面的函数中检索的pkey是类型EVP_PKEY*,我不能在函数RSA_public_encrypt中使用。(RSA_public_encrypt使用RSA*类型密钥) 如何将EVP_PKEY*PKEY转

  • 据我所知,如我所愿,我应该能够使用RSA来确保真实性或隐私。在我的例子中,我想确保真实性,所以我用私钥加密数据,并允许任何人用公钥解密。数据并不是真正的秘密,但我需要保证它是由公钥(和私钥)的所有者创建的。 当我尝试使用PyCrypto解密时,我从PyCrypto得到没有私钥错误。代码是这样的: 我使用公钥文件的路径(OpenSSH格式)调用它加密数据不是由我生成的,它不是用Python而是用PH

  • 我想使用带有RSA算法的OpenSSL使用私钥加密文件: 现在,如果我执行解密操作: 此操作需要私钥 我知道我应该使用公钥进行加密,如果我使用私钥,我会得到一个签名。 然而,我想这样做是为了学习。

  • 但这总是给我以下的例外- 我的键盘生成逻辑- 我的加密逻辑- Base64 Util方法-

  • 我的理解是,公钥可以用于加密,私钥可以用于解密,公钥不能解密由同一公钥加密的文件。我有没有误解,或者我做错了什么? 1) 生成密钥 openssl genrsa-out./private.pem2048 2) 生成公钥 openssl rsa-in/私有的pem-发布 3)加密一个小文本文件 openssl加密/在里面txt-输出/出来附件e-aes256-k/平民的pem公司 4) 使用公钥解密

  • 问题内容: 我正在编写一个用于传输文件的小型应用程序,或多或少地将其作为一种学习更多编程加密基础的方法。这个想法是生成一个RSA密钥对,交换公共密钥,并发送AES iv和密钥以进一步解密。我想用接收者的RSA公钥加密AES密钥,如下所示: 然后,我将密钥值写给接收器,并按如下方式解密: 在控制台的另一端,我将其作为输出: 此外,如果我创建一个大小为16的字节数组,并将cipher.doFinal(