Beispiel #1
0
        public void testRsaDecryption()
        {
            TestTpmPrivateKey.KeyTestData dataSet = rsaKeyTestData;

            byte[]        pkcs8 = net.named_data.jndn.util.Common.base64Decode(dataSet.privateKeyPkcs8Unencrypted);
            TpmPrivateKey key   = new TpmPrivateKey();

            key.loadPkcs8(ILOG.J2CsMapping.NIO.ByteBuffer.wrap(pkcs8));

            Blob plainText = new Blob(new int[] { 0x00, 0x01, 0x02, 0x03, 0x04,
                                                  0x05, 0x06, 0x07 });

            String cipherTextBase64 = "i2XNpZ2JbLa4JmBTdDrGmsd4/0C+p+BSCpW3MuPBNe5uChQ0eRO1dvjTnEqwSECY\n"
                                      + "38en9JZwcyb0It/TSFNXHlq+Z1ZpffnjIJxQR9HcgwvwQJh6WRH0vu38tvGkGuNv\n"
                                      + "60Rdn85hqSy1CikmXCeWXL9yCqeqcP21R94G/T3FuA+c1FtFko8KOzCwvrTXMO6n\n"
                                      + "5PNsqlLXabSGr+jz4EwOsSCgPkiDf9U6tXoSPRA2/YvqFQdaiUXIVlomESvaqqZ8\n"
                                      + "FxPs2BON0lobM8gT+xdzbRKofp+rNjNK+5uWyeOnXJwzCszh17cdJl2BH1dZwaVD\n"
                                      + "PmTiSdeDQXZ94U5boDQ4Aw==\n";

            byte[] cipherText = net.named_data.jndn.util.Common.base64Decode(cipherTextBase64);

            Blob decryptedText = key.decrypt(ILOG.J2CsMapping.NIO.ByteBuffer.wrap(cipherText));

            Assert.AssertTrue(decryptedText.equals(plainText));
        }
Beispiel #2
0
        /// <summary>
        /// Decrypt the encryptedData using the keyBits according the encrypt params.
        /// </summary>
        ///
        /// <param name="keyBits">The key value (PKCS8-encoded private key).</param>
        /// <param name="encryptedData">The data to decrypt.</param>
        /// <param name="params">This decrypts according to params.getAlgorithmType().</param>
        /// <returns>The decrypted data.</returns>
        public static Blob decrypt(Blob keyBits, Blob encryptedData,
                                   EncryptParams paras)
        {
            TpmPrivateKey privateKey = new TpmPrivateKey();

            try {
                privateKey.loadPkcs8(keyBits.buf());
            } catch (TpmPrivateKey.Error ex) {
                throw new SecurityException("decrypt: Error in loadPkcs8: " + ex);
            }

            try {
                return(privateKey.decrypt(encryptedData.buf(),
                                          paras.getAlgorithmType()));
            } catch (TpmPrivateKey.Error ex_0) {
                throw new SecurityException("decrypt: Error in decrypt: " + ex_0);
            }
        }