當前位置: 首頁>>代碼示例>>Java>>正文


Java Base64類代碼示例

本文整理匯總了Java中org.spongycastle.util.encoders.Base64的典型用法代碼示例。如果您正苦於以下問題:Java Base64類的具體用法?Java Base64怎麽用?Java Base64使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Base64類屬於org.spongycastle.util.encoders包,在下文中一共展示了Base64類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: signatureToKeyBytes

import org.spongycastle.util.encoders.Base64; //導入依賴的package包/類
/**
 * Given a piece of text and a message signature encoded in base64, returns an ECKey
 * containing the public key that was used to sign it. This can then be compared to the expected public key to
 * determine if the signature was correct.
 *
 * @param messageHash a piece of human readable text that was signed
 * @param signatureBase64 The Ethereum-format message signature in base64
 *
 * @return -
 * @throws SignatureException If the public key could not be recovered or if there was a signature format error.
 */
public static byte[] signatureToKeyBytes(byte[] messageHash, String signatureBase64) throws SignatureException {
    byte[] signatureEncoded;
    try {
        signatureEncoded = Base64.decode(signatureBase64);
    } catch (RuntimeException e) {
        // This is what you get back from Bouncy Castle if base64 doesn't decode :(
        throw new SignatureException("Could not decode base64", e);
    }
    // Parse the signature bytes into r/s and the selector value.
    if (signatureEncoded.length < 65)
        throw new SignatureException("Signature truncated, expected 65 bytes and got " + signatureEncoded.length);

    return signatureToKeyBytes(
            messageHash,
            ECDSASignature.fromComponents(
                    Arrays.copyOfRange(signatureEncoded, 1, 33),
                    Arrays.copyOfRange(signatureEncoded, 33, 65),
                    (byte) (signatureEncoded[0] & 0xFF)));
}
 
開發者ID:toshiapp,項目名稱:toshi-headless-client,代碼行數:31,代碼來源:ECKey.java

示例2: signatureToKeyBytes

import org.spongycastle.util.encoders.Base64; //導入依賴的package包/類
/**
 * Given a piece of text and a message signature encoded in base64, returns an ECKey
 * containing the public key that was used to sign it. This can then be compared to the expected public key to
 * determine if the signature was correct.
 *
 * @param messageHash a piece of human readable text that was signed
 * @param signatureBase64 The Ethereum-format message signature in base64
 *
 * @return -
 * @throws SignatureException If the public key could not be recovered or if there was a signature format error.
 */
public static byte[] signatureToKeyBytes(byte[] messageHash, String signatureBase64) throws SignatureException {
    byte[] signatureEncoded;
    try {
        signatureEncoded = Base64.decode(signatureBase64);
    } catch (RuntimeException e) {
        // This is what you get back from Bouncy Castle if base64 doesn't decode :(
        throw new SignatureException("Could not decode base64", e);
    }
    // Parse the signature bytes into r/s and the selector value.
    if (signatureEncoded.length < 65)
        throw new SignatureException("Signature truncated, expected 65 bytes and got " + signatureEncoded.length);

    return signatureToKeyBytes(
        messageHash,
        ECDSASignature.fromComponents(
            Arrays.copyOfRange(signatureEncoded, 1, 33),
            Arrays.copyOfRange(signatureEncoded, 33, 65),
            (byte) (signatureEncoded[0] & 0xFF)));
}
 
開發者ID:talentchain,項目名稱:talchain,代碼行數:31,代碼來源:ECKey.java

示例3: signMessage

import org.spongycastle.util.encoders.Base64; //導入依賴的package包/類
/**
 * Signs a text message using the standard Bitcoin messaging signing format and returns the signature as a base64
 * encoded string.
 *
 * @throws IllegalStateException if this ECKey does not have the private part.
 * @throws KeyCrypterException if this ECKey is encrypted and no AESKey is provided or it does not decrypt the ECKey.
 */
