📄 stringencrypter.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: StringEncrypter.java
package org.gudy.azureus2.ui.console.util;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.KeySpec;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import org.bouncycastle.util.encoders.Base64;
public class StringEncrypter
{
public static class EncryptionException extends Exception
{
private static final long serialVersionUID = 0x8651debb7d8cf6ceL;
public EncryptionException(Throwable t)
{
super(t);
}
}
public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
public static final String DES_ENCRYPTION_SCHEME = "DES";
public static final String DEFAULT_ENCRYPTION_KEY = "Azureus users love their sensitive information to be encrypted";
private KeySpec keySpec;
private SecretKeyFactory keyFactory;
private Cipher cipher;
private static final String UNICODE_FORMAT = "UTF8";
public StringEncrypter(String encryptionScheme)
throws EncryptionException
{
this(encryptionScheme, "Azureus users love their sensitive information to be encrypted");
}
public StringEncrypter(String encryptionScheme, String encryptionKey)
throws EncryptionException
{
if (encryptionKey == null)
throw new IllegalArgumentException("encryption key was null");
if (encryptionKey.trim().length() < 24)
throw new IllegalArgumentException("encryption key was less than 24 characters");
try
{
byte keyAsBytes[] = encryptionKey.getBytes("UTF8");
if (encryptionScheme.equals("DESede"))
keySpec = new DESedeKeySpec(keyAsBytes);
else
if (encryptionScheme.equals("DES"))
keySpec = new DESKeySpec(keyAsBytes);
else
throw new IllegalArgumentException((new StringBuilder()).append("Encryption scheme not supported: ").append(encryptionScheme).toString());
keyFactory = SecretKeyFactory.getInstance(encryptionScheme);
cipher = Cipher.getInstance(encryptionScheme);
}
catch (InvalidKeyException e)
{
throw new EncryptionException(e);
}
catch (UnsupportedEncodingException e)
{
throw new EncryptionException(e);
}
catch (NoSuchAlgorithmException e)
{
throw new EncryptionException(e);
}
catch (NoSuchPaddingException e)
{
throw new EncryptionException(e);
}
}
public String encrypt(String unencryptedString)
throws EncryptionException
{
if (unencryptedString == null || unencryptedString.trim().length() == 0)
throw new IllegalArgumentException("unencrypted string was null or empty");
byte ciphertext[];
javax.crypto.SecretKey key = keyFactory.generateSecret(keySpec);
cipher.init(1, key);
byte cleartext[] = unencryptedString.getBytes("UTF8");
ciphertext = cipher.doFinal(cleartext);
return new String(Base64.encode(ciphertext));
Exception e;
e;
throw new EncryptionException(e);
}
public String decrypt(String encryptedString)
throws EncryptionException
{
if (encryptedString == null || encryptedString.trim().length() <= 0)
throw new IllegalArgumentException("encrypted string was null or empty");
byte ciphertext[];
javax.crypto.SecretKey key = keyFactory.generateSecret(keySpec);
cipher.init(2, key);
byte cleartext[] = Base64.decode(encryptedString);
ciphertext = cipher.doFinal(cleartext);
return bytes2String(ciphertext);
Exception e;
e;
throw new EncryptionException(e);
}
private static String bytes2String(byte bytes[])
{
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < bytes.length; i++)
stringBuffer.append((char)bytes[i]);
return stringBuffer.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -