求大神用java实现RC4的加密,解密功能,高分悬赏.
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求大神用java实现RC4的加密,解密功能,高分悬赏.相关的知识,希望对你有一定的参考价值。
求大神用java实现RC4的加密,解密功能,高分悬赏.
import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
public class Test
public static void main(String[] args) throws Exception
Cipher cipher=Cipher.getInstance("RC4");
String pwd="123456";
String ptext="Hello World 你好";
SecretKeySpec key=new SecretKeySpec(pwd.getBytes("UTF-8"), "RC4");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cdata =cipher.update(ptext.getBytes("UTF-8"));
//解密
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] ddata =cipher.update(cdata);
System.out.println("密码: "+pwd);
System.out.println("明文: "+ptext);
System.out.println("密文: "+DatatypeConverter.printHexBinary(cdata));
System.out.println("解密文: "+new String(ddata,"UTF-8"));
密码: 123456
明文: Hello World 你好
密文: 489D120B4B1342F30D5B46961D83E12B4875
解密文: Hello World 你好
RC4已经不太安全,只能用于一般加密,不能用于金融等紧要场合。
参考技术A /*** 加解密方法
* @param aInput 加解密文本
* @param aKey 加解密密码
* @return
*/
public static String HloveyRC4(String aInput,String aKey)
int[] iS = new int[256];
byte[] iK = new byte[256];
for (int i=0;i<256;i++)
iS[i]=i;
int j = 1;
for (short i= 0;i<256;i++)
iK[i]=(byte)aKey.charAt((i % aKey.length()));
j=0;
for (int i=0;i<255;i++)
j=(j+iS[i]+iK[i]) % 256;
int temp = iS[i];
iS[i]=iS[j];
iS[j]=temp;
int i=0;
j=0;
char[] iInputChar = aInput.toCharArray();
char[] iOutputChar = new char[iInputChar.length];
for(short x = 0;x<iInputChar.length;x++)
i = (i+1) % 256;
j = (j+iS[i]) % 256;
int temp = iS[i];
iS[i]=iS[j];
iS[j]=temp;
int t = (iS[i]+(iS[j] % 256)) % 256;
int iY = iS[t];
char iCY = (char)iY;
iOutputChar[x] =(char)( iInputChar[x] ^ iCY) ;
return new String(iOutputChar);
一个php加密方法,怎么用java实现,高分!
最近做一个php项目的改写,里面有个调第三方webservices用到的加密方法,怎么样用java来实现它。
php代码:
function getSecureString($s)
$key = pack("H*" , '145b206c283b3c534a3822454c313a4f');
$iv = pack("H*" , '343c232c2a1b123d');
$encrypted = openssl_encrypt($s, 'bf-cbc', $key, OPENSSL_RAW_DATA, $iv);
$encryptedString = base64_encode($encrypted);
return $encryptedString;
好像里面的openssl_encrypt方法,我在网上找都找不到,不知道内部是怎么实现的。
第三方又是老外,交流不来,给了我文档,里面也没有提到这个加密原理。
50分求解答!答案解决了加50分
bf-cbc的加密方式,求java实现代码,跪求啊,快被逼疯
Java OpenSSLPBEInputStream
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class OpenSSLPBEInputStream extends InputStream
private final static int READ_BLOCK_SIZE = 64 * 1024;
private final Cipher cipher;
private final InputStream inStream;
private final byte[] bufferCipher = new byte[READ_BLOCK_SIZE];
private byte[] bufferClear = null;
private int index = Integer.MAX_VALUE;
private int maxIndex = 0;
public OpenSSLPBEInputStream(final InputStream streamIn, String algIn, int iterationCount, char[] password)
throws IOException
this.inStream = streamIn;
try
byte[] salt = readSalt();
cipher = OpenSSLPBECommon.initializeCipher(password, salt, Cipher.DECRYPT_MODE, algIn, iterationCount);
catch (Exception e)
throw new IOException(e);
@Override
public int available() throws IOException
return inStream.available();
@Override
public int read() throws IOException
if (index > maxIndex)
index = 0;
int read = inStream.read(bufferCipher);
if (read != -1)
bufferClear = cipher.update(bufferCipher, 0, read);
if (read == -1 || bufferClear == null || bufferClear.length == 0)
try
bufferClear = cipher.doFinal();
catch (Exception e)
bufferClear = null;
if (bufferClear == null || bufferClear.length == 0)
return -1;
maxIndex = bufferClear.length - 1;
if (bufferClear == null || bufferClear.length == 0)
return -1;
return bufferClear[index++] & 0xff;
private byte[] readSalt() throws IOException
byte[] headerBytes = new byte[OpenSSLPBECommon.OPENSSL_HEADER_STRING.length()];
inStream.read(headerBytes);
String headerString = new String(headerBytes, OpenSSLPBECommon.OPENSSL_HEADER_ENCODE);
if (!OpenSSLPBECommon.OPENSSL_HEADER_STRING.equals(headerString))
throw new IOException("unexpected file header " + headerString);
byte[] salt = new byte[OpenSSLPBECommon.SALT_SIZE_BYTES];
inStream.read(salt);
return salt;
Java OpenSSLPBEOutputStream
import java.io.IOException;import java.io.OutputStream;
import java.security.SecureRandom;
import javax.crypto.Cipher;
public class OpenSSLPBEOutputStream extends OutputStream
private static final int BUFFER_SIZE = 5 * 1024 * 1024;
private final Cipher cipher;
private final OutputStream outStream;
private final byte[] buffer = new byte[BUFFER_SIZE];
private int bufferIndex = 0;
public OpenSSLPBEOutputStream(final OutputStream outputStream, String algIn, int iterationCount,
char[] password) throws IOException
outStream = outputStream;
try
/* Create and use a random SALT for each instance of this output stream. */
byte[] salt = new byte[OpenSSLPBECommon.SALT_SIZE_BYTES];
new SecureRandom().nextBytes(salt);
cipher = OpenSSLPBECommon.initializeCipher(password, salt, Cipher.ENCRYPT_MODE, algIn, iterationCount);
/* Write header */
writeHeader(salt);
catch (Exception e)
throw new IOException(e);
@Override
public void write(int b) throws IOException
buffer[bufferIndex] = (byte) b;
bufferIndex++;
if (bufferIndex == BUFFER_SIZE)
byte[] result = cipher.update(buffer, 0, bufferIndex);
outStream.write(result);
bufferIndex = 0;
@Override
public void flush() throws IOException
if (bufferIndex > 0)
byte[] result;
try
result = cipher.doFinal(buffer, 0, bufferIndex);
outStream.write(result);
catch (Exception e)
throw new IOException(e);
bufferIndex = 0;
@Override
public void close() throws IOException
flush();
outStream.close();
private void writeHeader(byte[] salt) throws IOException
outStream.write(OpenSSLPBECommon.OPENSSL_HEADER_STRING.getBytes(OpenSSLPBECommon.OPENSSL_HEADER_ENCODE));
outStream.write(salt);
Main Class 测试以上两个class
import javax.crypto.Cipher;import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
class OpenSSLPBECommon
protected static final int SALT_SIZE_BYTES = 8;
protected static final String OPENSSL_HEADER_STRING = "Salted__";
protected static final String OPENSSL_HEADER_ENCODE = "ASCII";
protected static Cipher initializeCipher(char[] password, byte[] salt, int cipherMode,
final String algorithm, int iterationCount) throws NoSuchAlgorithmException, InvalidKeySpecException,
InvalidKeyException, NoSuchPaddingException, InvalidAlgorithmParameterException
PBEKeySpec keySpec = new PBEKeySpec(password);
SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm);
SecretKey key = factory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(cipherMode, key, new PBEParameterSpec(salt, iterationCount));
return cipher;
来源引用
https://github.com/guardianproject/ChatSecureAndroid/tree/master/src/info/guardianproject/otr
参考技术A 先找出来是什么算法,JAVA 里面的现成的算法还是较多的。看加密的方法,应该是 blowfish 请百度 还是较容易找到的 blowfish JAVA 实现
以上是关于求大神用java实现RC4的加密,解密功能,高分悬赏.的主要内容,如果未能解决你的问题,请参考以下文章