Refine search
private void generatesDifferentCipherTexts(BytesEncryptor bcEncryptor) { byte[] encrypted1 = bcEncryptor.encrypt(testData); byte[] encrypted2 = bcEncryptor.encrypt(testData); Assert.assertFalse(Arrays.areEqual(encrypted1, encrypted2)); byte[] decrypted1 = bcEncryptor.decrypt(encrypted1); byte[] decrypted2 = bcEncryptor.decrypt(encrypted2); Assert.assertArrayEquals(testData, decrypted1); Assert.assertArrayEquals(testData, decrypted2); }
public byte[] encrypt(String plaintext) throws EncryptionServiceException { try { byte[] newSalt = generateRandomArray(PBKDF2_SALT_SIZE_BYTES); SecretKey key = new SecretKeySpec(generateKey(newSalt), CIPHER); Cipher myCipher = Cipher.getInstance(CIPHERSCHEME); byte[] newNonce = generateRandomArray(GCM_IV_NONCE_SIZE_BYTES); GCMParameterSpec spec = new GCMParameterSpec(GCM_AUTHENTICATION_TAG_SIZE_BITS, newNonce); myCipher.init(Cipher.ENCRYPT_MODE, key, spec); byte[] bytes = plaintext.getBytes(); return Arrays.concatenate(newNonce, newSalt, myCipher.doFinal(bytes)); } catch (Exception e) { logger.error("Encryption failed", e); throw new EncryptionServiceException(e); } }
public byte[] decrypt(byte[] encrypt) throws EncryptionServiceException { try { byte[] myNonce = new byte[GCM_IV_NONCE_SIZE_BYTES]; byte[] mySalt = new byte[PBKDF2_SALT_SIZE_BYTES]; ByteArrayInputStream fileInputStream = new ByteArrayInputStream(encrypt); fileInputStream.read(myNonce); fileInputStream.read(mySalt); SecretKey key = new SecretKeySpec(generateKey(mySalt), CIPHER); Cipher myCipher = Cipher.getInstance(CIPHERSCHEME); GCMParameterSpec spec = new GCMParameterSpec(GCM_AUTHENTICATION_TAG_SIZE_BITS, myNonce); myCipher.init(Cipher.DECRYPT_MODE, key, spec); return myCipher.doFinal(Arrays.copyOfRange(encrypt, GCM_IV_NONCE_SIZE_BYTES + PBKDF2_SALT_SIZE_BYTES, encrypt.length)); } catch (Exception e) { logger.error("Decryption failed", e); throw new EncryptionServiceException(e); } }
protected BigInteger decodeValue(BigInteger n, byte[] buf, int off, int len) { byte[] bs = Arrays.copyOfRange(buf, off, off + len); return checkValue(n, new BigInteger(1, bs)); }
private XMSSMTPublicKey(ASN1Sequence seq) { if (!ASN1Integer.getInstance(seq.getObjectAt(0)).getValue().equals(BigInteger.valueOf(0))) { throw new IllegalArgumentException("unknown version of sequence"); } this.publicSeed = Arrays.clone(DEROctetString.getInstance(seq.getObjectAt(1)).getOctets()); this.root = Arrays.clone(DEROctetString.getInstance(seq.getObjectAt(2)).getOctets()); }
public static ByteArray ecPublicKeyToRaw(ECPublicKey key) { byte[] x = key.getW().getAffineX().toByteArray(); byte[] y = key.getW().getAffineY().toByteArray(); byte[] xPadding = new byte[Math.max(0, 32 - x.length)]; byte[] yPadding = new byte[Math.max(0, 32 - y.length)]; Arrays.fill(xPadding, (byte) 0); Arrays.fill(yPadding, (byte) 0); return new ByteArray(org.bouncycastle.util.Arrays.concatenate( new byte[]{ 0x04 }, org.bouncycastle.util.Arrays.concatenate( xPadding, Arrays.copyOfRange(x, Math.max(0, x.length - 32), x.length) ), org.bouncycastle.util.Arrays.concatenate( yPadding, Arrays.copyOfRange(y, Math.max(0, y.length - 32), y.length) ) )); }
/** * Adds another polynomial which can have a different number of coefficients. * * @param b another polynomial */ public void add(BigIntPolynomial b) { if (b.coeffs.length > coeffs.length) { int N = coeffs.length; coeffs = Arrays.copyOf(coeffs, b.coeffs.length); for (int i = N; i < coeffs.length; i++) { coeffs[i] = Constants.BIGINT_ZERO; } } for (int i = 0; i < b.coeffs.length; i++) { coeffs[i] = coeffs[i].add(b.coeffs[i]); } }
private void encodeValue(BigInteger n, BigInteger x, byte[] buf, int off, int len) { byte[] bs = checkValue(n, x).toByteArray(); int bsOff = Math.max(0, bs.length - len); int bsLen = bs.length - bsOff; int pos = len - bsLen; Arrays.fill(buf, off, off + pos, (byte)0); System.arraycopy(bs, bsOff, buf, off + pos, bsLen); } }
protected void engineInit( byte[] params, String format) throws IOException { if (this.isASN1FormatString(format)) { RC2CBCParameter p = RC2CBCParameter.getInstance(ASN1Primitive.fromByteArray(params)); if (p.getRC2ParameterVersion() != null) { parameterVersion = p.getRC2ParameterVersion().intValue(); } iv = p.getIV(); return; } if (format.equals("RAW")) { engineInit(params); return; } throw new IOException("Unknown parameters format in IV parameters object"); }
/** * Subtracts another polynomial which can have a different number of coefficients. * * @param b another polynomial */ public void sub(BigIntPolynomial b) { if (b.coeffs.length > coeffs.length) { int N = coeffs.length; coeffs = Arrays.copyOf(coeffs, b.coeffs.length); for (int i = N; i < coeffs.length; i++) { coeffs[i] = Constants.BIGINT_ZERO; } } for (int i = 0; i < b.coeffs.length; i++) { coeffs[i] = coeffs[i].subtract(b.coeffs[i]); } }
byte[] alteredCiphertext = Arrays.clone(originalCiphertext); alteredCiphertext[8] = (byte) (alteredCiphertext[8] ^ 0x08); // <<< Change 100$ to 900$
if (! org.bouncycastle.util.Arrays.constantTimeAreEqual(expectedAuthTag, authTag)) {
public HashRefDo(final byte[] data) { if (data == null) hash = new byte[0]; else hash = Arrays.copyOf(data, data.length); }
protected void checkMAC(long seqNo, short type, byte[] recBuf, int recStart, int recEnd, byte[] calcBuf, int calcOff, int calcLen) throws IOException { byte[] receivedMac = Arrays.copyOfRange(recBuf, recStart, recEnd); byte[] computedMac = readMac.calculateMac(seqNo, type, calcBuf, calcOff, calcLen); if (!Arrays.constantTimeAreEqual(receivedMac, computedMac)) { throw new TlsFatalAlert(AlertDescription.bad_record_mac); } }
private static byte[] genPad(byte b, int count) { byte[] padding = new byte[count]; Arrays.fill(padding, b); return padding; } }
private static void checkNamedCurve(int[] namedCurves, int namedCurve) throws IOException { if (namedCurves != null && !Arrays.contains(namedCurves, namedCurve)) { /* * RFC 4492 4. [...] servers MUST NOT negotiate the use of an ECC cipher suite * unless they can complete the handshake while respecting the choice of curves * and compression techniques specified by the client. */ throw new TlsFatalAlert(AlertDescription.illegal_parameter); } }
public static byte[] createSupportedPointFormatsExtension(short[] ecPointFormats) throws IOException { if (ecPointFormats == null || !Arrays.contains(ecPointFormats, ECPointFormat.uncompressed)) { /* * RFC 4492 5.1. If the Supported Point Formats Extension is indeed sent, it MUST * contain the value 0 (uncompressed) as one of the items in the list of point formats. */ // NOTE: We add it at the end (lowest preference) ecPointFormats = Arrays.append(ecPointFormats, ECPointFormat.uncompressed); } return TlsUtils.encodeUint8ArrayWithUint8Length(ecPointFormats); }
public static ECField convertField(FiniteField field) { if (ECAlgorithms.isFpField(field)) { return new ECFieldFp(field.getCharacteristic()); } else //if (ECAlgorithms.isF2mField(curveField)) { Polynomial poly = ((PolynomialExtensionField)field).getMinimalPolynomial(); int[] exponents = poly.getExponentsPresent(); int[] ks = Arrays.reverse(Arrays.copyOfRange(exponents, 1, exponents.length - 1)); return new ECFieldF2m(poly.getDegree(), ks); } }