public String signMessage(String message, @Nullable KeyParameter aesKey) throws KeyCrypterException {
    byte[] data = Utils.formatMessageForSigning(message);
    Sha256Hash hash = Sha256Hash.twiceOf(data);
    ECDSASignature sig = sign(hash, aesKey);
    // Now we have to work backwards to figure out the recId needed to recover the signature.
    int recId = -1;
    for (int i = 0; i < 4; i++) {
        ECKey k = ECKey.recoverFromSignature(i, sig, hash, isCompressed());
        if (k != null && k.pub.equals(pub)) {
            recId = i;
            break;
        }
    }
    if (recId == -1)
        throw new RuntimeException("Could not construct a recoverable key. This should never happen.");
    int headerByte = recId + 27 + (isCompressed() ? 4 : 0);
    byte[] sigData = new byte[65];  // 1 header + 32 bytes for R + 32 bytes for S
    sigData[0] = (byte)headerByte;
    System.arraycopy(Utils.bigIntegerToBytes(sig.r, 32), 0, sigData, 1, 32);
    System.arraycopy(Utils.bigIntegerToBytes(sig.s, 32), 0, sigData, 33, 32);
    return new String(Base64.encode(sigData), Charset.forName("UTF-8"));
}
 
開發者ID:creativechain,項目名稱:creacoinj,代碼行數:30,代碼來源:ECKey.java

示例4: signMessage

import org.spongycastle.util.encoders.Base64; //導入依賴的package包/類
/**
 * Signs a text message using the standard Bitcoin messaging signing format
 * and returns the signature as a base64 encoded string.
 * 
 * Some blockchains require additional header bytes infront of the message.
 * <code>[24] "Bitcoin Signed Message:\n" [message.length as a varint] message</code>
 * 
 * @param headerBytes
 *            the additional header bytes required by the blockchain.
 * @throws IllegalStateException
 *             if this ECKey does not have the private part.
 * @throws KeyCrypterException
 *             if this ECKey is encrypted and no AESKey is provided or it
 *             does not decrypt the ECKey.
 */
public String signMessage(String message, Charset charset, @Nullable KeyParameter aesKey,
        @Nullable byte[] headerBytes) {
    byte[] data = CryptoUtils.formatMessageForSigning(message, charset, headerBytes);
    Sha256Hash hash = Sha256Hash.twiceOf(data);
    ECDSASignature sig = sign(hash, aesKey);
    // Now we have to work backwards to figure out the recId needed to
    // recover the signature.
    int recId = -1;
    for (int i = 0; i < 4; i++) {
        ECKey k = ECKey.recoverFromSignature(i, sig, hash, isCompressed());
        if (k != null && k.pub.equals(pub)) {
            recId = i;
            break;
        }
    }
    if (recId == -1)
        throw new RuntimeException("Could not construct a recoverable key. This should never happen.");
    int headerByte = recId + 27 + (isCompressed() ? 4 : 0);
    byte[] sigData = new byte[65]; // 1 header + 32 bytes for R + 32 bytes
                                   // for S
    sigData[0] = (byte) headerByte;
    System.arraycopy(CryptoUtils.bigIntegerToBytes(sig.r, 32), 0, sigData, 1, 32);
    System.arraycopy(CryptoUtils.bigIntegerToBytes(sig.s, 32), 0, sigData, 33, 32);
    return new String(Base64.encode(sigData), Charset.forName("UTF-8"));
}
 
開發者ID:marvin-we,項目名稱:crypto-core,代碼行數:41,代碼來源:ECKey.java

示例5: signatureToKeyBytes

import org.spongycastle.util.encoders.Base64; //導入依賴的package包/類
/**
 * Given a piece of text and a message signature encoded in base64, returns an ECKey
 * containing the public key that was used to sign it. This can then be compared to the expected public key to
 * determine if the signature was correct.
 *
 * @param messageHash a piece of human readable text that was signed
 * @param signatureBase64 The Ethereum-format message signature in base64
 *
 * @return -
 * @throws SignatureException If the public key could not be recovered or if there was a signature format error.
 */
