Java Code Examples for org.apache.shiro.codec.Base64#decode()

The following examples show how to use org.apache.shiro.codec.Base64#decode() . 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: AesEncryptUtil.java    From jeecg-boot-with-activiti with MIT License 6 votes vote down vote up
/**
  * 解密方法
  * @param data 要解密的数据
  * @param key  解密key
  * @param iv 解密iv
  * @return 解密的结果
  * @throws Exception
  */
 public static String desEncrypt(String data, String key, String iv) throws Exception {
     try {
byte[] encrypted1 = Base64.decode(data);

         Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
         SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
         IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

         cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

         byte[] original = cipher.doFinal(encrypted1);
         String originalString = new String(original);
         return originalString;
     } catch (Exception e) {
         e.printStackTrace();
         return null;
     }
 }
 
Example 2
Source File: AesEncryptUtil.java    From teaching with Apache License 2.0 6 votes vote down vote up
/**
  * 解密方法
  * @param data 要解密的数据
  * @param key  解密key
  * @param iv 解密iv
  * @return 解密的结果
  * @throws Exception
  */
 public static String desEncrypt(String data, String key, String iv) throws Exception {
     try {
byte[] encrypted1 = Base64.decode(data);

         Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
         SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
         IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

         cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

         byte[] original = cipher.doFinal(encrypted1);
         String originalString = new String(original);
         return originalString;
     } catch (Exception e) {
         e.printStackTrace();
         return null;
     }
 }
 
Example 3
Source File: AesEncryptUtil.java    From jeecg-boot with Apache License 2.0 6 votes vote down vote up
/**
  * 解密方法
  * @param data 要解密的数据
  * @param key  解密key
  * @param iv 解密iv
  * @return 解密的结果
  * @throws Exception
  */
 public static String desEncrypt(String data, String key, String iv) throws Exception {
     try {
byte[] encrypted1 = Base64.decode(data);

         Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
         SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
         IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

         cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

         byte[] original = cipher.doFinal(encrypted1);
         String originalString = new String(original);
         return originalString;
     } catch (Exception e) {
         e.printStackTrace();
         return null;
     }
 }
 
Example 4
Source File: HashedCredentialsMatcher.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param info the AuthenticationInfo from which to retrieve the credentials which assumed to be in already-hashed form.
 * @return a {@link Hash Hash} instance representing the given AuthenticationInfo's stored credentials.
 */
protected Object getCredentials(AuthenticationInfo info) {
    Object credentials = info.getCredentials();

    byte[] storedBytes = toBytes(credentials);

    if (credentials instanceof String || credentials instanceof char[]) {
        //account.credentials were a char[] or String, so
        //we need to do text decoding first:
        if (isStoredCredentialsHexEncoded()) {
            storedBytes = Hex.decode(storedBytes);
        } else {
            storedBytes = Base64.decode(storedBytes);
        }
    }
    AbstractHash hash = newHashInstance();
    hash.setBytes(storedBytes);
    return hash;
}
 
Example 5
Source File: ShiroTest.java    From EasyEE with MIT License 6 votes vote down vote up
public static void main(String[] args) {
	String base64="zGkG0zevFbnIioooS3MfnnbWeBUeiZrScKVJms0CZAdJ2MYTddPkvBHGmMhvgdKA5QJk8FSb9T1Y1tFlUnnCsIvcK+iX4cfrwD7voGdU5bW9AWjwNvl3BDrAgRf+hvjrI3T5nBTFeW7uI6GzfFrIx92qER9lQ97g19Dn555uwIzf0ULvc8jICZTraLLrf2avh1hy2kUJJblO6u5IHiBbAXBNitZ6W1yjLEWmSFnb+EsEWAGB3WwV1u1HZO045LB4G57UIH4QGM0xfJjWWeahiTS4j9IAEJBbghwfL1A5pdzFiXJzzA5GF85vtP+6jLwQfGaQJyv35PvNNsDmCqFe8eUSBLji5z5y/y+yKfZk9izXiEvFjKQ5kqMqKfLMp+Vn5OuO+syc4CfJL4PLI16vwVUPV1EWAzyxUhK7DtD5OMVcLPwVtwZ11dG88wkZtjXvBymLyGCj5/Tk8gTWYsdcNKD5i8WvbMLT45S4iWsZxa/5offIiCipkkqoqvxCppJLTzBoaR/wlqoa1Bc/cvpijiJTIbSCj+iFloQRdax1mMQ";
	 base64 = ensurePadding(base64);
	  byte[] decoded = Base64.decode(base64);
	  
	  
	   byte[] serialized = decoded;
        CipherService cipherService = new AesCipherService();
        if (cipherService != null) {
            ByteSource byteSource = cipherService.decrypt(decoded, new byte[]{-112, -15, -2, 108, -116, 100, -28, 61, -99, 121, -104, -120, -59, -58, -102, 104});
            serialized = byteSource.getBytes();
        }
        Serializer<PrincipalCollection> serializer = new DefaultSerializer<PrincipalCollection>();
        ;
        System.out.println(serializer.deserialize(serialized));
        
        	SimplePrincipalCollection p=(SimplePrincipalCollection) serializer.deserialize(serialized);	

	  System.out.println(p.getPrimaryPrincipal());
	  System.out.println(p.getRealmNames());
	  System.out.println(p);
}
 
