com.jeesuite.common.crypt.Base64 Java Examples

The following examples show how to use com.jeesuite.common.crypt.Base64. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RsaSignUtils.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
/** 
 * 随机生成密钥对 
 */  
public static String[] generateKeyPair() {  
    // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象  
    KeyPairGenerator keyPairGen = null;  
    try {  
        keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);  
    } catch (NoSuchAlgorithmException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }  
    // 初始化密钥对生成器,密钥大小为96-1024位  
    keyPairGen.initialize(KEY_SIZE,new SecureRandom());  
    // 生成一个密钥对,保存在keyPair中  
    KeyPair keyPair = keyPairGen.generateKeyPair();  
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();  
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();  
    String publicKeyString = Base64.encodeToString(publicKey.getEncoded(),true);  
    String privateKeyString = Base64.encodeToString(privateKey.getEncoded(),true); 
    
    return new String[]{publicKeyString,privateKeyString};
}
 
Example #2
Source File: ConfigcenterContext.java    From jeesuite-config with Apache License 2.0 5 votes vote down vote up
private static String decryptWithAES(String key, String data){
	try {
		String secretKey = DigestUtils.md5(key).substring(16);
		byte[] bytes = AES.decrypt(Base64.decode(data.getBytes(StandardCharsets.UTF_8)),  secretKey.getBytes(StandardCharsets.UTF_8));
		return  new String(bytes, StandardCharsets.UTF_8);
	} catch (Exception e) {
		System.err.println(String.format("解密错误:%s",data));
		throw new RuntimeException(e);
	}
}
 
Example #3
Source File: CryptComponent.java    From jeesuite-config with Apache License 2.0 5 votes vote down vote up
private static String encryptWithAES(String key, String data){
	try {
		String secretKey = DigestUtils.md5(key).substring(16);
		byte[] bytes = AES.encrypt(data.getBytes(StandardCharsets.UTF_8), secretKey.getBytes(StandardCharsets.UTF_8));
		return  Base64.encodeToString(bytes, false);
	} catch (Exception e) {
		throw new JeesuiteBaseException(9999, "加密失败");
	}
}
 
Example #4
Source File: CryptComponent.java    From jeesuite-config with Apache License 2.0 5 votes vote down vote up
private static String decryptWithAES(String key, String data){
	try {
		String secretKey = DigestUtils.md5(key).substring(16);
		byte[] bytes = AES.decrypt(Base64.decode(data.getBytes(StandardCharsets.UTF_8)),  secretKey.getBytes(StandardCharsets.UTF_8));
		return  new String(bytes, StandardCharsets.UTF_8);
	} catch (Exception e) {
		throw new JeesuiteBaseException(9999, "解密失败");
	}
}
 
Example #5
Source File: DigestUtils.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
public static String encodeBase64(String string){
	try {
		return Base64.encodeToString(string.getBytes(CARTSET_UTF_8), false);
	} catch (UnsupportedEncodingException e) {
		throw new RuntimeException(e);
	}
}
 
Example #6
Source File: DigestUtils.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
public static String decodeBase64(String string){
	try {
		return new String(Base64.decodeFast(string.getBytes(CARTSET_UTF_8)));
	} catch (UnsupportedEncodingException e) {
		throw new RuntimeException(e);
	}
}
 
Example #7
Source File: RsaSignUtils.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
public static boolean verifySignature(String contents, String sign, PublicKey publicKey) {
	try {
		byte[] data = contents.getBytes(StandardCharsets.UTF_8.name());
		byte[] dataSignature = Base64.decode(sign);

		Signature signature = Signature.getInstance(SIGN_ALGORITHM);
		signature.initVerify(publicKey);
		signature.update(data, 0, data.length);
		return signature.verify(dataSignature);
	} catch (Exception e) {
		return false;
	}

}
 
Example #8
Source File: RsaSignUtils.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
public static PublicKey loadPublicKey(String pubKeyString) {
	byte[] keyBytes = Base64.decode(pubKeyString);
	X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
	try {
		KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM);
		PublicKey publicKey = factory.generatePublic(x509EncodedKeySpec);
		return publicKey;
	} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {}
	return null;
}
 
Example #9
Source File: RsaSignUtils.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
public static PrivateKey loadPrivateKey(String privateKeyString) {
	try {
		byte[] privateKeyBytes = Base64.decode(privateKeyString);
		PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
		return keyFactory.generatePrivate(pkcs8KeySpec);
	} catch (Exception e) {
		// TODO: handle exception
	}
	return null;
}
 
Example #10
Source File: SimpleCryptUtils.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
public static String decrypt(String key,String data) {
 key = DigestUtils.md5Short(key) + KEY_TAIL;
 byte[] bytes = Base64.decode(data);
 data = new String(bytes, StandardCharsets.UTF_8);
 return DES.decrypt(key, data);
}
 
Example #11
Source File: SecurityCryptUtils.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
public static String decrypt(String data) {
	 byte[] bytes = Base64.decode(data);
	 data = new String(bytes, StandardCharsets.UTF_8);
	 return DES.decrypt(cryptKey, data);
}
 
Example #12
Source File: SecurityCryptUtils.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
public static String encrypt(String data) {
	 String encode = DES.encrypt(cryptKey, data);
	 byte[] bytes = Base64.encodeToByte(encode.getBytes(StandardCharsets.UTF_8), false);
	 return new String(bytes, StandardCharsets.UTF_8);
}
 
Example #13
Source File: RsaSignUtils.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
public static String decrypt(PrivateKey key, String encodedText) {
	byte[] bytes = Base64.decode(encodedText);
	return decrypt(key, bytes);
}
 
Example #14
Source File: RsaSignUtils.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
public static String encrypt(PublicKey key, String plainText) {
	byte[] encodeBytes = encrypt(key, plainText.getBytes(StandardCharsets.UTF_8));
	return Base64.encodeToString(encodeBytes, false);
}
 
Example #15
Source File: SecurityCryptUtils.java    From oneplatform with Apache License 2.0 4 votes vote down vote up
public static String encrypt(String data) {
	 String encode = DES.encrypt(cryptKey, data);
	 byte[] bytes = Base64.encodeToByte(encode.getBytes(StandardCharsets.UTF_8), false);
	 return new String(bytes, StandardCharsets.UTF_8);
}
 
Example #16
Source File: SimpleCryptUtils.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
public static String encrypt(String key,String data) {
 key = DigestUtils.md5Short(key) + KEY_TAIL;
 String encode = DES.encrypt(key, data);
 byte[] bytes = Base64.encodeToByte(encode.getBytes(StandardCharsets.UTF_8), false);
 return new String(bytes, StandardCharsets.UTF_8);
}
 
Example #17
Source File: HttpRequestEntity.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
public String getEncodeBasicAuth(){
	String encoded = Base64.encodeToString((name + ":" + password).getBytes(StandardCharsets.UTF_8),false);
	return "Basic "+encoded;
}
 
Example #18
Source File: SecurityCryptUtils.java    From oneplatform with Apache License 2.0 4 votes vote down vote up
public static String decrypt(String data) {
	 byte[] bytes = Base64.decode(data);
	 data = new String(bytes, StandardCharsets.UTF_8);
	 return DES.decrypt(cryptKey, data);
}