public static byte[] signatureToKeyBytes(byte[] messageHash, String signatureBase64)
    throws SignatureException {
  byte[] signatureEncoded;
  try {
    signatureEncoded = Base64.decode(signatureBase64);
  } catch (RuntimeException e) {
    // This is what you get back from Bouncy Castle if base64 doesn't decode :(
    throw new SignatureException("Could not decode base64", e);
  }
  // Parse the signature bytes into r/s and the selector value.
  if (signatureEncoded.length < 65) {
    throw new SignatureException(
        "Signature truncated, expected 65 bytes and got " + signatureEncoded.length);
  }

  return signatureToKeyBytes(messageHash,
      ECDSASignature.fromComponents(Arrays.copyOfRange(signatureEncoded, 1, 33),
          Arrays.copyOfRange(signatureEncoded, 33, 65), (byte) (signatureEncoded[0] & 0xFF)));
}
 
開發者ID:Aptoide,項目名稱:AppCoins-ethereumj,代碼行數:31,代碼來源:ECKey.java

示例6: signMessage

import org.spongycastle.util.encoders.Base64; //導入依賴的package包/類
/**
 * Signs a text message using the standard Bitcoin messaging signing format and returns the signature as a base64
 * encoded string.
 *
 * @throws IllegalStateException if this ECKey does not have the private part.
 */
public String signMessage(String message) {
    byte[] data = Utils.formatMessageForSigning(message);
    Sha256Hash hash = Sha256Hash.twiceOf(data);
    ECDSASignature sig = sign(hash);
    // Now we have to work backwards to figure out the recId needed to recover the signature.
    int recId = -1;
    for (int i = 0; i < 4; i++) {
        BtcECKey k = BtcECKey.recoverFromSignature(i, sig, hash, isCompressed());
        if (k != null && k.pub.equals(pub)) {
            recId = i;
            break;
        }
    }
    if (recId == -1)
        throw new RuntimeException("Could not construct a recoverable key. This should never happen.");
    int headerByte = recId + 27 + (isCompressed() ? 4 : 0);
    byte[] sigData = new byte[65];  // 1 header + 32 bytes for R + 32 bytes for S
    sigData[0] = (byte)headerByte;
    System.arraycopy(Utils.bigIntegerToBytes(sig.r, 32), 0, sigData, 1, 32);
    System.arraycopy(Utils.bigIntegerToBytes(sig.s, 32), 0, sigData, 33, 32);
    return new String(Base64.encode(sigData), Charset.forName("UTF-8"));
}
 
開發者ID:rsksmart,項目名稱:bitcoinj-thin,代碼行數:29,代碼來源:BtcECKey.java

示例7: signMessage

import org.spongycastle.util.encoders.Base64; //導入依賴的package包/類
/**
 * Signs a text message using the standard NithPoints messaging signing format and returns the signature as a base64
 * encoded string.
 *
 * @throws IllegalStateException if this ECKey does not have the private part.
 * @throws KeyCrypterException if this ECKey is encrypted and no AESKey is provided or it does not decrypt the ECKey.
 */
public String signMessage(String message, KeyParameter aesKey) throws KeyCrypterException {
    if (priv == null)
        throw new IllegalStateException("This ECKey does not have the private key necessary for signing.");
    byte[] data = Utils.formatMessageForSigning(message);
    Sha256Hash hash = Sha256Hash.createDouble(data);
    ECDSASignature sig = sign(hash, aesKey);
    // Now we have to work backwards to figure out the recId needed to recover the signature.
    int recId = -1;
    for (int i = 0; i < 4; i++) {
        ECKey k = ECKey.recoverFromSignature(i, sig, hash, isCompressed());
        if (k != null && Arrays.equals(k.pub, pub)) {
            recId = i;
            break;
        }
    }
    if (recId == -1)
        throw new RuntimeException("Could not construct a recoverable key. This should never happen.");
    int headerByte = recId + 27 + (isCompressed() ? 4 : 0);
    byte[] sigData = new byte[65];  // 1 header + 32 bytes for R + 32 bytes for S
    sigData[0] = (byte)headerByte;
    System.arraycopy(Utils.bigIntegerToBytes(sig.r, 32), 0, sigData, 1, 32);
    System.arraycopy(Utils.bigIntegerToBytes(sig.s, 32), 0, sigData, 33, 32);
    return new String(Base64.encode(sigData), Charset.forName("UTF-8"));
}
 
開發者ID:appteam-nith,項目名稱:NithPointsj,代碼行數:32,代碼來源:ECKey.java

示例8: writeEncoded

