当前位置: 首页>>代码示例>>Java>>正文


Java Base64类代码示例

本文整理汇总了Java中org.bouncycastle.util.encoders.Base64的典型用法代码示例。如果您正苦于以下问题:Java Base64类的具体用法?Java Base64怎么用?Java Base64使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Base64类属于org.bouncycastle.util.encoders包,在下文中一共展示了Base64类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testEncryptRijndael

import org.bouncycastle.util.encoders.Base64; //导入依赖的package包/类
public String testEncryptRijndael(String value,String key) throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    BlockCipher engine = new RijndaelEngine(256);
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine), new ZeroBytePadding());

    byte[] keyBytes = key.getBytes();
    cipher.init(true, new KeyParameter(keyBytes));

    byte[] input = value.getBytes();
    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];

    int cipherLength = cipher.processBytes(input, 0, input.length, cipherText, 0);
    cipher.doFinal(cipherText, cipherLength);

    String result = new String(Base64.encode(cipherText));
    //Log.e("testEncryptRijndael : " , result);
    return  result;
}
 
开发者ID:David-Hackro,项目名称:ExamplesAndroid,代码行数:18,代码来源:Metodos.java

示例2: encryptReply

import org.bouncycastle.util.encoders.Base64; //导入依赖的package包/类
public EncryptedResponsePacket encryptReply(String aesKey, InputStream plaintext) throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, IOException {
    byte[] keyBytes = Base64.decode(aesKey);

    SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");

    Cipher cipher = Cipher.getInstance("AES/CCM/NoPadding", "BC");
    cipher.init(Cipher.ENCRYPT_MODE, key, new SecureRandom());
    byte[] iv = cipher.getIV();

    ByteArrayOutputStream encrypted = new ByteArrayOutputStream();


    CipherOutputStream cOut = new CipherOutputStream(encrypted, cipher);
    Streams.pipeAll(plaintext, cOut);
    cOut.close();

    EncryptedResponsePacket packet = new EncryptedResponsePacket();
    packet.iv = Base64.toBase64String(iv);
    packet.mode = "ccm";
    packet.ct = Base64.toBase64String(encrypted.toByteArray());

    return packet;
}
 
开发者ID:Tethik,项目名称:whistleblower,代码行数:24,代码来源:CryptographyHandler.java

示例3: writeEncoded

import org.bouncycastle.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:Appdome,项目名称:ipack,代码行数:23,代码来源:PemWriter.java

示例4: verify

import org.bouncycastle.util.encoders.Base64; //导入依赖的package包/类
@Override
public boolean verify(final String hostname, final int port, final PublicKey key)
        throws ConnectionCanceledException, ChecksumException {
    final String lookup = preferences.getProperty(this.getFormat(hostname, key));
    if(StringUtils.equals(Base64.toBase64String(key.getEncoded()), lookup)) {
        if(log.isInfoEnabled()) {
            log.info(String.format("Accepted host key %s matching %s", key, lookup));
        }
        return true;
    }
    final boolean accept;
    if(null == lookup) {
        accept = this.isUnknownKeyAccepted(hostname, key);
    }
    else {
        accept = this.isChangedKeyAccepted(hostname, key);
    }
    return accept;
}
 
开发者ID:iterate-ch,项目名称:cyberduck,代码行数:20,代码来源:PreferencesHostKeyVerifier.java

示例5: parseRSAPublicKey

import org.bouncycastle.util.encoders.Base64; //导入依赖的package包/类
/**
 * Creates a RSA Public Key from a PEM String
 *
 * @param pemPublicKey public key in PEM format
 * @return a RSA public key
 */
public static PublicKey parseRSAPublicKey(final String pemPublicKey)
        throws GeneralSecurityException {
  try {
    String publicKeyString = pemPublicKey
            .replace(PEM_PUBLIC_START, "")
            .replace(PEM_PUBLIC_END, "")
            .replaceAll("\\s", "");
    byte[] keyBytes = Base64.decode(publicKeyString.getBytes("UTF-8"));
    X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    return keyFactory.generatePublic(spec);

  } catch (InvalidKeySpecException | NoSuchAlgorithmException | UnsupportedEncodingException e) {
    throw new GeneralSecurityException(e);
  }
}
 
开发者ID:symphonyoss,项目名称:app-auth-example,代码行数:23,代码来源:SecurityKeyUtils.java

示例6: generatePublicKeyFromString

