Explain the use of cryptography in ensuring the confidentiality and integrity of both data in transit and data at rest.