import org.spongycastle.util.encoders.Base64; //導入依賴的package包/類
private void writeEncoded(byte[] bytes)
    throws IOException
{
    bytes = Base64.encode(bytes);

    for (int i = 0; i < bytes.length; i += buf.length)
    {
        int index = 0;

        while (index != buf.length)
        {
            if ((i + index) >= bytes.length)
            {
                break;
            }
            buf[index] = (char)bytes[i + index];
            index++;
        }
        this.write(buf, 0, index);
        this.newLine();
    }
}
 
開發者ID:MaxSmile,項目名稱:EasyVPN-Free,代碼行數:23,代碼來源:PemWriter.java

示例9: serializeSSHKeyFromNistp256

import org.spongycastle.util.encoders.Base64; //導入依賴的package包/類
/**
 * <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());
}
 
開發者ID:martin-lizner,項目名稱:trezor-ssh-agent,代碼行數:31,代碼來源:IdentityUtils.java

示例10: getConnection

import org.spongycastle.util.encoders.Base64; //導入依賴的package包/類
private HttpURLConnection getConnection() throws IOException {
    HttpURLConnection connection;
    if (isSwapUrl()) {
        // swap never works with a proxy, its unrouted IP on the same subnet
        connection = (HttpURLConnection) sourceUrl.openConnection();
    } else {
        connection = NetCipher.getHttpURLConnection(sourceUrl);
    }

    // workaround until NetCipher supports HTTPS SNI
    // https://gitlab.com/fdroid/fdroidclient/issues/431
    if (connection instanceof HttpsURLConnection
            && "f-droid.org".equals(sourceUrl.getHost())) {
        ((HttpsURLConnection) connection).setSSLSocketFactory(HttpsURLConnection.getDefaultSSLSocketFactory());
    }

    if (username != null && password != null) {
        // add authorization header from username / password if set
        String authString = username + ":" + password;
        connection.setRequestProperty("Authorization", "Basic " + Base64.toBase64String(authString.getBytes()));
    }
    return connection;
}
 
開發者ID:CmDnoEdition,項目名稱:fdroid,代碼行數:24,代碼來源:HttpDownloader.java

示例11: signMessage

import org.spongycastle.util.encoders.Base64; //導入依賴的package包/類
/**
 * Signs a text message using the standard Bitcoin messaging signing format and returns the signature as a base64
 * encoded string.
 *
 * @throws IllegalStateException if this ECKey does not have the private part.
 * @throws KeyCrypterException if this ECKey is encrypted and no AESKey is provided or it does not decrypt the ECKey.
 */
public String signMessage(String message, @Nullable KeyParameter aesKey) throws KeyCrypterException {
    byte[] data = Utils.formatMessageForSigning(message);
    Sha256Hash hash = Sha256Hash.createDouble(data);
    ECDSASignature sig = sign(hash, aesKey);
    // Now we have to work backwards to figure out the recId needed to recover the signature.
    int recId = -1;
    for (int i = 0; i < 4; i++) {
        ECKey k = ECKey.recoverFromSignature(i, sig, hash, isCompressed());
        if (k != null && k.pub.equals(pub)) {
            recId = i;
            break;
        }
    }
    if (recId == -1)
        throw new RuntimeException("Could not construct a recoverable key. This should never happen.");
    int headerByte = recId + 27 + (isCompressed() ? 4 : 0);
    byte[] sigData = new byte[65];  // 1 header + 32 bytes for R + 32 bytes for S
    sigData[0] = (byte)headerByte;
    System.arraycopy(Utils.bigIntegerToBytes(sig.r, 32), 0, sigData, 1, 32);
    System.arraycopy(Utils.bigIntegerToBytes(sig.s, 32), 0, sigData, 33, 32);
    return new String(Base64.encode(sigData), Charset.forName("UTF-8"));
}
 
開發者ID:HashEngineering,項目名稱:namecoinj,代碼行數:30,代碼來源:ECKey.java

示例12: generateSaltKey

import org.spongycastle.util.encoders.Base64; //導入依賴的package包/類
/**
 * @summary Method to generate a new salt key
 * @return  {String} The newly generated salt key
 */
