下面列出了java.security.InvalidKeyException#printStackTrace ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* create by: iizvv
* description: 获取Token
* create time: 2019-06-29 15:14
*
* @return 请求头
*/
static Map getToken(String p8, String iss, String kid) {
String s = p8.
replace("-----BEGIN PRIVATE KEY-----", "").
replace("-----END PRIVATE KEY-----", "");
byte[] encodeKey = Base64.decode(s);
String token = null;
try {
token = Jwts.builder().
setHeaderParam(JwsHeader.ALGORITHM, "ES256").
setHeaderParam(JwsHeader.KEY_ID,kid).
setHeaderParam(JwsHeader.TYPE, "JWT").
setIssuer(iss).
claim("exp", System.currentTimeMillis()/1000 + 60 * 10).
setAudience("appstoreconnect-v1").
signWith(SignatureAlgorithm.ES256, new ECPrivateKeyImpl(encodeKey)).
compact();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
Map map = new HashMap();
map.put("Content-Type", "application/json");
map.put("Authorization", "Bearer " + token);
return map;
}
/**
* Serialization read ... PKCS#8 keys serialize as
* themselves, and they're parsed when they get read back.
*/
@java.io.Serial
private void readObject (ObjectInputStream stream)
throws IOException {
try {
decode(stream);
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("deserialized key is invalid: " +
e.getMessage());
}
}
/**
* hmac HmacSHA256
*
* @param message 信息
* @param secret 秘钥
* @return 加密结果
*/
public static String hmacSha256(String message, String secret) {
Exception exception = null;
try {
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
mac.init(secretKeySpec);
return HexUtil.hexEncode(mac.doFinal(message.getBytes()), false);
} catch (NoSuchAlgorithmException ignored) {
} catch (InvalidKeyException e) {
e.printStackTrace();
exception = e;
}
throw new ExchangeException("hmacSha256 error.", exception);
}
@Override
public void onBindViewHolder(@NonNull ContactHolder holder, final int position) {
Contact currentContact = contacts.get(position);
holder.tvName.setText(currentContact.getFname() + " " + currentContact.getLname());
holder.tvWebsite.setText(currentContact.getWebsite());
holder.tvName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
index = position;
notifyDataSetChanged();
}
});
if(index == position){
HomeFragment.selected_id = currentContact.getId();
HomeFragment.selected_fname = currentContact.getFname();
HomeFragment.selected_lname = currentContact.getLname();
try {
HomeFragment.selected_phonenumber = CreateContactFragment.decrypt(currentContact.getPhonenumber());
HomeFragment.selected_email = CreateContactFragment.decrypt(currentContact.getEmail());
} catch (InvalidKeyException e) {
e.printStackTrace();
}
HomeFragment.selected_website = currentContact.getWebsite();
HomeFragment.selected_location = currentContact.getLocation();
holder.tvName.setTextColor(Color.parseColor("#0033cc"));
}else {
holder.tvName.setTextColor(Color.parseColor("#000000"));
}
}
/**
* Serialization read ... PKCS#8 keys serialize as
* themselves, and they're parsed when they get read back.
*/
private void readObject (ObjectInputStream stream)
throws IOException {
try {
decode(stream);
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("deserialized key is invalid: " +
e.getMessage());
}
}
/**
* Serialization read ... PKCS#8 keys serialize as
* themselves, and they're parsed when they get read back.
*/
private void readObject (ObjectInputStream stream)
throws IOException {
try {
decode(stream);
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("deserialized key is invalid: " +
e.getMessage());
}
}
/**
* Serialization read ... X.509 keys serialize as
* themselves, and they're parsed when they get read back.
*/
private void readObject(ObjectInputStream stream) throws IOException {
try {
decode(stream);
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("deserialized key is invalid: " +
e.getMessage());
}
}
/**
* Serialization read ... PKCS#8 keys serialize as
* themselves, and they're parsed when they get read back.
*/
private void readObject (ObjectInputStream stream)
throws IOException {
try {
decode(stream);
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("deserialized key is invalid: " +
e.getMessage());
}
}
/**
* Serialization read ... X.509 keys serialize as
* themselves, and they're parsed when they get read back.
*/
private void readObject(ObjectInputStream stream) throws IOException {
try {
decode(stream);
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("deserialized key is invalid: " +
e.getMessage());
}
}
/**
* Serialization read ... PKCS#8 keys serialize as
* themselves, and they're parsed when they get read back.
*/
private void readObject (ObjectInputStream stream)
throws IOException {
try {
decode(stream);
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("deserialized key is invalid: " +
e.getMessage());
}
}
/**
* Serialization read ... PKCS#8 keys serialize as
* themselves, and they're parsed when they get read back.
*/
private void readObject (ObjectInputStream stream)
throws IOException {
try {
decode(stream);
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("deserialized key is invalid: " +
e.getMessage());
}
}
protected long doBatch(long numBytes) throws InterruptedException {
long numIterations = java.lang.Math.round((double)numBytes/data.length);
try{
for (long i = 0; i < numIterations; i++) {
mac.init(key);
byte[] result = mac.doFinal(data);
}
} catch (InvalidKeyException e){
e.printStackTrace();
}
return numIterations*data.length;
}
/**
* 获取ASN.1 格式公钥编码
* @param n N
* @param e E
* @return 公钥编码
* @throws CspException
*/
private static byte[] getPublicDer(BigInteger n, BigInteger e) throws CspException{
try {
RSAPublicKeyImpl rsapublickeyimpl = new RSAPublicKeyImpl(n, e);
return rsapublickeyimpl.getEncoded();
}catch( InvalidKeyException ex){
ex.printStackTrace();
String err = String.format("[JC_PKCS]:InvalidKeyException ErrMessage: %s", ex.getMessage());
setLoggerErr(err, 0);
throw new CspException(err, ex.getCause());
}
}
private static Mac initialize(final byte[] byteKey) {
final Mac hmacSha512 = getInstance(HMAC_SHA512);
final SecretKeySpec keySpec = new SecretKeySpec(byteKey, HMAC_SHA512);
try {
hmacSha512.init(keySpec);
} catch (InvalidKeyException e) {
e.printStackTrace();
}
return hmacSha512;
}
/**
* Serialization read ... X.509 keys serialize as
* themselves, and they're parsed when they get read back.
*/
private void readObject(ObjectInputStream stream) throws IOException {
try {
decode(stream);
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("deserialized key is invalid: " +
e.getMessage());
}
}
/**
* Serialization read ... PKCS#8 keys serialize as
* themselves, and they're parsed when they get read back.
*/
private void readObject (ObjectInputStream stream)
throws IOException {
try {
decode(stream);
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("deserialized key is invalid: " +
e.getMessage());
}
}
private static byte[][] deriveSRTPKeys(Cipher AEScipher, long index, long keyDerivationRate, byte[] masterSalt, byte[] masterKey) throws UnsupportedEncodingException
{
byte[] iv = new byte[16];
byte[] encKey = new byte[16];
byte[] authKey = new byte[20];
byte[] saltKey = new byte[14];
byte[][] ret = new byte[3][];
long label = 0;
// compute the session encryption key
computeIv(iv, label, index, keyDerivationRate, masterSalt);
SecretKey encryptionKey = new SecretKeySpec(masterKey, 0, 16, "AES");
try
{
AEScipher.init(Cipher.ENCRYPT_MODE, encryptionKey);
}
catch (InvalidKeyException e1)
{
e1.printStackTrace();
}
getCipherStream(AEScipher, encKey, 16, iv);
label = 0x01;
computeIv(iv, label, index, keyDerivationRate, masterSalt);
getCipherStream(AEScipher, authKey, 20, iv);
label = 0x02;
computeIv(iv, label, index, keyDerivationRate, masterSalt);
getCipherStream(AEScipher, saltKey, 14, iv);
ret[0] = encKey;
ret[1] = authKey;
ret[2] = saltKey;
return ret;
}
/**
* Serialization read ... X.509 keys serialize as
* themselves, and they're parsed when they get read back.
*/
private void readObject(ObjectInputStream stream) throws IOException {
try {
decode(stream);
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("deserialized key is invalid: " +
e.getMessage());
}
}
/**
* Serialization read ... PKCS#8 keys serialize as
* themselves, and they're parsed when they get read back.
*/
private void readObject (ObjectInputStream stream)
throws IOException {
try {
decode(stream);
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("deserialized key is invalid: " +
e.getMessage());
}
}
/**
* Serialization read ... X.509 keys serialize as
* themselves, and they're parsed when they get read back.
*/
@java.io.Serial
private void readObject(ObjectInputStream stream) throws IOException {
try {
decode(stream);
} catch (InvalidKeyException e) {
e.printStackTrace();
throw new IOException("deserialized key is invalid: " +
e.getMessage());
}
}