1.首先新建java project,然后新建类,代码如下
如果需要第三方类的支持,需要添加build path,比如这个类使用了alibaba的fastjson.jar
package one;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.UUID;
import javax.crypto.Cipher;
import javax.crypto.spec.OAEPParameterSpec;
import javax.crypto.spec.PSource;
import com.alibaba.fastjson.JSONObject;
//需要导入jar包fastjson.jar
public class jiami {
public static void main(String[] args) throws Exception{
jiami j = new jiami();
int offset = Integer.parseInt(args[1]);
//String v = j.getSecretValue(publicKey,1, "qatest054","Aa123456");
String v = j.getSecretValue(args[0],offset, args[2],args[3]);
System.out.println(v);
}
public String getSecretValue(String publicKey, int offset,String account, String pwd) throws Exception {
String publicKeyPEM = publicKey
.replace("-----BEGIN PUBLIC KEY-----", "")
.replaceAll(System.lineSeparator(), "")
.replace("-----END PUBLIC KEY-----", "");
byte[] pbks = Base64.getMimeDecoder().decode(publicKeyPEM);
X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(pbks);
PublicKey newPbk = KeyFactory.getInstance("RSA").generatePublic(encodedKeySpec);
OAEPParameterSpec oaepParameterSpec = new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-256"), PSource.PSpecified.DEFAULT);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
cipher.init(Cipher.ENCRYPT_MODE, newPbk, oaepParameterSpec);
JSONObject jsonObject = new JSONObject();
jsonObject = createSignInPayload(offset, account, pwd);
byte[] bytes = cipher.doFinal(jsonObject.toString().getBytes());
return Base64.getEncoder().encodeToString(bytes);
}
public JSONObject createSignInPayload(int offset, String account, String pwd) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", account);
jsonObject.put("authType", "PASSWORD");
jsonObject.put("authValue", pwd);
int timestamp = Math.round(System.currentTimeMillis() / 1000);
jsonObject.put("timestamp", offset + timestamp);
jsonObject.put("salt", UUID.randomUUID().toString());
return jsonObject;
}
}
右键点击项目,点击Export导出jar包
python代码如下
// path是jar包所在路径 b.jar是jar包的名称
path = r"C:\Users\jeff.xie\Desktop"
os.chdir(path)
//带了4个参数传入
cmds = "java -jar b.jar %s %d %s %s" %(public_key, offset, "tester054", "Aa123456")
print(cmds)
secret_r = os.popen(cmds)
credentials = secret_r.read() #读取java打印的值
如果java输出了多行的值,可以使用一下方式
for line in secret_r.splitlines():
print line
python执行系统命令后获取返回值的几种方式集合
第一种情况
os.system('ps aux')
执行系统命令,没有返回值
第二种情况
result = os.popen('ps aux')
res = result.read()
for line in res.splitlines():
print line
执行系统命令,可以获取执行系统命令的结果
p = subprocess.Popen('ps aux',shell=True,stdout=subprocess.PIPE)
out,err = p.communicate()
for line in out.splitlines():
print line
同上,执行系统命令,可以获取执行系统命令的结果
第三种情况
output = commands.getstatusoutput('ps aux')
print output
执行系统命令,并获取当前函数的返回值
以上就是python执行系统命令后获取返回值的几种方式集合