public static String generateSaltKey()
    throws UnsupportedEncodingException {

    SecureRandom csprng = new SecureRandom();
    byte[] saltKeyBytes = new byte[SALT_KEY_LENGTH];
    csprng.nextBytes(saltKeyBytes);
    String saltKey = null;
    try {
        saltKey = new String(Base64.encode(saltKeyBytes), UTF8);
    } catch (UnsupportedEncodingException e) {
        // Throw it to the caller.
        throw e;
    }
    return saltKey;
}
 
開發者ID:manzdagratiano,項目名稱:gobbledygook,代碼行數:20,代碼來源:Crypto.java

示例13: signMessage

import org.spongycastle.util.encoders.Base64; //導入依賴的package包/類
/**
 * Signs a text message using the standard Bitcoin messaging signing format and returns the signature as a base64
 * encoded string.
 *
 * @throws IllegalStateException if this ECKey does not have the private part.
 * @throws KeyCrypterException if this ECKey is encrypted and no AESKey is provided or it does not decrypt the ECKey.
 */
public String signMessage(String message, @Nullable KeyParameter aesKey) throws KeyCrypterException {
    if (priv == null)
        throw new IllegalStateException("This ECKey does not have the private key necessary for signing.");
    byte[] data = Utils.formatMessageForSigning(message);
    Sha256Hash hash = Sha256Hash.createDouble(data);
    ECDSASignature sig = sign(hash, aesKey);
    // Now we have to work backwards to figure out the recId needed to recover the signature.
    int recId = -1;
    for (int i = 0; i < 4; i++) {
        ECKey k = ECKey.recoverFromSignature(i, sig, hash, isCompressed());
        if (k != null && Arrays.equals(k.pub, pub)) {
            recId = i;
            break;
        }
    }
    if (recId == -1)
        throw new RuntimeException("Could not construct a recoverable key. This should never happen.");
    int headerByte = recId + 27 + (isCompressed() ? 4 : 0);
    byte[] sigData = new byte[65];  // 1 header + 32 bytes for R + 32 bytes for S
    sigData[0] = (byte)headerByte;
    System.arraycopy(Utils.bigIntegerToBytes(sig.r, 32), 0, sigData, 1, 32);
    System.arraycopy(Utils.bigIntegerToBytes(sig.s, 32), 0, sigData, 33, 32);
    return new String(Base64.encode(sigData), Charset.forName("UTF-8"));
}
 
開發者ID:HashEngineering,項目名稱:megacoinj,代碼行數:32,代碼來源:ECKey.java

示例14: decrypt

import org.spongycastle.util.encoders.Base64; //導入依賴的package包/類
@Override
public String decrypt(String protectedString) {
    if (protectedString == null) {
        throw new IllegalArgumentException("ProtectedString must not be null");
    }

    byte[] protectedBuffer = Base64.decode(protectedString.getBytes());
    byte[] plainText = new byte[protectedBuffer.length];

    try {
        salsa20Engine.processBytes(protectedBuffer, 0, protectedBuffer.length, plainText, 0);
        return new String(plainText, ENCODING);
    } catch (UnsupportedEncodingException e) {
        throw new UnsupportedOperationException(MSG_UNKNOWN_UTF8_ENCODING, e);
    }
}
 
開發者ID:cternes,項目名稱:openkeepass,代碼行數:17,代碼來源:Salsa20.java

示例15: encrypt

import org.spongycastle.util.encoders.Base64; //導入依賴的package包/類
@Override
public String encrypt(String plainString) {
    if (plainString == null) {
        throw new IllegalArgumentException("PlainString must not be null");
    }

    try {
        byte[] plainStringBytes = plainString.getBytes(ENCODING);
        byte[] encodedText = new byte[plainStringBytes.length];

        salsa20Engine.processBytes(plainStringBytes, 0, plainStringBytes.length, encodedText, 0);

        byte[] protectedBuffer = Base64.encode(encodedText);

        return new String(protectedBuffer, ENCODING);
    } catch (UnsupportedEncodingException e) {
        throw new UnsupportedOperationException(MSG_UNKNOWN_UTF8_ENCODING, e);
    }
}
 
開發者ID:cternes,項目名稱:openkeepass,代碼行數:20,代碼來源:Salsa20.java


注:本文中的org.spongycastle.util.encoders.Base64類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。