私がテストした文字列はjQueryソースコードでした。
プレーンテキストの長さ:78082
圧縮後:26566
暗号化長:54746
再圧縮:41647
-------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- ------------
パスワードの長さ:41647
減圧:54746
復号化後:26566
再圧縮:78082
-------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- -------------------------------------------------------------- ------------
正常に比較
DESにはjar:sun.misc.base64decoder.jarが必要です
テスト
コードコピーは次のとおりです。
public static void main(string [] args)スロー例外{
文字列cont = "";
文字列cont2 = jm(yjy(cont));
if(cont.equals(cont2)){
System.out.println( "比較成功");
}それ以外{
System.out.println( "比較失敗");
}
}
public static string yjy(string content)は例外をスローします{
System.out.println( "Plantext length:" + cont.Length());
//最初の圧縮
cont = ziputil2.compress(cont);
System.out.println( "圧縮後:" + cont.Length());
//初めて暗号化
cont = desutil.encrypt(cont、desutil.pwd_key);
System.out.println( "暗号化長:" + cont.Length());
// 2番目の圧縮
cont = ziputil2.compress(cont);
System.out.println( "Recompress:" + cont.Length());
contを返します。
}
public static string jm(string content)は例外をスローします{
system.out.println( "----------------------------------);
System.out.println( "ciphertext length:" + cont.Length());
//初めて減圧します
cont = ziputil2.uncompress(cont);
System.out.println( "Decompress:" + cont.Length());
//初めて復号化されます
cont = desutil.decrypt(cont、desutil.pwd_key);
System.out.println( "decryption:" + cont.Length());
// 2番目の減圧
cont = ziputil2.uncompress(cont);
System.out.println( "Rextract:" + cont.Length());
contを返します。
}
Desutil
コードコピーは次のとおりです。
java.io.ioexceptionをインポートします。
java.security.securerandomをインポートします。
javax.crypto.cipherをインポートします。
javax.crypto.secretkeyをインポートします。
javax.crypto.secretkeyFactoryをインポートします。
javax.crypto.spec.deskeyspecをインポートします。
decoder.base64Decoderをインポートします。
decoder.base64encoderをインポートします。
パブリッククラスDesutil {
プライベート最終静的文字列des = "des";
public final static string pwd_key = "mzthpwdjm";
public final static string id_key = "mzthidjm";
public static void main(string [] args)スロー例外{
文字列data = "xkajsdasdk'al; ks'dl; kasl; d";
system.err.println( "encrypt:"+encrypt(data、pwd_key));
System.err.println( "decrypt:" +decrypt(encrypt(data、pwd_key)、pwd_key));
}
/**
*説明キー値に基づく暗号化
*
* @paramデータ
* @paramキー
*暗号化キーバイト配列
* @戻る
* @Throws例外
*/
public static string encrypt(string data、string key)スロー例外{
byte [] bt = encrypt(data.getBytes()、key.getBytes());
string strs = new base64Encoder()。エンコード(bt);
strsを返します。
}
/**
*説明キー値に従って復号化されます
*
* @paramデータ
* @paramキー
*暗号化キーバイト配列
* @戻る
* @throws ioexception
* @Throws例外
*/
public static string decrypt(string data、string key)はioexceptionをスローします。
例外 {
if(data == null)
nullを返します。
base64Decoder decoder = new Base64Decoder();
byte [] buf = decoder.decodebuffer(data);
byte [] bt = decrypt(buf、key.getbytes());
新しい文字列(bt)を返します。
}
/**
*説明キー値に基づく暗号化
*
* @paramデータ
* @paramキー
*暗号化キーバイト配列
* @戻る
* @Throws例外
*/
private static byte [] encrypt(byte [] data、byte [] key)スロー例外{
//信頼できる乱数ソースを生成します
securerandom sr = new Securerandom();
//元のキーデータからdeskeyspecオブジェクトを作成する
deskeyspec dks = new deskeyspec(key);
//キーファクトリーを作成し、それを使用してDeskeysPecをSecretKeyオブジェクトに変換します
SecretKeyFactory KeyFactory = SecretKeyFactory.getInstance(des);
SecretKey SecureKey = keyFactory.generateSecret(dks);
//暗号オブジェクトは実際に暗号化操作を完了します
cipher cipher = cipher.getInstance(des);
//キーを使用して暗号オブジェクトを初期化します
cipher.init(cipher.encrypt_mode、securekey、sr);
cipher.dofinal(data)を返します。
}
/**
*説明キー値に従って復号化されます
*
* @paramデータ
* @paramキー
*暗号化キーバイト配列
* @戻る
* @Throws例外
*/
private static byte [] decrypt(byte [] data、byte [] key)スロー例外{
//信頼できる乱数ソースを生成します
securerandom sr = new Securerandom();
//元のキーデータからdeskeyspecオブジェクトを作成する
deskeyspec dks = new deskeyspec(key);
//キーファクトリーを作成し、それを使用してDeskeysPecをSecretKeyオブジェクトに変換します
SecretKeyFactory KeyFactory = SecretKeyFactory.getInstance(des);
SecretKey SecureKey = keyFactory.generateSecret(dks);
//暗号オブジェクトは実際に復号化操作を完了します
cipher cipher = cipher.getInstance(des);
//キーを使用して暗号オブジェクトを初期化します
cipher.init(cipher.decrypt_mode、securekey、sr);
cipher.dofinal(data)を返します。
}
}
ziputil2
。
コードコピーは次のとおりです。
java.io.bytearrayinputStreamをインポートします。
java.io.bytearrayoutputStreamをインポートします。
java.io.ioexceptionをインポートします。
java.util.zip.gzipinputStreamをインポートします。
java.util.zip.gzipoutputStreamをインポートします。
// zipモードで文字列を圧縮して解凍します
パブリッククラスziputil2 {
//テスト方法
public static void main(string [] args)throws ioexception {
//文字列をテストします
文字列str = "";
system.out.println( "元の長さ:" + str.length());
System.out.println( "圧縮後:" + ziputil2.compress(str).length());
System.out
.println( "decompress:" + ziputil2.uncompress(ziputil2.compress(str)));
}
//圧縮
public static string compress(string str)throws ioexception {
if(str == null || str.length()== 0){
strを返します。
}
bytearrayoutputStream out = new bytearrayoutputStream();
gzipoutputStream gzip = new gzipoutputStream(out);
gzip.write(str.getBytes());
gzip.close();
return.toString( "ISO-8859-1");
}
//減圧
public static string uncompress(string str)throws ioexception {
if(str == null || str.length()== 0){
strを返します。
}
bytearrayoutputStream out = new bytearrayoutputStream();
bytearrayInputStream in = new bytearrayinputStream(
str.getBytes( "ISO-8859-1"));
gzipinputStream gunzip = new gzipinputStream(in);
byte [] buffer = new byte [256];
int n;
while((n = gunzip.read(buffer))> = 0){
out.write(buffer、0、n);
}
// toString()はプラットフォームのデフォルトエンコードを使用し、toString(GBK)などの明示的に指定することもできます
return.toString();
}
}