import org.bouncycastle.util.encoders.Base64; //导入依赖的package包/类
private static PublicKey generatePublicKeyFromString(String key, String algorithm) {
	PublicKey publicKey = null;
	if(key.length()>1){
		key = key.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "")
				.replaceAll("\\s+", "").replaceAll("\\r+", "").replaceAll("\\n+", "");
		byte[] keyByteArray = java.util.Base64.getDecoder().decode(key);
		try {
			KeyFactory kf = KeyFactory.getInstance(algorithm);
			EncodedKeySpec keySpec = new X509EncodedKeySpec(keyByteArray);
			publicKey = kf.generatePublic(keySpec);
		} catch (Exception e) {
			ConsoleOut.output(e.getMessage());
		}
	}
	return publicKey;
}
 
开发者ID:mvetsch,项目名称:JWT4B,代码行数:17,代码来源:AlgorithmLinker.java

示例7: generatePrivateKeyFromString

import org.bouncycastle.util.encoders.Base64; //导入依赖的package包/类
private static PrivateKey generatePrivateKeyFromString(String key, String algorithm) {
	PrivateKey privateKey = null;
	if(key.length()>1){
		key = key.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "")
			 .replaceAll("\\s+", "").replaceAll("\\r+", "").replaceAll("\\n+", "");
		byte[] keyByteArray = Base64.decode(key);
		try {
			KeyFactory kf = KeyFactory.getInstance(algorithm);
			EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyByteArray);
			privateKey = kf.generatePrivate(keySpec);
		} catch (Exception e) {
			ConsoleOut.output(e.getMessage());
		}
	}
	return privateKey;
}
 
开发者ID:mvetsch,项目名称:JWT4B,代码行数:17,代码来源:AlgorithmLinker.java

示例8: DecryptMsg

import org.bouncycastle.util.encoders.Base64; //导入依赖的package包/类
/**
 * 解码
 * @param byteMsg base64格式的byte数组
 * @return 解码后的byte数组
 * @since  1.0
 */
public byte[] DecryptMsg(byte[] byteMsg) {

    if (byteMsg == null || byteMsg.length == 0) {
        return new byte[]{};
    }
    byte[] msg = Base64.decode(byteMsg);
    byte[] pDecData = AESUtils.decrypt(msg);
    if (pDecData == null || pDecData.length < 4) {
        return new byte[]{};
    }
    byte[] lenbytes = Arrays.copyOfRange(pDecData, pDecData.length - 4, pDecData.length);
    int nInLen = CommonUtils.byteArray2int(lenbytes);

    return Arrays.copyOf(pDecData, nInLen);
}
 
开发者ID:ccfish86,项目名称:sctalk,代码行数:22,代码来源:SecurityUtils.java

示例9: EncryptMsg

import org.bouncycastle.util.encoders.Base64; //导入依赖的package包/类
/**
 * 加密
 * @param strMsg 明文
 * @return 加密后的byte数组(base64)
 * @since  1.0
 */
public byte[] EncryptMsg(String strMsg) {
    try {
        byte[] pInData = strMsg.getBytes("utf8");
        int nInLen = pInData.length;
        int nRemain = nInLen % 16;
        int nBlocks = (nInLen + 15) / 16;

        if (nRemain > 12 || nRemain == 0) {
            nBlocks += 1;
        }
        int nEncryptLen = nBlocks * 16;

        byte[] pData = Arrays.copyOf(pInData, nEncryptLen);

        byte[] lenbytes = CommonUtils.intToBytes(nInLen);

        for (int i = 0; i < 4; i++) {
            pData[nEncryptLen + i - 4] = lenbytes[i];
        }
        byte[] encrypted = AESUtils.encrypt(pData);
        return Base64.encode(encrypted);
    } catch (UnsupportedEncodingException e) {
        return new byte[]{};
    }
}
 
开发者ID:ccfish86,项目名称:sctalk,代码行数:32,代码来源:SecurityUtils.java

示例10: pgpKeyFromExchange

