sh1prng算法

/**
	 * @return 经过加密和编码后的cookie字符串 v=1&u=xxx&a=1
	 */
	public String generateEncodeValue() {
		StringBuffer encodeValue = new StringBuffer();
		encodeValue.append("v=").append(this.version).append("|u=")
				.append(this.uid).append("|a=").append(this.autoLogin);
		try {
			String key = CMCrypto.initKey(seed);
			String value = CMHash.encryptBASE64(CMCrypto.encrypt(encodeValue
					.toString().getBytes(), key));
			value = value.replaceAll("\r", "<");
			value = value.replaceAll("\n", ">");
			return value;
		} catch (Exception e) {
			LogNew.fatal("crypto cookie fail.", e);
		}
		return "";
	}
 
public static String initKey(String seed) throws Exception {

		
		KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);
		SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
		secureRandom.setSeed(seed.getBytes());
		kg.init(128, secureRandom);

		SecretKey secretKey = kg.generateKey();

		return CMHash.encryptBASE64(secretKey.getEncoded());
	}
 

相关推荐