예제 #1
0
func decrypt(ciphertext, keyBytes []byte) ([]byte, error) {
	privateKey, err := bletchley.PEMToPrivateKey(keyBytes)
	if err != nil {
		return []byte{}, err
	}

	var encrypted bletchley.EncryptedMessage
	err = json.Unmarshal(ciphertext, &encrypted)
	if err != nil {
		return []byte{}, fmt.Errorf("Expected JSON input: " + err.Error())
	}

	return bletchley.Decrypt(privateKey, encrypted)
}
예제 #2
0
	BeforeEach(func() {
		nbits := 1024
		var err error
		privateKey, err = rsa.GenerateKey(rand.Reader, nbits)
		Expect(err).NotTo(HaveOccurred())

		publicKey = privateKey.Public().(*rsa.PublicKey)
	})

	It("should encrypt and decrypt without data loss", func() {
		message := []byte("this is a secret message")

		encrypted, err := bletchley.Encrypt(publicKey, message)
		Expect(err).NotTo(HaveOccurred())

		decrypted, err := bletchley.Decrypt(privateKey, encrypted)
		Expect(err).NotTo(HaveOccurred())

		Expect(decrypted).To(Equal(message))
	})

	It("should handle plaintext messages longer than the asymmetric key", func() {
		longMessage := make([]byte, 1024*1024)
		Expect(rand.Read(longMessage)).To(Equal(len(longMessage)))

		encrypted, err := bletchley.Encrypt(publicKey, longMessage)
		Expect(err).NotTo(HaveOccurred())

		decrypted, err := bletchley.Decrypt(privateKey, encrypted)
		Expect(err).NotTo(HaveOccurred())