import org.bouncycastle.util.encoders.Base64; //导入依赖的package包/类
private PGPPublicKey pgpKeyFromExchange(Exchange exchange) throws IOException {
	String user = exchange.getIn().getHeader("To", String.class);
	String base64PgpKey = exchange.getIn().getHeader("PgpKey", String.class);
	if (base64PgpKey != null) {
		byte[] pgpKey = Base64.decode(base64PgpKey);
		InputStream in = new ByteArrayInputStream(pgpKey);
		InputStream decoderStream = PGPUtil.getDecoderStream(in);
		PGPPublicKeyRing keyRing = new JcaPGPPublicKeyRing(decoderStream);
		Spliterator<PGPPublicKey> splitIterator = spliteratorUnknownSize(keyRing.getPublicKeys(), Spliterator.ORDERED);
		Stream<PGPPublicKey> targetStream = StreamSupport.stream(splitIterator, false);
		return targetStream.filter(pgpPublicKey -> pgpPublicKey.isEncryptionKey() && !pgpPublicKey.isMasterKey())
				.findFirst().orElseThrow(() -> new IllegalArgumentException(user + " error: Invalid PGP public key"));
	} else {
		throw new IllegalArgumentException(user + " error: PGP key not provided");
	}
}
 
开发者ID:LuatixHQ,项目名称:openex-worker,代码行数:17,代码来源:EmailPgp.java

示例11: encrypt

import org.bouncycastle.util.encoders.Base64; //导入依赖的package包/类
public String encrypt( String unencryptedString ) throws EncryptionException
{
	if ( unencryptedString == null || unencryptedString.trim().length() == 0 )
		throw new IllegalArgumentException(
		"unencrypted string was null or empty" );
	
	try
	{
		SecretKey key = keyFactory.generateSecret( keySpec );
		cipher.init( Cipher.ENCRYPT_MODE, key );
		byte[] cleartext = unencryptedString.getBytes( UNICODE_FORMAT );
		byte[] ciphertext = cipher.doFinal( cleartext );
		
		return new String( Base64.encode( ciphertext ));
	}
	catch (Exception e)
	{
		throw new EncryptionException( e );
	}
}
 
开发者ID:thangbn,项目名称:Direct-File-Downloader,代码行数:21,代码来源:StringEncrypter.java

示例12: decrypt

import org.bouncycastle.util.encoders.Base64; //导入依赖的package包/类
public String decrypt( String encryptedString ) throws EncryptionException
{
	if ( encryptedString == null || encryptedString.trim().length() <= 0 )
		throw new IllegalArgumentException( "encrypted string was null or empty" );
	
	try
	{
		SecretKey key = keyFactory.generateSecret( keySpec );
		cipher.init( Cipher.DECRYPT_MODE, key );
		byte[] cleartext = Base64.decode( encryptedString );
		byte[] ciphertext = cipher.doFinal( cleartext );
		
		return bytes2String( ciphertext );
	}
	catch (Exception e)
	{
		throw new EncryptionException( e );
	}
}
 
开发者ID:thangbn,项目名称:Direct-File-Downloader,代码行数:20,代码来源:StringEncrypter.java

示例13: writeEncoded

import org.bouncycastle.util.encoders.Base64; //导入依赖的package包/类
private void writeEncoded(byte[] bytes) 
    throws IOException
{
    char[]  buf = new char[64];
    
    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:thangbn,项目名称:Direct-File-Downloader,代码行数:25,代码来源:PEMWriter.java

示例14: readBytes

import org.bouncycastle.util.encoders.Base64; //导入依赖的package包/类
private byte[] readBytes(String endMarker)
    throws IOException
{
    String          line;
    StringBuffer    buf = new StringBuffer();
  
    while ((line = readLine()) != null)
    {
        if (line.indexOf(endMarker) != -1)
        {
            break;
        }
        buf.append(line.trim());
    }

    if (line == null)
    {
        throw new IOException(endMarker + " not found");
    }

    return Base64.decode(buf.toString());
}
 
开发者ID:thangbn,项目名称:Direct-File-Downloader,代码行数:23,代码来源:PEMReader.java

示例15: encodeToJSONObject

import org.bouncycastle.util.encoders.Base64; //导入依赖的package包/类
/**
 * encodes a map into a JSONObject.
 * <P>
 * It's recommended that you use {@link #encodeToJSON(Map)} instead
 * 
 * @param map
 * @return
 *
 * @since 3.0.1.5
 */
public static Map encodeToJSONObject(Map map) {
	Map newMap = new JSONObject();

	for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
		String key = (String) iter.next();
		Object value = map.get(key);

		if (value instanceof byte[]) {
			key += ".B64";
			value = Base64.encode((byte[]) value);
		}

		value = coerce(value);

		newMap.put(key, value);
	}
	return newMap;
}
 
开发者ID:thangbn,项目名称:Direct-File-Downloader,代码行数:29,代码来源:JSONUtils.java


注:本文中的org.bouncycastle.util.encoders.Base64类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。