Example 6
Source File: ShiroTest.java    From EasyEE with MIT License 6 votes vote down vote up
public static void main(String[] args) {
	String base64="zGkG0zevFbnIioooS3MfnnbWeBUeiZrScKVJms0CZAdJ2MYTddPkvBHGmMhvgdKA5QJk8FSb9T1Y1tFlUnnCsIvcK+iX4cfrwD7voGdU5bW9AWjwNvl3BDrAgRf+hvjrI3T5nBTFeW7uI6GzfFrIx92qER9lQ97g19Dn555uwIzf0ULvc8jICZTraLLrf2avh1hy2kUJJblO6u5IHiBbAXBNitZ6W1yjLEWmSFnb+EsEWAGB3WwV1u1HZO045LB4G57UIH4QGM0xfJjWWeahiTS4j9IAEJBbghwfL1A5pdzFiXJzzA5GF85vtP+6jLwQfGaQJyv35PvNNsDmCqFe8eUSBLji5z5y/y+yKfZk9izXiEvFjKQ5kqMqKfLMp+Vn5OuO+syc4CfJL4PLI16vwVUPV1EWAzyxUhK7DtD5OMVcLPwVtwZ11dG88wkZtjXvBymLyGCj5/Tk8gTWYsdcNKD5i8WvbMLT45S4iWsZxa/5offIiCipkkqoqvxCppJLTzBoaR/wlqoa1Bc/cvpijiJTIbSCj+iFloQRdax1mMQ";
	 base64 = ensurePadding(base64);
	  byte[] decoded = Base64.decode(base64);
	  
	  
	   byte[] serialized = decoded;
        CipherService cipherService = new AesCipherService();
        if (cipherService != null) {
            ByteSource byteSource = cipherService.decrypt(decoded, new byte[]{-112, -15, -2, 108, -116, 100, -28, 61, -99, 121, -104, -120, -59, -58, -102, 104});
            serialized = byteSource.getBytes();
        }
        Serializer<PrincipalCollection> serializer = new DefaultSerializer<PrincipalCollection>();
        ;
        System.out.println(serializer.deserialize(serialized));
        
        	SimplePrincipalCollection p=(SimplePrincipalCollection) serializer.deserialize(serialized);	

	  System.out.println(p.getPrimaryPrincipal());
	  System.out.println(p.getRealmNames());
	  System.out.println(p);
}
 
Example 7
Source File: WechatUtil.java    From code with Apache License 2.0 5 votes vote down vote up
public static JSONObject getUserInfo(String encryptedData, String sessionKey, String iv) {
    // 被加密的数据
    byte[] dataByte = Base64.decode(encryptedData);
    // 加密秘钥
    byte[] keyByte = Base64.decode(sessionKey);
    // 偏移量
    byte[] ivByte = Base64.decode(iv);
    try {
        // 如果密钥不足16位,那么就补足.  这个if 中的内容很重要
        int base = 16;
        if (keyByte.length % base != 0) {
            int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
            byte[] temp = new byte[groups * base];
            Arrays.fill(temp, (byte) 0);
            System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
            keyByte = temp;
        }
        // 初始化
        Security.addProvider(new BouncyCastleProvider());
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
        SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
        AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
        parameters.init(new IvParameterSpec(ivByte));
        cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
        byte[] resultByte = cipher.doFinal(dataByte);
        if (null != resultByte && resultByte.length > 0) {
            String result = new String(resultByte, "UTF-8");
            return JSON.parseObject(result);
        }
    } catch (Exception e) {
    }
    return null;
}
 
