MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / encryptAndWriteToFile

Function encryptAndWriteToFile

ciphers/rsa_cipher.py:89–102  ·  view source on GitHub ↗
(messageFilename, keyFilename, message, blockSize=DEFAULT_BLOCK_SIZE)

Source from the content-addressed store, hash-verified

87
88
89def encryptAndWriteToFile(messageFilename, keyFilename, message, blockSize=DEFAULT_BLOCK_SIZE):
90 keySize, n, e = readKeyFile(keyFilename)
91 if keySize < blockSize * 8:
92 sys.exit('ERROR: Block size is %s bits and key size is %s bits. The RSA cipher requires the block size to be equal to or greater than the key size. Either decrease the block size or use different keys.' % (blockSize * 8, keySize))
93
94 encryptedBlocks = encryptMessage(message, (n, e), blockSize)
95
96 for i in range(len(encryptedBlocks)):
97 encryptedBlocks[i] = str(encryptedBlocks[i])
98 encryptedContent = ','.join(encryptedBlocks)
99 encryptedContent = '%s_%s_%s' % (len(message), blockSize, encryptedContent)
100 with open(messageFilename, 'w') as fo:
101 fo.write(encryptedContent)
102 return encryptedContent
103
104
105def readFromFileAndDecrypt(messageFilename, keyFilename):

Callers 1

mainFunction · 0.85

Calls 2

readKeyFileFunction · 0.85
encryptMessageFunction · 0.70

Tested by

no test coverage detected