Exemple #1
0
func encrypt(plaintext, keyBytes []byte) ([]byte, error) {
	publicKey, err := bletchley.PEMToPublicKey(keyBytes)
	if err != nil {
		return []byte{}, err
	}

	encrypted, err := bletchley.Encrypt(publicKey, plaintext)
	if err != nil {
		return []byte{}, err
	}

	return json.Marshal(encrypted)
}
		publicKey  *rsa.PublicKey
	)

	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())