ArcFour加密解密,java RC4加密解密,java 加密解密

简介:


package com.app.common.util;
public class ArcFour {
 /**
  * 加密解密 参数为String 字符串(String data, String key)
  * 
  * @param data
  * @param key
  * @return String
  * @throws UnsupportedEncodingException
  */
 public static final int vactorLen = 256;
 private static String encodeAndDecodeStr(String data, String key)
 {
  // 1.参数检查
  if (data != null && data.length() > 0 && key != null
    && key.length() > 0)
  {
   // 2. 初始化算法
   // 2.1定义矢量vactor
   int[] vactor = new int[vactorLen];
   // 2.2定义临时变量tempk
   byte[] tempK = new byte[vactorLen];
   // 2.3给vactor赋值ֵ0-255
   for (int i = 0; i < vactorLen; i++)
    vactor[i] = i;
   int j = 1;
   // 2.4循环给tempK赋值
   for (short i = 0; i < vactorLen; i++)
   {
    tempK[i] = (byte) key.charAt((i % key.length()));
   }
   j = 0;
   // 2.5 置换vactor值
   for (int i = 0; i < 255; i++)
   {
    j = (j + vactor[i] + tempK[i]) % vactorLen;
    int temp = vactor[i];
    vactor[i] = vactor[j];
    vactor[j] = temp;
   }
   int i = 0;
   j = 0;
   // 2.6把data定义成char[]数组dataChar
   char[] dataChar = data.toCharArray();
   // 2.7计算数组dataChar[]长度,用InputCharLen接收。
   int charLen = dataChar.length;
   // 2.8 定义 长度为InputCharLen的char数组iOutputChar[]
   char[] resultChar = new char[charLen];
   // 2.9 加密解密算法
   for (short x = 0; x < charLen; x++)
   {
    i = (i + 1) % vactorLen;
    j = (j + vactor[i]) % vactorLen;
    int temp = vactor[i];
    vactor[i] = vactor[j];
    vactor[j] = temp;
    char tempChar = (char) vactor[(vactor[i] + (vactor[j] % vactorLen))
      % vactorLen];
    resultChar[x] = (char) (dataChar[x] ^ tempChar);
   }
   // 3. 输出结果
   return new String(resultChar);
  }
  return null;
 }
 /**
  * 二进制转换16进制*********
  * 
  * @param buf
  * @return
  */
 private static String parseByte2HexStr(byte bytes[])
 {
  // 参数检查
  if (bytes != null && bytes.length > 0)
  {
   // 定义b长度为bLenth
   int bLenth = bytes.length;
   StringBuffer stringBuffer = new StringBuffer();
   for (int i = 0; i < bLenth; i++)
   {
    String hex = Integer.toHexString(bytes[i] & 0xFF);
    if (hex.length() == 1)
    {
     hex = '0' + hex;
    }
    stringBuffer.append(hex.toUpperCase());
   }
   return stringBuffer.toString();
  }
  return null;
 }
 // 测试**************
 public static void main(String[] args)
 {
  // String data = "梦想还是要有的,万一实现了呢?";
  String data = "s.,gKsf发$@($*@¥棥梦想hai¥##@……LSLK";
  String key = "abcdefa";
  // 原文
  System.out.println("加密前:" + data);
  // 密文
  String str = null;
  if (data != null && data.length() > 0)
  {
   str = encodeAndDecodeStr(data, key);
   System.out.println("加密后:" + parseByte2HexStr(str.getBytes()));
   // 解密
   System.out.println("解密后:"
     + new String(encodeAndDecodeStr(str, key)));
  }
 }
}


相关文章
|
Java 数据安全/隐私保护
Java实现最电话号码的简单加密源码
Java实现最电话号码的简单加密源码
17 0
|
2月前
|
存储 安全 算法
【接口加密】Java中的接口加密实践
【接口加密】Java中的接口加密实践
|
Java 数据安全/隐私保护
java实现加密电话号码,有具体的加密流程注释
java实现加密电话号码,有具体的加密流程注释
19 0
|
11天前
|
Java 数据安全/隐私保护
java base64 加密 解密
java base64 加密 解密
|
21天前
|
编解码 算法 安全
【Java技术专题】「入门到精通系列」深入探索Java技术中常用到的六种加密技术和实现
【Java技术专题】「入门到精通系列」深入探索Java技术中常用到的六种加密技术和实现
44 0
|
28天前
|
安全 Java 数据安全/隐私保护
提升 Java 编程安全性 - 代码加密混淆工具的重要性和应用
提升 Java 编程安全性 - 代码加密混淆工具的重要性和应用
|
29天前
|
安全 小程序 Java
java实现微信服务(公众)号用户关注时,获取openid,安全模式下的加密解密实现
java实现微信服务(公众)号用户关注时,获取openid,安全模式下的加密解密实现
17 0
|
1月前
|
前端开发 安全 Java
Java 新手如何使用Spring MVC RestAPI的加密
Java 新手如何使用Spring MVC RestAPI的加密
|
1月前
|
存储 算法 安全
Java代码能实现这些隐藏的加密功能
Java代码能实现这些隐藏的加密功能
57 0
|
2月前
|
Java 数据安全/隐私保护
6-4 字符串加密(Java解法,两种网上的类型题)
6-4 字符串加密(Java解法,两种网上的类型题)
23 0