今天逆向一个android RSA 加密 java,python都无法还原加密函数。
百思不得骑姐,发量又少了。
搜索N久找到参考资料:
https://blog.csdn.net/weixin_42489404/article/details/98024121
OK!!!!!!
android测试没有问题
java 报错(请知道解决办法的告知下)
错误:java.lang.Exception: java.security.InvalidKeyException: RSA keys must be at least 512 bits long
android 代码如下:
import java.security.KeyFactory; import java.security.interfaces.RSAPublicKey; import java.security.spec.InvalidKeySpecException; public final class RSA { public static final String ALGORITHM_RSA = "RSA"; public static String D(String msg, String n, String e) throws Exception { Cipher instance = Cipher.getInstance(ALGORITHM_RSA); instance.init(1, bu(str2, str3)); // 生成公钥 byte[] doFinal = instance.doFinal(str.getBytes()); StringBuffer stringBuffer = new StringBuffer(); for (byte b : doFinal) { stringBuffer.append(Integer.toString((b & 255) + 256, 16).substring(1)); } return stringBuffer.toString(); } // getPublicKey private static RSAPublicKey bu(String modulus, String exponent) throws Exception { try { KeySpec keySpec = new RSAPublicKeySpec(new BigInteger(modulus, 16), new BigInteger(exponent, 16)); return (RSAPublicKey) KeyFactory.getInstance(ALGORITHM_RSA).generatePublic(keySpec); } catch (InvalidKeySpecException e2) { throw new Exception(e2.getMessage()); } } public static String getTest(){ String retPass, password, e, n; password = "2222"; n = "ab09797f5408c4e1b43e985571db37a6ace935d5b498877f84634ccc61ddbaad"; e = "10001"; // 正确结果:8fafd167efa96567cb193c73738d41b9b0a113ae86cdc1c019682c32cb91e05f try { retPass = RSA.D(password, n, e); } catch (Exception e1) { retPass = "错误:" + e1; e1.printStackTrace(); } return retPass; }
python 代码:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Time : 2022/3/19 10:51 # @Site : # @File : res1.py # @Software: PyCharm import rsa import base64 from Crypto.PublicKey import RSA class RSA_ECB_Nopadding: def __init__(self, size, n, e): self.n = n self.e = e self.size = size def _toBin(self, s): """可能有些编码扩展位数""" b = bytes(s.encode()) if self.size > 0: for i in range(self.size - len(b)): b += b'\0' return b def encrypt(self, message): kLen = rsa.common.byte_size(self.n) msg = self._toBin(message) _b = rsa.transform.bytes2int(msg) _i = rsa.core.encrypt_int(_b, self.e, self.n) result = rsa.transform.int2bytes(_i, kLen) return result.hex().lower() class RSA_ECB_Nopadding_str(RSA_ECB_Nopadding): def __init__(self, size, key): self.pubkey = RSA.importKey(base64.b64decode(key)) super().__init__(size, self.pubkey.n, self.pubkey.e) if __name__ == '__main__': message = '2222' n = b"ab09797f5408c4e1b43e985571db37a6ace935d5b498877f84634ccc61ddbaad" e = b"10001" # 8fafd167efa96567cb193c73738d41b9b0a113ae86cdc1c019682c32cb91e05f n = int(n, 16) e = int(e, 16) rsaA = RSA_ECB_Nopadding(1, n, e) print(rsaA.encrypt(message)) // message = '{"code":"123451","clienttime":1564560057}' // pubkey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD2DT4odzkDd7hMlZ7djdZQH12j38nKxriINW1MGjMry3tXheya113xwmbBOwN0GA4zTwKFauFJRzcsD0nDFq1eaatcFKeDF25R4dnQRX+4BdTwFVS8lIb8nJMluSBwK+i4Z3VF+gfZ0AqQOXda6lJ4jPBt9Ep7VXEAHXUDn9JM8wIDAQAB" // rsaA = RSA_ECB_Nopadding_str(128, pubkey) // print(rsaA.encrypt(message))