package iminto.util.common; import java.io.IOException; import java.security.NoSuchAlgorithmException; import iminto.util.encypt.Base64Decoder; import iminto.util.encypt.Base64Encoder; import iminto.util.encypt.MD5; /** * <pre> * 基于密匙的xor加解密 * 可以进行安全的二进制加密 * </pre> */ public class XorEncrypt { /** * 私有密匙 */ private String privateKey = "EnCR5hOR_inchenwang97768038542121L"; /** * 密匙 */ private String key; /** * 协同密匙 */ private String companionKey; /** * * @param key 公有密匙 * @throws NoSuchAlgorithmException */ public XorEncrypt(String key) throws NoSuchAlgorithmException { this.key = MD5.md5(privateKey + key); this.companionKey = this.genCompanion(key); } /** * 设置私有密匙 * @param privateKey */ public void setPrivateKey(String privateKey) { this.privateKey = privateKey; } /** * 制取取协同密匙 * @param <type> $key * @return <type> */ private String genCompanion(String key) { StringBuilder strBuilder = new StringBuilder(); byte[] keys = key.getBytes(); strBuilder.append(privateKey); for (byte _key : keys) { strBuilder.append(_key); } return strBuilder.toString(); } /** * 异或加解密 * @param Mixed $content * @param String $pKey * @return String */ private String getXorBilateral(String content, String pKey) { int len = pKey.length(); int count = content.length(); char current; StringBuilder strBuilder = new StringBuilder(); int kIdx = 0; int i = 0; for (i = 0; i < count; i++) { if (kIdx == len) { kIdx = 0; } current = content.charAt(i); strBuilder.append((char) (current ^ pKey.charAt(kIdx))); kIdx++; } return strBuilder.toString(); } /** * 加密 * @param content * @return String * @throws IOException */ public String encrypt(String content) throws IOException { String randKey = RandomHelper.toString(16); content = getXorBilateral(content, this.key); content = getXorBilateral(content, randKey); content = randKey + content; content = getXorBilateral(content, this.companionKey); content = new Base64Encoder().encode(content); return content; } /** * 解密 * @param content * @return String * @throws IOException */ public String decrypt(String content) throws IOException { content = new Base64Decoder().decode(content); content = getXorBilateral(content, this.companionKey); String randKey = StringHelper.subString(content, 0, 16); content = StringHelper.subString(content, 16, -1); content = getXorBilateral(content, randKey); content = getXorBilateral(content, this.key); return content; } public static void main(String[] args) throws NoSuchAlgorithmException, IOException { XorEncrypt x=new XorEncrypt("iminto"); System.out.println(x.encrypt("test")); } }

yymmdm LV6
2022年8月10日
qqbaidu123 LV4
2022年2月21日
文艺小青年 LV1
2020年3月13日
zw5097 LV23
2019年11月24日
a986527557 LV6
2019年11月19日
aisc1314 LV14
2018年9月4日
wheilson LV1
2018年4月19日
zhengxinshuai LV1
2018年3月14日
759886267 LV11
2017年9月23日
rastaclat LV9
2017年8月8日

不不要 LV1
2024年5月15日
yymmdm LV6
2022年8月10日
dlh7967656 LV1
2022年3月30日
微信网友_5865371899187200
2022年3月9日
暂无贡献等级
qqbaidu123 LV4
2022年2月21日
缘------ LV9
2021年3月10日
Kervin LV11
2020年9月29日
丶附耳聆听 LV21
2020年8月31日
jerry_mouse LV6
2020年7月31日
禅之禅 LV9
2020年7月9日