当前位置: 首页>>代码示例>>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;未经允许,请勿转载。