RSA和AES解密和加密问题

发布于 2024-09-18 11:56:31 字数 1487 浏览 7 评论 0原文

我已经在我的 Android 应用程序上生成了一对 RSA 密钥。

我从网络服务收到 - AES 密钥,使用我的 RSA 公钥加密 - 使用 AES 密钥编码的字符串。

所以我必须执行以下操作: - 解密 AES 密钥 - 使用获得的 AES 密钥解密字符串。

为了生成 RSA 密钥,我做了:

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

在 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;
      }

在 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;   
        }

因此,在我的代码中,为了获取解密的 AES 密钥,我这样做

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

最后,我得到的解密字符串为空。 我做错了什么?

I have generated on my android application a pair of RSA Keys.

I receive from a web service
- an AES Key, encrypted with my RSA public key
- a String encoded with the AES key.

So I must do the following:
- decrypt the AES Key
- decrypt the string with the obtained AES Key.

To generate the RSA Keys I did:

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

On RSA decrypt I use :

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:

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

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.
What am I doing wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡水深流 2024-09-25 11:56:31

好吧,问题是解密的密钥是 8 字节长,我必须将其设为 16 字节才能兼容 AES 128 位

所以,我做了一个将其转换回来的方法

 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;
      }

以及重写的解密方法:

  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;   
        }

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

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;   
        }
別甾虛僞 2024-09-25 11:56:31

我不确定你正在使用什么语言或库(看起来像Java?),但可以尝试一些非常通用的事情:

  1. 你得到加密的字符串了吗?检查 ReceivedEncryptedAESString 的长度和 Base64.decode 的输出以检查它们是否正常。
  2. AES 解密不会失败,因此它一定是库初始化中的问题。在构建步骤和初始化步骤之后检查 cipher 的值/状态。
  3. 尝试一个更简单的测试用例:忽略 RSA 加密并尝试使用 Cipher 对象解密某些内容。

I'm not sure what language or libraries you are using (looks like Java?), but some really general things to try:

  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.
  2. AES decryption can't fail so it must be a problem in the library initialisation. Check the value/state of cipher after the construction step and the init step.
  3. Try a simpler testcase: ignore the RSA encryption and just try to decrypt something using your Cipher object.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文