2017-10-18 8 views
0

私はusersputからargsを使ってパスワードを暗号化し、confファイルの既存のdict変数を上書きせずに既存のconfファイルに書き込もうとしています。Pythonを使用して既存のdict変数を上書きせずにconf/jsonファイルに特定の変数を書き込む方法

私は以下のスクリプトを試してみましたが、それを達成する方法を教えてください、または何か提案がありがとうございます。

inputJson.conf

{ 
    "MetadataInputs": { 
     "redHat": { 
      "hostName": "10.110.20.30", 
      "userName": "admin", 
      "password": "admin123", 
      "organisationName": "networks", 
      "repository":"Red Hat" 
     } 
    } 
} 

WriteJson.py

import json 
from Crypto.Cipher import AES 
import base64 
import sys 
import io 

with open('conf.json') as inputParameters: 
     readInputData = json.load(inputParameters) 


class Test(): 
    def __init__(self,readInputData): 
     self.__dict__=readInputData 
     self.hostName = readInputData['MetadataInputs']['redHat']['hostName'] 
     self.userName = readInputData['MetadataInputs']['redHat']['userName'] 
     self.orgName = readInputData['MetadataInputs']['redHat']['organisationName'] 
     self.repostiroyName = readInputData['MetadataInputs']['redHat']['repository'] 

    def encryptPassword(self): 
     try: 
      args = sys.argv[1:] 
      self.inputPassword = sys.argv[1] 
      msg_text = self.inputPassword.rjust(32) 
      secret_key = '123456789' 
      cipher = AES.new(secret_key, AES.MODE_ECB) 
      encoded = base64.b64encode(cipher.encrypt(msg_text)) 
      # self.password = { 
      #  "MetadataInputs": { 
      #   "redHat": { 
      #    "password": encoded 
      #   } 
      #  }} 
      with io.open('conf.json', 'w', encoding='utf8') as outfile: 
       self.password=outfile['MetadataInputs']['redHat']['password'] 
       str_ = json.dump(self.password, outfile, indent=4, ensure_ascii=False) 
       outfile.write((str_)) 

     except Exception: 
      print "Exception Occurred --- Please provide password " 



obj = Test(readInputData) 
obj.encryptPassword() 

答えて

0

私は私のアプローチが間違っていた、このための答えを得ました。私は以下の答えでそれを修正しました。誰でも私が試しているものに対してより良いアプローチをしているなら、あなたの答えを投稿してください。

import json 
from Crypto.Cipher import AES 
import base64 
import sys 

with open('outputJson.json') as inputParameters: 
     readInputData = json.load(inputParameters) 

class Test(): 
    def __init__(self,readInputData): 
     self.__dict__=readInputData 
     self.rhsnHostName = readInputData['MetadataInputs']['redHat']['hostName'] 
     self.rhsnUserName = readInputData['MetadataInputs']['redHat']['userName'] 
     self.rhsnOrgName = readInputData['MetadataInputs']['redHat']['organisationName'] 
     self.rhsnRepostiroyName = readInputData['MetadataInputs']['redHat']['repositoryName'] 

    def encryptPassword(self): 
     try: 
      args = sys.argv[1:] 
      self.inputPassword = sys.argv[1] 
      msg_text = self.inputPassword.rjust(32) 
      secret_key = '123456789' # create new & store somewhere safe 
      print "Secret Key length is "+ str(len(secret_key)) 
      cipher = AES.new(secret_key, AES.MODE_ECB) # never use ECB in strong systems obviously 
      encoded = base64.b64encode(cipher.encrypt(msg_text)) 
      print "Encoded password for %s is %s "%(encoded,self.inputPassword) 
      print "\n" 
      redHatPatchMetaDataConf = {} 
      redHatPatchMetaDataConf={ 
         "MetadataInputs": { 
          "redHat": { 
           "hostName": self.rhsnHostName, 
           "userName": self.rhsnUserName, 
           "password": encoded, 
           "organisationName": self.rhsnOrgName, 
           "repositoryName":self.rhsnRepostiroyName 
          } 
         } 
        } 
      outfile = open('outputJson.json', 'w'); 
      json.dump(redHatPatchMetaDataConf, outfile, indent=4, skipkeys=True, sort_keys=True) 
      outfile.close() 
      with open('outputJson.json') as decodeJson: 
       try: 
        readInputData = json.load(decodeJson) 
        decPassword = readInputData['MetadataInputs']['redHat']['password'] 
        print "decPassword is "+decPassword 
        decoded = cipher.decrypt(base64.b64decode(decPassword)) 
        print "Decoded password for %s is %s"%(decPassword,decoded.strip()) 
       except Exception: 
        print "Unable to Decode Password" 
     except Exception as e: 
      print e 

obj = Test(readInputData) 
obj.encryptPassword() 
関連する問題