本文整理汇总了Java中org.bouncycastle.util.Arrays.copyOfRange方法的典型用法代码示例。如果您正苦于以下问题:Java Arrays.copyOfRange方法的具体用法?Java Arrays.copyOfRange怎么用?Java Arrays.copyOfRange使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.util.Arrays
的用法示例。
在下文中一共展示了Arrays.copyOfRange方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: EncryptAes256
import org.bouncycastle.util.Arrays; //导入方法依赖的package包/类
private static byte[] EncryptAes256(byte[] data, byte[] encryptionKey)
{
try {
KeyParameter keyParam = new KeyParameter(encryptionKey);
BlockCipherPadding padding = new PKCS7Padding();
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
new CBCBlockCipher(new AESEngine()), padding);
cipher.reset();
cipher.init(true, keyParam);
byte[] buffer = new byte[cipher.getOutputSize(data.length)];
int len = cipher.processBytes(data, 0, data.length, buffer, 0);
len += cipher.doFinal(buffer, len);
return Arrays.copyOfRange(buffer, 0, len);
} catch (Exception e) {
throw new RuntimeException("decrypt error in SimpleAesManaged", e);
}
}
示例2: verifyUserSig
import org.bouncycastle.util.Arrays; //导入方法依赖的package包/类
@Override
public boolean verifyUserSig(String identifier, String sig)throws QCloudException {
try {
Security.addProvider(new BouncyCastleProvider());
//DeBaseUrl64 urlSig to json
Base64 decoder = new Base64();
byte [] compressBytes = Base64Url.base64DecodeUrl(sig.getBytes(Charset.forName("UTF-8")));
//Decompression
Inflater decompression = new Inflater();
decompression.setInput(compressBytes, 0, compressBytes.length);
byte [] decompressBytes = new byte [1024];
int decompressLength = decompression.inflate(decompressBytes);
decompression.end();
String jsonString = new String(Arrays.copyOfRange(decompressBytes, 0, decompressLength));
//Get TLS.Sig from json
JSONObject jsonObject= JSON.parseObject(jsonString);
String sigTLS = jsonObject.getString("TLS.sig");
//debase64 TLS.Sig to get serailString
byte[] signatureBytes = decoder.decode(sigTLS.getBytes(Charset.forName("UTF-8")));
String strSdkAppid = jsonObject.getString("TLS.sdk_appid");
String sigTime = jsonObject.getString("TLS.time");
String sigExpire = jsonObject.getString("TLS.expire_after");
if (!imConfig.getSdkAppId().equals(strSdkAppid))
{
return false;
}
if ( System.currentTimeMillis()/1000 - Long.parseLong(sigTime) > Long.parseLong(sigExpire)) {
return false;
}
//Get Serial String from json
String SerialString =
"TLS.appid_at_3rd:" + 0 + "\n" +
"TLS.account_type:" + 0 + "\n" +
"TLS.identifier:" + identifier + "\n" +
"TLS.sdk_appid:" + imConfig.getSdkAppId() + "\n" +
"TLS.time:" + sigTime + "\n" +
"TLS.expire_after:" + sigExpire + "\n";
Reader reader = new CharArrayReader(imConfig.getPublicKey().toCharArray());
PEMParser parser = new PEMParser(reader);
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
Object obj = parser.readObject();
parser.close();
PublicKey pubKeyStruct = converter.getPublicKey((SubjectPublicKeyInfo) obj);
Signature signature = Signature.getInstance("SHA256withECDSA","BC");
signature.initVerify(pubKeyStruct);
signature.update(SerialString.getBytes(Charset.forName("UTF-8")));
return signature.verify(signatureBytes);
}catch (Exception e) {
throw new QCloudException(e);
}
}
示例3: unpackBigNumber
import org.bouncycastle.util.Arrays; //导入方法依赖的package包/类
public BigInteger unpackBigNumber() throws IOException {
ImmutableValue value = this.unpackValue();
if (!value.isExtensionValue())
throw new RuntimeException("Expected extension value");
ExtensionValue extValue = value.asExtensionValue();
if (extValue.getType() != 0)
throw new RuntimeException("Expected bignum value");
byte[] data = extValue.getData();
byte sign = data[0];
if (sign != '+' && sign != '-')
throw new RuntimeException("Sign was not pos or neg");
BigInteger bn = new BigInteger(sign == '+' ? 1 : -1, Arrays.copyOfRange(data, 1, data.length));
return bn;
}
示例4: decodeCiphertext
import org.bouncycastle.util.Arrays; //导入方法依赖的package包/类
public byte[] decodeCiphertext(long seqNo, short type, byte[] ciphertext, int offset, int len)
throws IOException
{
int macSize = readMac.getSize();
if (len < macSize)
{
throw new TlsFatalAlert(AlertDescription.decode_error);
}
byte[] deciphered = new byte[len];
decryptCipher.processBytes(ciphertext, offset, len, deciphered, 0);
int macInputLen = len - macSize;
byte[] receivedMac = Arrays.copyOfRange(deciphered, macInputLen, len);
byte[] computedMac = readMac.calculateMac(seqNo, type, deciphered, 0, macInputLen);
if (!Arrays.constantTimeAreEqual(receivedMac, computedMac))
{
throw new TlsFatalAlert(AlertDescription.bad_record_mac);
}
return Arrays.copyOfRange(deciphered, 0, macInputLen);
}
示例5: unwrapFromResponse
import org.bouncycastle.util.Arrays; //导入方法依赖的package包/类
public byte[] unwrapFromResponse(final byte[] bytes) {
// caution: placeholder bytes' end-pos must less than 200
String fuzzyHeader = new String(Arrays.copyOfRange(bytes, 0, 200));
if (!fuzzyHeader.startsWith(Http.VERSION_1_1)) {
throw new RuntimeException("unknown format");
}
fuzzyHeader = fuzzyHeader.substring(fuzzyHeader.indexOf("Content-Length: ") + "Content-Length: ".length());
fuzzyHeader = fuzzyHeader.substring(0, fuzzyHeader.indexOf(Http.CRLF));
int rawLen = Integer.parseInt(fuzzyHeader);
return Arrays.copyOfRange(bytes, bytes.length - rawLen, bytes.length);
}
示例6: base64EncodeUrl
import org.bouncycastle.util.Arrays; //导入方法依赖的package包/类
public static byte[] base64EncodeUrl(byte[] in_str) {
byte[] out_str = new byte[1024];
int out_current = 0;
int current = 0;
int length = in_str.length;
while (length > 2) { /* keep going until we have less than 24 bits */
out_str[out_current++] = base64_table_url[unsignedToBytes((unsignedToBytes(in_str[current]) >>> 2))];
out_str[out_current++] = base64_table_url[unsignedToBytes(unsignedToBytes(unsignedToBytes(in_str[current]) & 0x03) << 4)
+ unsignedToBytes((unsignedToBytes(in_str[current + 1]) >>> 4))];
out_str[out_current++] = base64_table_url[(unsignedToBytes((unsignedToBytes(in_str[current + 1]) & 0x0f)) << 2)
+ unsignedToBytes((unsignedToBytes(in_str[current + 2]) >>> 6))];
out_str[out_current++] = base64_table_url[unsignedToBytes((unsignedToBytes(in_str[current + 2]) & 0x3f))];
current += 3;
length -= 3; /* we just handle 3 octets of data */
}
/* now deal with the tail end of things */
if (length != 0) {
out_str[out_current++] = base64_table_url[unsignedToBytes(in_str[current]) >>> 2];
if (length > 1) {
out_str[out_current++] = base64_table_url[unsignedToBytes((unsignedToBytes(in_str[current]) & 0x03) << 4)
+ unsignedToBytes(unsignedToBytes(in_str[current + 1]) >>> 4)];
out_str[out_current++] = base64_table_url[unsignedToBytes((unsignedToBytes(in_str[current + 1]) & 0x0f) << 2)];
out_str[out_current++] = base64_pad_url;
} else {
out_str[out_current++] = base64_table_url[unsignedToBytes((unsignedToBytes(in_str[current]) & 0x03) << 4)];
out_str[out_current++] = base64_pad_url;
out_str[out_current++] = base64_pad_url;
}
}
// System.out.println("length in base64EncodeUrl: " + out_current );
byte[] out_bytes = new String(out_str).getBytes();
return Arrays.copyOfRange(out_bytes, 0, out_current);
}
示例7: encodeMod3Tight
import org.bouncycastle.util.Arrays; //导入方法依赖的package包/类
/**
* Encodes an <code>int</code> array whose elements are between <code>-1</code> and <code>1</code>, to a byte array.
*
* @return the encoded array
*/
public static byte[] encodeMod3Tight(int[] intArray)
{
BigInteger sum = BigInteger.ZERO;
for (int i = intArray.length - 1; i >= 0; i--)
{
sum = sum.multiply(BigInteger.valueOf(3));
sum = sum.add(BigInteger.valueOf(intArray[i] + 1));
}
int size = (BigInteger.valueOf(3).pow(intArray.length).bitLength() + 7) / 8;
byte[] arr = sum.toByteArray();
if (arr.length < size)
{
// pad with leading zeros so arr.length==size
byte[] arr2 = new byte[size];
System.arraycopy(arr, 0, arr2, size - arr.length, arr.length);
return arr2;
}
if (arr.length > size)
// drop sign bit
{
arr = Arrays.copyOfRange(arr, 1, arr.length);
}
return arr;
}
示例8: toBinary3Tight
import org.bouncycastle.util.Arrays; //导入方法依赖的package包/类
/**
* Converts a polynomial with ternary coefficients to binary.
*
* @return the encoded polynomial
*/
public byte[] toBinary3Tight()
{
BigInteger sum = Constants.BIGINT_ZERO;
for (int i = coeffs.length - 1; i >= 0; i--)
{
sum = sum.multiply(BigInteger.valueOf(3));
sum = sum.add(BigInteger.valueOf(coeffs[i] + 1));
}
int size = (BigInteger.valueOf(3).pow(coeffs.length).bitLength() + 7) / 8;
byte[] arr = sum.toByteArray();
if (arr.length < size)
{
// pad with leading zeros so arr.length==size
byte[] arr2 = new byte[size];
System.arraycopy(arr, 0, arr2, size - arr.length, arr.length);
return arr2;
}
if (arr.length > size)
// drop sign bit
{
arr = Arrays.copyOfRange(arr, 1, arr.length);
}
return arr;
}
示例9: encodePlaintext
import org.bouncycastle.util.Arrays; //导入方法依赖的package包/类
public byte[] encodePlaintext(long seqNo, short type, byte[] plaintext, int offset, int len)
throws IOException
{
if (writeMac == null)
{
return Arrays.copyOfRange(plaintext, offset, offset + len);
}
byte[] mac = writeMac.calculateMac(seqNo, type, plaintext, offset, len);
byte[] ciphertext = new byte[len + mac.length];
System.arraycopy(plaintext, offset, ciphertext, 0, len);
System.arraycopy(mac, 0, ciphertext, len, mac.length);
return ciphertext;
}
示例10: decodeCiphertext
import org.bouncycastle.util.Arrays; //导入方法依赖的package包/类
public byte[] decodeCiphertext(long seqNo, short type, byte[] ciphertext, int offset, int len)
throws IOException
{
if (readMac == null)
{
return Arrays.copyOfRange(ciphertext, offset, offset + len);
}
int macSize = readMac.getSize();
if (len < macSize)
{
throw new TlsFatalAlert(AlertDescription.decode_error);
}
int macInputLen = len - macSize;
byte[] receivedMac = Arrays.copyOfRange(ciphertext, offset + macInputLen, offset + len);
byte[] computedMac = readMac.calculateMac(seqNo, type, ciphertext, offset, macInputLen);
if (!Arrays.constantTimeAreEqual(receivedMac, computedMac))
{
throw new TlsFatalAlert(AlertDescription.bad_record_mac);
}
return Arrays.copyOfRange(ciphertext, offset, offset + macInputLen);
}
示例11: base64DecodeUrl
import org.bouncycastle.util.Arrays; //导入方法依赖的package包/类
public static byte[] base64DecodeUrl(byte[] in_str) {
// const unsigned char *current = in_str;
int ch, i = 0, j = 0, k;
int current = 0;
byte[] out_str = new byte[1024];
int length = in_str.length;
/* this sucks for threaded environments */
/* run through the whole string, converting as we go */
// while ((ch = in_str[current++]) != '\0' && length-- > 0) {
ch = in_str[0];
while (length-- > 0) {
ch = in_str[current++];
if (ch == base64_pad_url)
break;
/*
* When Base64 gets POSTed, all pluses are interpreted as spaces.
* This line changes them back. It's not exactly the Base64 spec,
* but it is completely compatible with it (the spec says that
* spaces are invalid). This will also save many people considerable
* headache. - Turadg Aleahmad <[email protected]>
*/
if (ch == ' ')
ch = '*'; // never using '+'
ch = base64_reverse_table_url[ch];
if (ch < 0)
continue;
switch (i % 4) {
case 0:
out_str[j] = (byte) unsignedToBytes(unsignedToBytes(ch) << 2);
break;
case 1:
out_str[j++] |= (byte) unsignedToBytes(unsignedToBytes(ch) >>> 4);
out_str[j] = (byte) unsignedToBytes(unsignedToBytes(unsignedToBytes(ch) & 0x0f) << 4);
break;
case 2:
out_str[j++] |= (byte) unsignedToBytes(unsignedToBytes(ch) >>> 2);
out_str[j] = (byte) unsignedToBytes(unsignedToBytes(unsignedToBytes(ch) & 0x03) << 6);
break;
case 3:
out_str[j++] |= (byte) unsignedToBytes(ch);
break;
}
i++;
}
k = j;
/* mop things up if we ended on a boundary */
if (ch == base64_pad_url) {
switch (i % 4) {
case 0:
case 1:
byte[] error = new byte[1];
error[0] = '\0';
return error;
case 2:
k++;
case 3:
out_str[k++] = 0;
}
}
return Arrays.copyOfRange(out_str, 0, j);
}
示例12: processBlock
import org.bouncycastle.util.Arrays; //导入方法依赖的package包/类
public byte[] processBlock(
byte[] in,
int inOff,
int inLen)
throws InvalidCipherTextException
{
if (forEncryption)
{
if (keyPairGenerator != null)
{
EphemeralKeyPair ephKeyPair = keyPairGenerator.generate();
this.privParam = ephKeyPair.getKeyPair().getPrivate();
this.V = ephKeyPair.getEncodedPublicKey();
}
}
else
{
if (keyParser != null)
{
ByteArrayInputStream bIn = new ByteArrayInputStream(in, inOff, inLen);
try
{
this.pubParam = keyParser.readKey(bIn);
}
catch (IOException e)
{
throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.getMessage(), e);
}
int encLength = (inLen - bIn.available());
this.V = Arrays.copyOfRange(in, inOff, inOff + encLength);
}
}
// Compute the common value and convert to byte array.
agree.init(privParam);
BigInteger z = agree.calculateAgreement(pubParam);
byte[] Z = BigIntegers.asUnsignedByteArray(agree.getFieldSize(), z);
// Create input to KDF.
byte[] VZ;
if (V.length != 0)
{
VZ = new byte[V.length + Z.length];
System.arraycopy(V, 0, VZ, 0, V.length);
System.arraycopy(Z, 0, VZ, V.length, Z.length);
}
else
{
VZ = Z;
}
// Initialise the KDF.
KDFParameters kdfParam = new KDFParameters(VZ, param.getDerivationV());
kdf.init(kdfParam);
return forEncryption
? encryptBlock(in, inOff, inLen)
: decryptBlock(in, inOff, inLen);
}
示例13: decodeCiphertext
import org.bouncycastle.util.Arrays; //导入方法依赖的package包/类
public byte[] decodeCiphertext(long seqNo, short type, byte[] ciphertext, int offset, int len)
throws IOException
{
int blockSize = decryptCipher.getBlockSize();
int macSize = readMac.getSize();
int minLen = Math.max(blockSize, macSize + 1);
if (useExplicitIV)
{
minLen += blockSize;
}
if (len < minLen)
{
throw new TlsFatalAlert(AlertDescription.decode_error);
}
if (len % blockSize != 0)
{
throw new TlsFatalAlert(AlertDescription.decryption_failed);
}
if (useExplicitIV)
{
decryptCipher.init(false, new ParametersWithIV(null, ciphertext, offset, blockSize));
offset += blockSize;
len -= blockSize;
}
for (int i = 0; i < len; i += blockSize)
{
decryptCipher.processBlock(ciphertext, offset + i, ciphertext, offset + i);
}
// If there's anything wrong with the padding, this will return zero
int totalPad = checkPaddingConstantTime(ciphertext, offset, len, blockSize, macSize);
int macInputLen = len - totalPad - macSize;
byte[] decryptedMac = Arrays.copyOfRange(ciphertext, offset + macInputLen, offset + macInputLen + macSize);
byte[] calculatedMac = readMac.calculateMacConstantTime(seqNo, type, ciphertext, offset, macInputLen, len
- macSize, randomData);
boolean badMac = !Arrays.constantTimeAreEqual(calculatedMac, decryptedMac);
if (badMac || totalPad == 0)
{
throw new TlsFatalAlert(AlertDescription.bad_record_mac);
}
return Arrays.copyOfRange(ciphertext, offset, offset + macInputLen);
}
示例14: TlsAEADCipher
import org.bouncycastle.util.Arrays; //导入方法依赖的package包/类
public TlsAEADCipher(TlsContext context, AEADBlockCipher clientWriteCipher, AEADBlockCipher serverWriteCipher,
int cipherKeySize, int macSize)
throws IOException
{
if (!ProtocolVersion.TLSv12.isEqualOrEarlierVersionOf(context.getServerVersion().getEquivalentTLSVersion()))
{
throw new TlsFatalAlert(AlertDescription.internal_error);
}
this.context = context;
this.macSize = macSize;
// NOTE: Valid for RFC 5288 ciphers but may need review for other AEAD ciphers
this.nonce_explicit_length = 8;
// TODO SecurityParameters.fixed_iv_length
int fixed_iv_length = 4;
int key_block_size = (2 * cipherKeySize) + (2 * fixed_iv_length);
byte[] key_block = TlsUtils.calculateKeyBlock(context, key_block_size);
int offset = 0;
KeyParameter client_write_key = new KeyParameter(key_block, offset, cipherKeySize);
offset += cipherKeySize;
KeyParameter server_write_key = new KeyParameter(key_block, offset, cipherKeySize);
offset += cipherKeySize;
byte[] client_write_IV = Arrays.copyOfRange(key_block, offset, offset + fixed_iv_length);
offset += fixed_iv_length;
byte[] server_write_IV = Arrays.copyOfRange(key_block, offset, offset + fixed_iv_length);
offset += fixed_iv_length;
if (offset != key_block_size)
{
throw new TlsFatalAlert(AlertDescription.internal_error);
}
KeyParameter encryptKey, decryptKey;
if (context.isServer())
{
this.encryptCipher = serverWriteCipher;
this.decryptCipher = clientWriteCipher;
this.encryptImplicitNonce = server_write_IV;
this.decryptImplicitNonce = client_write_IV;
encryptKey = server_write_key;
decryptKey = client_write_key;
}
else
{
this.encryptCipher = clientWriteCipher;
this.decryptCipher = serverWriteCipher;
this.encryptImplicitNonce = client_write_IV;
this.decryptImplicitNonce = server_write_IV;
encryptKey = client_write_key;
decryptKey = server_write_key;
}
byte[] dummyNonce = new byte[fixed_iv_length + nonce_explicit_length];
this.encryptCipher.init(true, new AEADParameters(encryptKey, 8 * macSize, dummyNonce));
this.decryptCipher.init(false, new AEADParameters(decryptKey, 8 * macSize, dummyNonce));
}