本文整理汇总了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;
}
示例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;
}
示例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();
}
}
示例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;
}
示例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);
}
}
示例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;
}
示例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;
}
示例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);
}
示例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[]{};
}
}
示例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");
}
}
示例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 );
}
}
示例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 );
}
}
示例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();
}
}
示例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());
}
示例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;
}