简体   繁体   English

RSA和AES解密和加密问题

[英]RSA and AES Decrypt and Encrypt problem

I have generated on my android application a pair of RSA Keys. 我在我的Android应用程序上生成了一对RSA密钥。

I receive from a web service - an AES Key, encrypted with my RSA public key - a String encoded with the AES key. 我收到一个Web服务 - 一个AES密钥,用我的RSA公钥加密 - 一个用AES密钥编码的字符串。

So I must do the following: - decrypt the AES Key - decrypt the string with the obtained AES Key. 所以我必须执行以下操作: - 解密AES密钥 - 使用获得的AES密钥解密字符串。

To generate the RSA Keys I did: 为了生成RSA密钥我做了:

 keyGen = KeyPairGenerator.getInstance("RSA");
  keyGen.initialize(size);
  keypair = keyGen.genKeyPair();
  privateKey = keypair.getPrivate();
  publicKey = keypair.getPublic();

On RSA decrypt I use : 在RSA解密我使用:

public static byte[] decryptRSA( PrivateKey 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;
      }

On AES decrypt I use: 在AES解密我使用:

public static byte[] decryptAES(byte[] key, byte[] text) throws Exception {   
            SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");   
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS1Padding");   
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);   
            byte[] decrypted = cipher.doFinal(text);   
            return decrypted;   
        }

So, in my code, to obtain the decrypted AES Key I do 所以,在我的代码中,我要获得解密的AES密钥

byte[] decryptedAESKey = sm.decryptRSA(key, Base64.decode(ReceivedBase64EncryptedAESKey));
byte[] decryptedString = sm.decryptAES(decryptedAESKey, Base64.decode(ReceivedEncryptedAESString));

On the end I get a null for decryptedString. 最后,我获得了decryptedString的null。 What am I doing wrong ? 我究竟做错了什么 ?

Well, the thing is that the key decrypted was 8 byte long and I had to make it 16 byte to be AES 128 bits compatible 嗯,问题是解密的密钥是8字节长,我必须使其16字节与AES 128位兼容

So, I made a method to convert it back 所以,我做了一个方法将其转换回来

 private static byte[] GetKey(byte[] suggestedKey)
      {
          byte[] kRaw = suggestedKey;
          ArrayList<Byte> kList = new  ArrayList<Byte>();

          for (int i = 0; i < 128; i += 8)
          {
              kList.add(kRaw[(i / 8) % kRaw.length]);
          }

          byte[] byteArray = new byte[kList.size()];
          for(int i = 0; i<kList.size(); i++){
            byteArray[i] = kList.get(i);
          }
          return byteArray;
      }

And the rewritten decrypt method: 并重写的解密方法:

  public static byte[] decryptAES(byte[] key, byte[] text) throws Exception {   

          SecretKeySpec skeySpec = new SecretKeySpec(GetKey(key), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");  

            byte [] iv = new byte[cipher.getBlockSize()];
            for(int i=0;i<iv.length;i++)iv[i] = 0;
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);

            byte[] decrypted = cipher.doFinal(text);   
            return decrypted;   
        }

I'm not sure what language or libraries you are using (looks like Java?), but some really general things to try: 我不确定你使用的是哪种语言或库(看起来像Java?),但有些事情要尝试:

  1. Did you get the encrypted string, ok? 你有加密的字符串,好吗? Check the length of ReceivedEncryptedAESString and the output of the Base64.decode to check they look alright. 检查ReceivedEncryptedAESString的长度和Base64.decode的输出以检查它们是否正常。
  2. AES decryption can't fail so it must be a problem in the library initialisation. AES解密不会失败,因此它必须是库初始化中的问题。 Check the value/state of cipher after the construction step and the init step. 在构造步骤和初始化步骤之后检查cipher的值/状态。
  3. Try a simpler testcase: ignore the RSA encryption and just try to decrypt something using your Cipher object. 尝试更简单的测试用例:忽略RSA加密,然后尝试使用Cipher对象解密。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM