private void saveIvToFile(final byte[] iv) { final String toWrite = Base64.toBase64String(iv); //this.preferences.edit().putString(IV, toWrite).apply(); }
@Override public String write(byte[] value) throws Exception { return Base64.toBase64String(value); }
public static String Base64ToString(byte[] data) { return Base64.toBase64String(data); }
@Override public String write(UUID value) throws Exception { if(value == null) { return Base64.toBase64String(new byte[0]); } return Base64.toBase64String(ByteUtils.uuidToBytes(value)); }
private String annotateRequest(byte[] data) { int i = 0; StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); // pw.print("-----BEGIN CERTIFICATE REQUEST-----\n"); do { if (i + 48 < data.length) { pw.print(Base64.toBase64String(data, i, 48)); i += 48; } else { pw.print(Base64.toBase64String(data, i, data.length - i)); i = data.length; } pw.print('\n'); } while (i < data.length); // pw.print("-----END CERTIFICATE REQUEST-----\n"); pw.flush(); return sw.toString(); }
public static String toBase64String( byte[] data, int off, int length) { byte[] encoded = encode(data, off, length); return Strings.fromByteArray(encoded); }
/** * <p>Get an SSH key from the compressed EC public key in base64</p> * * @param publicKey The ecdsa-sha2-nistp256 EC public key * * @return An ssh key-only base64 format of public key from given EC public key */ public static String decompressSSHKeyFromNistp256(ECPublicKey publicKey) { ByteBuffer buffer = ByteBuffer.allocate(104); buffer.putInt(KEY_PREFIX.getBytes(Charsets.UTF_8).length); buffer.put(KEY_PREFIX.getBytes(Charsets.UTF_8)); buffer.putInt(CURVE_NAME.getBytes(Charsets.UTF_8).length); buffer.put(CURVE_NAME.getBytes(Charsets.UTF_8)); byte[] octet = {(byte) 0x04}; // this is special byte for SSH byte[] x = publicKey.getW().getAffineX().toByteArray(); // get X, Y cords of ECPoint byte[] y = publicKey.getW().getAffineY().toByteArray(); byte[] x32 = ByteUtils.subArray(x, x.length - 32, x.length); //get last 32 bytes byte[] y32 = ByteUtils.subArray(y, y.length - 32, y.length); // Ignore the y32 warning here in Intellij - it's just a naming mismatch in parameters byte[] data = ByteUtils.concatenate(octet, ByteUtils.concatenate(x32, y32)); buffer.putInt(data.length); buffer.put(data); return Base64.toBase64String(buffer.array()); }
/** * <p> * Get an SSH key from the compressed EC public key in base64</p> * * @param publicKey The ecdsa-sha2-nistp256 EC public key * * @return An ssh key-only base64 format of public key from given EC public * key */ public static String serializeSSHKeyFromNistp256(ECPublicKey publicKey) { ByteBuffer buffer = ByteBuffer.allocate(104); buffer.putInt(NISTP256_KEY_PREFIX.getBytes(Charsets.UTF_8).length); buffer.put(NISTP256_KEY_PREFIX.getBytes(Charsets.UTF_8)); buffer.putInt(NISTP256_CURVE_NAME.getBytes(Charsets.UTF_8).length); buffer.put(NISTP256_CURVE_NAME.getBytes(Charsets.UTF_8)); byte[] octet = {(byte) 0x04}; // this is special byte for SSH byte[] x = publicKey.getW().getAffineX().toByteArray(); // get X, Y cords of ECPoint byte[] y = publicKey.getW().getAffineY().toByteArray(); byte[] x32 = ByteUtils.subArray(x, x.length - 32, x.length); //get last 32 bytes byte[] y32 = ByteUtils.subArray(y, y.length - 32, y.length); byte[] data = ByteUtils.concatenate(octet, ByteUtils.concatenate(x32, y32)); buffer.putInt(data.length); buffer.put(data); return Base64.toBase64String(buffer.array()); }
public static String serializeSSHKeyFromEd25519(byte[] pubKey) { byte[] pubKeyWorking = ByteUtils.clone(pubKey); if (pubKeyWorking[0] == 0x00) { pubKeyWorking = ByteUtils.subArray(pubKeyWorking, 1); //strip the first byte } byte[] keyTypeFrame = AgentUtils.frameArray(ED25519_KEY_PREFIX.getBytes(Charsets.UTF_8)); byte[] pubKeyFrame = AgentUtils.frameArray(pubKeyWorking); String serializedKey = Base64.toBase64String(ByteUtils.concatenate(keyTypeFrame, pubKeyFrame)); // easier than nistp256, we do not need to uncompress pubkey to x and y, we just send it compressed as device provided return ED25519_KEY_PREFIX + " " + serializedKey; }
public static boolean verifyMessage(PublicKey pubkey, MasternodeSignature vchSig, String strMessage, StringBuilder errorMessage) { //int length = Utils.BITCOIN_SIGNED_MESSAGE_HEADER.length()+strMessage.length(); //byte dataToHash [] = (Utils.BITCOIN_SIGNED_MESSAGE_HEADER_BYTES+strMessage).getBytes(); ECKey pubkey2 = null; try { //pubkey2 = PublicKey.recoverCompact(Sha256Hash.twiceOf(dataToHash), vchSig); pubkey2 = ECKey.fromPublicOnly(pubkey.getBytes()); pubkey2.verifyMessage(strMessage.getBytes(), vchSig.getBytes()); //ECKey.verify() //if(DarkCoinSystem.fDebug && !pubkey.getId().equals(pubkey2.getId())) // log.info("DarkSendSigner.verifyMessage -- keys don't match: " + pubkey2.getId().toString()+ " " + pubkey.getId().toString()); //return pubkey.getId().equals(pubkey2.getId()); return true; } catch(SignatureException x) { errorMessage.append("keys don't match - input: "+Utils.HEX.encode(pubkey.getId())); errorMessage.append(", recovered: " + (pubkey2 != null ? Utils.HEX.encode(pubkey2.getPubKeyHash()) : "null")); errorMessage.append(",\nmessage: "+ String.valueOf(strMessage)); errorMessage.append(", sig: \n" + Base64.toBase64String(vchSig.getBytes())+ "\n" + x.getMessage()); return false; } } public static boolean verifyMessage1(PublicKey pubkey, MasternodeSignature vchSig, byte[] message, StringBuilder errorMessage)
public static boolean verifyHash(Sha256Hash hash, byte [] pubkeyId, MasternodeSignature vchSig, StringBuilder strErrorRet) { ECKey pubkeyFromSig; try { pubkeyFromSig = ECKey.signedMessageToKey(hash, vchSig.getBytes()); if (pubkeyFromSig == null) { strErrorRet.append("Error recovering public key."); return false; } if (!Arrays.equals(pubkeyFromSig.getPubKeyHash(), pubkeyId)) { strErrorRet.append(String.format("Keys don't match: pubkey=%s, pubkeyFromSig=%s, hash=%s, vchSig=%s", HEX.encode(pubkeyId), HEX.encode(pubkeyFromSig.getPubKeyHash()), hash.toString(), Base64.toBase64String(vchSig.getBytes()))); return false; } return true; } catch (SignatureException x) { strErrorRet.append("exception: " + x.getMessage()); return false; } }
public ESTRequest onConnection(Source source, ESTRequest request) throws IOException { // // Add challenge password from tls unique // if (source instanceof TLSUniqueProvider && ((TLSUniqueProvider)source).isTLSUniqueAvailable()) { PKCS10CertificationRequestBuilder localBuilder = new PKCS10CertificationRequestBuilder(builder); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] tlsUnique = ((TLSUniqueProvider)source).getTLSUnique(); localBuilder.setAttribute(PKCSObjectIdentifiers.pkcs_9_at_challengePassword, new DERPrintableString(Base64.toBase64String(tlsUnique))); bos.write(annotateRequest(localBuilder.build(contentSigner).getEncoded()).getBytes()); bos.flush(); ESTRequestBuilder reqBuilder = new ESTRequestBuilder(request).withData(bos.toByteArray()); reqBuilder.setHeader("Content-Type", "application/pkcs10"); reqBuilder.setHeader("Content-Transfer-Encoding", "base64"); reqBuilder.setHeader("Content-Length", Long.toString(bos.size())); return reqBuilder.build(); } else { throw new IOException("Source does not supply TLS unique."); } } });
String keyTypeProvided = unframeKeyTypeFromProvidedSSHKey(keyInBytes); Logger.getLogger(SSHAgent.class.getName()).log(Level.FINE, "Server sent challenge: {0}", Base64.toBase64String(challengeData)); Logger.getLogger(SSHAgent.class.getName()).log(Level.FINE, "Effective username: {0}", new String(userName)); Logger.getLogger(SSHAgent.class.getName()).log(Level.FINE, "Effective public key: {0}", Base64.toBase64String(keyInBytes));
String base64Signature = Base64.toBase64String(signature.getSignature());
answer.setHeader("Authorization", "Basic " + Base64.toBase64String(userPass.getBytes()));
String rawTransaction = Base64.toBase64String(rawData); // generate transaction raw data
Base64.toBase64String(sig.getBytes())); Base64.toBase64String(sig.getBytes()));
message = strMessage.getBytes(); log.info("CMasternodeBroadcast::VerifySignature - sanitized strMessage: "+Utils.sanitizeString(strMessage)+", pubKeyCollateralAddress address: "+new Address(params, pubKeyCollateralAddress.getId()).toString()+", sig: %s\n" + Base64.toBase64String(sig.getBytes()));