Cloud Code | Encrypted Password Clı
robertxxx
User
robertxxx has not provided any additional information.
import argparse
import json
from cryptography.fernet import Fernet
def load_key():
try:
with open("secretkey.key", "rb") as f:
return f.read()
except FileNotFoundError:
return None
def generate_key():
key = Fernet.generate_key()
with open("secretkey.key", "wb") as f:
f.write(key)
return key
def regen_key(prev_key):
current = load_key()
if prev_key == current:
new_key = Fernet.generate_key()
with open("secretkey.key", "wb") as f:
f.write(new_key)
return new_key
else:
return "Previous key is invalid."
def add_service(key, service, password):
fernet = Fernet(key)
encrypted_password = fernet.encrypt(password.encode()).decode()
try:
with open("psw.json", "r") as file:
data = json.load(file)
except FileNotFoundError:
data = {}
data[service] = encrypted_password
with open("psw.json", "w") as file:
json.dump(data, file)
def get_service(key, service):
fernet = Fernet(key)
try:
with open("psw.json", "r") as file:
data = json.load(file)
except FileNotFoundError:
print("No services found.")
return
if service in data:
encrypted_password = data[service]
password = fernet.decrypt(encrypted_password.encode()).decode()
print(f"Password for {service}: {password}")
else:
print(f"No password found for service: {service}")
def remove_service(key, service):
try:
with open("psw.json", "r") as file:
data = json.load(file)
except FileNotFoundError:
print("No services found.")
return
if service in data:
del data[service]
with open("psw.json", "w") as file:
json.dump(data, file)
print(f"Service {service} removed.")
else:
print(f"No service found with name: {service}")
def main():
parser = argparse.ArgumentParser(description="Encrypted Password Generator CLI")
subparsers = parser.add_subparsers(dest="command")
subparsers.add_parser("generate-key", help="Generate a new secret key")
regen_parser = subparsers.add_parser("regen-key", help="Regenerate the secret key")
regen_parser.add_argument("-prev", "--previous", required=True, help="Previous secret key")
add_parser = subparsers.add_parser("add-service", help="Add a new service")
add_parser.add_argument("-key", "--key", required=True, help="Secret key")
add_parser.add_argument("-service", "--service", required=True, help="Service name")
add_parser.add_argument("-password", "--password", required=True, help="Password for the service")
get_parser = subparsers.add_parser("get-psw", help="Get the password for a service")
get_parser.add_argument("-key", "--key", required=True, help="Secret key")
get_parser.add_argument("-service", "--service", required=True, help="Service name")
remove_parser = subparsers.add_parser("remove-service", help="Remove a service")
remove_parser.add_argument("-key", "--key", required=True, help="Secret key")
remove_parser.add_argument("-service", "--service", required=True, help="Service name")
args = parser.parse_args()
if args.command == "generate-key":
key = generate_key()
print(f"Key generated: {key.decode()}")
elif args.command == "regen-key":
prev_key = args.previous.encode()
new_key = regen_key(prev_key)
print(f"New key generated: {new_key.decode()}")
elif args.command == "add-service":
key = args.key.encode()
add_service(key, args.service, args.password)
print(f"Service {args.service} added.")
elif args.command == "get-psw":
key = args.key.encode()
get_service(key, args.service)
elif args.command == "remove-service":
key = args.key.encode()
remove_service(key, args.service)
if __name__ == '__main__':
main()
import json
from cryptography.fernet import Fernet
def load_key():
try:
with open("secretkey.key", "rb") as f:
return f.read()
except FileNotFoundError:
return None
def generate_key():
key = Fernet.generate_key()
with open("secretkey.key", "wb") as f:
f.write(key)
return key
def regen_key(prev_key):
current = load_key()
if prev_key == current:
new_key = Fernet.generate_key()
with open("secretkey.key", "wb") as f:
f.write(new_key)
return new_key
else:
return "Previous key is invalid."
def add_service(key, service, password):
fernet = Fernet(key)
encrypted_password = fernet.encrypt(password.encode()).decode()
try:
with open("psw.json", "r") as file:
data = json.load(file)
except FileNotFoundError:
data = {}
data[service] = encrypted_password
with open("psw.json", "w") as file:
json.dump(data, file)
def get_service(key, service):
fernet = Fernet(key)
try:
with open("psw.json", "r") as file:
data = json.load(file)
except FileNotFoundError:
print("No services found.")
return
if service in data:
encrypted_password = data[service]
password = fernet.decrypt(encrypted_password.encode()).decode()
print(f"Password for {service}: {password}")
else:
print(f"No password found for service: {service}")
def remove_service(key, service):
try:
with open("psw.json", "r") as file:
data = json.load(file)
except FileNotFoundError:
print("No services found.")
return
if service in data:
del data[service]
with open("psw.json", "w") as file:
json.dump(data, file)
print(f"Service {service} removed.")
else:
print(f"No service found with name: {service}")
def main():
parser = argparse.ArgumentParser(description="Encrypted Password Generator CLI")
subparsers = parser.add_subparsers(dest="command")
subparsers.add_parser("generate-key", help="Generate a new secret key")
regen_parser = subparsers.add_parser("regen-key", help="Regenerate the secret key")
regen_parser.add_argument("-prev", "--previous", required=True, help="Previous secret key")
add_parser = subparsers.add_parser("add-service", help="Add a new service")
add_parser.add_argument("-key", "--key", required=True, help="Secret key")
add_parser.add_argument("-service", "--service", required=True, help="Service name")
add_parser.add_argument("-password", "--password", required=True, help="Password for the service")
get_parser = subparsers.add_parser("get-psw", help="Get the password for a service")
get_parser.add_argument("-key", "--key", required=True, help="Secret key")
get_parser.add_argument("-service", "--service", required=True, help="Service name")
remove_parser = subparsers.add_parser("remove-service", help="Remove a service")
remove_parser.add_argument("-key", "--key", required=True, help="Secret key")
remove_parser.add_argument("-service", "--service", required=True, help="Service name")
args = parser.parse_args()
if args.command == "generate-key":
key = generate_key()
print(f"Key generated: {key.decode()}")
elif args.command == "regen-key":
prev_key = args.previous.encode()
new_key = regen_key(prev_key)
print(f"New key generated: {new_key.decode()}")
elif args.command == "add-service":
key = args.key.encode()
add_service(key, args.service, args.password)
print(f"Service {args.service} added.")
elif args.command == "get-psw":
key = args.key.encode()
get_service(key, args.service)
elif args.command == "remove-service":
key = args.key.encode()
remove_service(key, args.service)
if __name__ == '__main__':
main()