Example 8
Source File: ShiroConfig.java    From yyblog with MIT License 5 votes vote down vote up
/**
* cookie管理器;
* @return
*/
@Bean
public CookieRememberMeManager rememberMeManager(){
    CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
    //rememberme cookie加密的密钥 建议每个项目都不一样 默认AES算法 密钥长度(128 256 512 位),通过以下代码可以获取
    //KeyGenerator keygen = KeyGenerator.getInstance("AES");
    //SecretKey deskey = keygen.generateKey();
    //System.out.println(Base64.encodeToString(deskey.getEncoded()));
    byte[] cipherKey = Base64.decode("wGiHplamyXlVB11UXWol8g==");
    cookieRememberMeManager.setCipherKey(cipherKey);
    cookieRememberMeManager.setCookie(rememberMeCookie());
    return cookieRememberMeManager;
}
 
Example 9
Source File: SerializeUtils.java    From Spring-Shiro-Spark with Apache License 2.0 5 votes vote down vote up
public static <T> T deserializeFromString(String base64){
    try{
        byte[] objectData = Base64.decode(base64);
        return deserialize(objectData);
    } catch (Exception e){
        throw new ResultException("deserialize session error");
    }
}
 
Example 10
Source File: SerializeUtils.java    From Spring-Shiro-Spark with Apache License 2.0 5 votes vote down vote up
public static <T> Collection<T> deserializeFromStrings(Collection<String> base64s){
    try{
        List<T> list = Lists.newLinkedList();
        for(String base64 : base64s){
            byte[] objectData = Base64.decode(base64);
            T t = deserialize(objectData);
            list.add(t);
        }
        return list;
    }catch (Exception e){
        throw new ResultException("deserialize session error");
    }
}
 
Example 11
Source File: SerializableUtil.java    From zheng with MIT License 5 votes vote down vote up
public static Session deserialize(String sessionStr) {
    if (StringUtils.isBlank(sessionStr)) {
        return null;
    }
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(sessionStr));
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (Session) ois.readObject();
    } catch (Exception e) {
        throw new RuntimeException("deserialize session error", e);
    }
}
 
Example 12
Source File: SerializableUtils.java    From phone with Apache License 2.0 5 votes vote down vote up
public static<T> T deserialize(String sessionStr,Class<T> clazz) {
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(sessionStr));
        ObjectInputStream ois = new ObjectInputStream(bis);
        return ReflectionUtil.convertObjectToBean(ois.readObject(),clazz);
    } catch (Exception e) {
        throw new RuntimeException("deserialize session error", e);
    }
}
 
Example 13
Source File: SecurityModule.java    From tapestry-security with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
public static RememberMeManager buildRememberMeManager(Serializer serializer, Logger logger,
													   @Symbol(SymbolConstants.HMAC_PASSPHRASE) String hmacPassphrase,
													   @Symbol(SecuritySymbols.REMEMBERME_CIPHERKERY) String rememberMeCipherKey) throws UnsupportedEncodingException {
	CookieRememberMeManager rememberMeManager = new CookieRememberMeManager();
	// the default Shiro serializer produces obnoxiously long cookies
	rememberMeManager.setSerializer(serializer);

	// assume properly configured cipher is of the right width (divisable by 16)
	byte[] cipherKey = Base64.decode(rememberMeCipherKey);
	if (cipherKey.length <= 0) {
		if (hmacPassphrase.isEmpty()) {
			logger
					.error("Neither symbol '"
								   + SecuritySymbols.REMEMBERME_CIPHERKERY
								   + "' nor  '"
								   + SymbolConstants.HMAC_PASSPHRASE
								   + "' is set. Using a random value as the cipher key for encrypting rememberMe information. Cookies will be invalidated when the JVM is restarted");
			return rememberMeManager;
		}

		logger.warn("Symbol '" + SecuritySymbols.REMEMBERME_CIPHERKERY + "' is not set, using '"
							+ SymbolConstants.HMAC_PASSPHRASE
							+ "' as the cipher. Beware that changing the value will invalidate rememberMe cookies");
		if (hmacPassphrase.length() < 16)
			hmacPassphrase = hmacPassphrase + ("================".substring(hmacPassphrase.length()));
		cipherKey = hmacPassphrase.getBytes("UTF-8");
		if (cipherKey.length > 16) cipherKey = Arrays.copyOf(cipherKey, 16);
	}
	rememberMeManager.setCipherKey(cipherKey);
	return rememberMeManager;
}
 
Example 14
Source File: Base64Uploader.java    From xmanager with Apache License 2.0 4 votes vote down vote up
private static byte[] decode(String content) {
	return Base64.decode(content);
}