/** * Generate and encapsulate a random session key. * * @param out the output buffer for the encapsulated key. * @param keyLen the length of the random session key. * @return the random session key. */ public CipherParameters encrypt(byte[] out, int keyLen) { return encrypt(out, 0, keyLen); }
/** * Generate and encapsulate a random session key. * * @param out the output buffer for the encapsulated key. * @param keyLen the length of the session key. * @return the random session key. */ public CipherParameters encrypt(byte[] out, int keyLen) { return encrypt(out, 0, keyLen); }
/** * Decrypt an encapsulated session key. * * @param in the input buffer for the encapsulated key. * @param keyLen the length of the session key. * @return the session key. */ public CipherParameters decrypt(byte[] in, int keyLen) { return decrypt(in, 0, in.length, keyLen); }
ECMultiplier basePointMultiplier = createBasePointMultiplier(); return deriveKey(keyLen, C, PEH);
public static byte[] Decrypt(byte[] prikey, byte[] IV, byte[] key_cxt, byte[] ciphertext, int keylen) { try { com.github.ontio.account.Account account = new com.github.ontio.account.Account(prikey, signatureScheme); ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec((String) curveParaSpec[0]); ECDomainParameters ecDomain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN()); AsymmetricCipherKeyPair keys = new AsymmetricCipherKeyPair( null, new ECPrivateKeyParameters(((BCECPrivateKey) account.getPrivateKey()).getD(), ecDomain)); byte[] out = key_cxt; ECIESKeyEncapsulation kem = new ECIESKeyEncapsulation(new KDF2BytesGenerator(defaultDigest), new SecureRandom()); KeyParameter key1; kem.init(keys.getPrivate()); key1 = (KeyParameter) kem.decrypt(out, keylen); byte[] plaintext; try { Cipher dec = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); Key key = new SecretKeySpec(key1.getKey(), "AES"); dec.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV)); plaintext = dec.doFinal(ciphertext); } catch (Exception e) { throw new Exception("AES failed initialisation - " + e.toString(), e); } return plaintext; } catch (Exception e) { e.printStackTrace(); } return null; }
public static String[] Encrypt(String pubkey, byte[] msg, int keylen) { try { com.github.ontio.account.Account account = new com.github.ontio.account.Account(false, Helper.hexToBytes(pubkey)); ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec((String) curveParaSpec[0]); ECDomainParameters ecDomain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN()); AsymmetricCipherKeyPair keys = new AsymmetricCipherKeyPair( new ECPublicKeyParameters(((BCECPublicKey) account.getPublicKey()).getQ(), ecDomain), null); byte[] out = new byte[(ecDomain.getCurve().getFieldSize() / 8) * 2 + 1]; ECIESKeyEncapsulation kem = new ECIESKeyEncapsulation(new KDF2BytesGenerator(defaultDigest), new SecureRandom()); KeyParameter key1; kem.init(keys.getPublic()); key1 = (KeyParameter) kem.encrypt(out, keylen); //AES key = key1 (is encrypted in out) byte[] IV = Hex.decode(getRandomString(keylen)); //choose random IV of length = keylen byte[] ciphertext; try { Cipher en = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); Key key = new SecretKeySpec(key1.getKey(), "AES"); en.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV)); ciphertext = en.doFinal(msg); } catch (Exception e) { throw new Exception("AES failed initialisation - " + e.toString(), e); } //(IV, out, ciphertext) return new String[]{Helper.toHexString(IV), Helper.toHexString(out), Helper.toHexString(ciphertext)}; } catch (Exception e) { e.printStackTrace(); } return null; }
/** * Decrypt an encapsulated session key. * * @param in the input buffer for the encapsulated key. * @param inOff the offset for the input buffer. * @param inLen the length of the encapsulated key. * @param keyLen the length of the session key. * @return the session key. */ public CipherParameters decrypt(byte[] in, int inOff, int inLen, int keyLen) throws IllegalArgumentException { if (!key.isPrivate()) { throw new IllegalArgumentException("Private key required for decryption"); } BigInteger n = key.getModulus(); BigInteger d = key.getExponent(); // Decode the input byte[] C = new byte[inLen]; System.arraycopy(in, inOff, C, 0, C.length); BigInteger c = new BigInteger(1, C); // Decrypt the ephemeral random and encode it BigInteger r = c.modPow(d, n); return generateKey(n, r, keyLen); }
/** * Decrypt an encapsulated session key. * * @param in the input buffer for the encapsulated key. * @param keyLen the length of the session key. * @return the session key. */ public CipherParameters decrypt(byte[] in, int keyLen) { return decrypt(in, 0, in.length, keyLen); }
return deriveKey(keyLen, C, PEH);
ECMultiplier basePointMultiplier = createBasePointMultiplier(); return deriveKey(keyLen, C, PEH);
public static byte[] Decrypt(byte[] prikey, byte[] IV, byte[] key_cxt, byte[] ciphertext, int keylen) { try { com.github.ontio.account.Account account = new com.github.ontio.account.Account(prikey, signatureScheme); ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec((String) curveParaSpec[0]); ECDomainParameters ecDomain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN()); AsymmetricCipherKeyPair keys = new AsymmetricCipherKeyPair( null, new ECPrivateKeyParameters(((BCECPrivateKey) account.getPrivateKey()).getD(), ecDomain)); byte[] out = key_cxt; ECIESKeyEncapsulation kem = new ECIESKeyEncapsulation(new KDF2BytesGenerator(defaultDigest), new SecureRandom()); KeyParameter key1; kem.init(keys.getPrivate()); key1 = (KeyParameter) kem.decrypt(out, keylen); byte[] plaintext; try { Cipher dec = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); Key key = new SecretKeySpec(key1.getKey(), "AES"); dec.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV)); plaintext = dec.doFinal(ciphertext); } catch (Exception e) { throw new Exception("AES failed initialisation - " + e.toString(), e); } return plaintext; } catch (Exception e) { e.printStackTrace(); } return null; }
public static String[] Encrypt(String pubkey, byte[] msg, int keylen) { try { com.github.ontio.account.Account account = new com.github.ontio.account.Account(false, Helper.hexToBytes(pubkey)); ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec((String) curveParaSpec[0]); ECDomainParameters ecDomain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN()); AsymmetricCipherKeyPair keys = new AsymmetricCipherKeyPair( new ECPublicKeyParameters(((BCECPublicKey) account.getPublicKey()).getQ(), ecDomain), null); byte[] out = new byte[(ecDomain.getCurve().getFieldSize() / 8) * 2 + 1]; ECIESKeyEncapsulation kem = new ECIESKeyEncapsulation(new KDF2BytesGenerator(defaultDigest), new SecureRandom()); KeyParameter key1; kem.init(keys.getPublic()); key1 = (KeyParameter) kem.encrypt(out, keylen); //AES key = key1 (is encrypted in out) byte[] IV = Hex.decode(getRandomString(keylen)); //choose random IV of length = keylen byte[] ciphertext; try { Cipher en = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); Key key = new SecretKeySpec(key1.getKey(), "AES"); en.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV)); ciphertext = en.doFinal(msg); } catch (Exception e) { throw new Exception("AES failed initialisation - " + e.toString(), e); } //(IV, out, ciphertext) return new String[]{Helper.toHexString(IV), Helper.toHexString(out), Helper.toHexString(ciphertext)}; } catch (Exception e) { e.printStackTrace(); } return null; }
/** * Decrypt an encapsulated session key. * * @param in the input buffer for the encapsulated key. * @param inOff the offset for the input buffer. * @param inLen the length of the encapsulated key. * @param keyLen the length of the session key. * @return the session key. */ public CipherParameters decrypt(byte[] in, int inOff, int inLen, int keyLen) throws IllegalArgumentException { if (!key.isPrivate()) { throw new IllegalArgumentException("Private key required for decryption"); } BigInteger n = key.getModulus(); BigInteger d = key.getExponent(); // Decode the input byte[] C = new byte[inLen]; System.arraycopy(in, inOff, C, 0, C.length); BigInteger c = new BigInteger(1, C); // Decrypt the ephemeral random and encode it BigInteger r = c.modPow(d, n); return generateKey(n, r, keyLen); }
/** * Generate and encapsulate a random session key. * * @param out the output buffer for the encapsulated key. * @param keyLen the length of the session key. * @return the random session key. */ public CipherParameters encrypt(byte[] out, int keyLen) { return encrypt(out, 0, keyLen); }
/** * Decrypt an encapsulated session key. * * @param in the input buffer for the encapsulated key. * @param keyLen the length of the session key. * @return the session key. */ public CipherParameters decrypt(byte[] in, int keyLen) { return decrypt(in, 0, in.length, keyLen); }
/** * Generate and encapsulate a random session key. * * @param out the output buffer for the encapsulated key. * @param keyLen the length of the random session key. * @return the random session key. */ public CipherParameters encrypt(byte[] out, int keyLen) { return encrypt(out, 0, keyLen); }
/** * Decrypt an encapsulated session key. * * @param in the input buffer for the encapsulated key. * @param keyLen the length of the session key. * @return the session key. */ public CipherParameters decrypt(byte[] in, int keyLen) { return decrypt(in, 0, in.length, keyLen); }
return deriveKey(keyLen, C, PEH);
/** * Generate and encapsulate a random session key. * * @param out the output buffer for the encapsulated key. * @param outOff the offset for the output buffer. * @param keyLen the length of the random session key. * @return the random session key. */ public CipherParameters encrypt(byte[] out, int outOff, int keyLen) throws IllegalArgumentException { if (key.isPrivate()) { throw new IllegalArgumentException("Public key required for encryption"); } BigInteger n = key.getModulus(); BigInteger e = key.getExponent(); // Generate the ephemeral random and encode it BigInteger r = BigIntegers.createRandomInRange(ZERO, n.subtract(ONE), rnd); // Encrypt the random and encode it BigInteger c = r.modPow(e, n); byte[] C = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, c); System.arraycopy(C, 0, out, outOff, C.length); return generateKey(n, r, keyLen); }
/** * Generate and encapsulate a random session key. * * @param out the output buffer for the encapsulated key. * @param outOff the offset for the output buffer. * @param keyLen the length of the random session key. * @return the random session key. */ public CipherParameters encrypt(byte[] out, int outOff, int keyLen) throws IllegalArgumentException { if (key.isPrivate()) { throw new IllegalArgumentException("Public key required for encryption"); } BigInteger n = key.getModulus(); BigInteger e = key.getExponent(); // Generate the ephemeral random and encode it BigInteger r = BigIntegers.createRandomInRange(ZERO, n.subtract(ONE), rnd); // Encrypt the random and encode it BigInteger c = r.modPow(e, n); byte[] C = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, c); System.arraycopy(C, 0, out, outOff, C.length); return generateKey(n, r, keyLen); }