Пример #1
0
// Sign signs a data of a namespace
func (kml *KeyManagerLocal) Decrypt(proto string, namespace string, data []byte) ([]byte, error) {
	keyDir, err := kml.getKeyDir(proto, namespace)
	if err != nil {
		return nil, err
	}

	privBytes, _ := ioutil.ReadFile(filepath.Join(keyDir, defaultPrivateKey))
	return utils.RSADecrypt(privBytes, data)
}
Пример #2
0
// TestRSAGenerateEnDe
func TestRSAGenerateEnDe(t *testing.T) {
	privBytes, pubBytes, err := utils.GenerateRSAKeyPair(1024)
	assert.Nil(t, err, "Fail to genereate RSA Key Pair")

	testData := []byte("This is the testdata for encrypt and decryp")
	encrypted, err := utils.RSAEncrypt(pubBytes, testData)
	assert.Nil(t, err, "Fail to encrypt data")
	decrypted, err := utils.RSADecrypt(privBytes, encrypted)
	assert.Nil(t, err, "Fail to decrypt data")
	assert.Equal(t, testData, decrypted, "Fail to get correct data after en/de")
}
Пример #3
0
		}

		privFile := context.Args().Get(0)
		encryptedFile := context.Args().Get(1)
		privBytes, err := ioutil.ReadFile(privFile)
		if err != nil {
			fmt.Println(err)
			return err
		}
		encryptedBytes, err := ioutil.ReadFile(encryptedFile)
		if err != nil {
			fmt.Println(err)
			return err
		}

		decryptedBytes, err := utils.RSADecrypt(privBytes, encryptedBytes)
		if err != nil {
			fmt.Println(err)
			return err
		}

		decryptedFile := encryptedFile + "-decrypted"
		err = ioutil.WriteFile(decryptedFile, decryptedBytes, 0644)
		if err != nil {
			fmt.Println(err)
			return err
		}
		fmt.Printf("Success to decrypt %s to %s\n", encryptedFile, decryptedFile)
		return nil
	},
}