我正在尝试使用对话框中的字符串值来加密文件。我已经能够使用AES以正常方式加密文件。该文件存储在SDCard上。但是当我从Dialog Box中获取一个值并将其用于加密时,我得到了大小为零字节的加密文件。这是我的代码:
public class MainActivity extends Activity {
//@SuppressWarnings("unchecked")
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
try {
encrypt(value);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
alert.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int
whichButton) {
// Canceled.
}
});
alert.show(); }
static void encrypt(String Value) throws IOException,
NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
final String libPath = Environment.getExternalStorageDirectory() + "/shoaib.jar";
// Here you read the cleartext.
FileInputStream fis = new FileInputStream(libPath);
// This stream write the encrypted text.
FileOutputStream fos = new
FileOutputStream(Environment.getExternalStorageDirectory() + "/encrypted.jar");
// Length is 16 byte
SecretKeySpec sks = new SecretKeySpec(Value.getBytes(), "AES");
Log.d(TAG,Value);
// Create cipher
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
// Wrap the output stream
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
// Write bytes
int b;
byte[] d = new byte[8];
while((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
// Flush and close streams.
cos.flush();
cos.close();
fis.close();
}
现在有人可以告诉我哪里出错了。
提前致谢:)
答案 0 :(得分:0)
试试这段代码,
public static String Encrypt(String text, String key)
{
String Encoded = "";
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] keyBytes = new byte[16];
byte[] b = key.getBytes("UTF-8");
int len = b.length;
if (len > keyBytes.length)
{
len = keyBytes.length;
}
System.arraycopy(b, 0, keyBytes, 0, len);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
//IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
final byte[] iv = new byte[16];
Arrays.fill(iv, (byte) 0x00);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, keySpec,ivSpec);
byte[] results = cipher.doFinal(text.getBytes("UTF-8"));
Encoded = Base64.encodeToString(results,Base64.URL_SAFE);
} catch (NoSuchAlgorithmException e) {
Log.v(TAG, e.getMessage());
e.printStackTrace();
} catch (NoSuchPaddingException e) {
Log.v(TAG, e.getMessage());
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
Log.v(TAG, e.getMessage());
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
Log.v(TAG, e.getMessage());
e.printStackTrace();
} catch (BadPaddingException e) {
Log.v(TAG, e.getMessage());
e.printStackTrace();
} catch (InvalidKeyException e) {
Log.v(TAG, e.getMessage());
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
Log.v(TAG, e.getMessage());
e.printStackTrace();
}
return Encoded;
}
答案 1 :(得分:0)
我使用这个课程:DesEncrypter。
在我的代码中:
String STR = "sIwDraAEhBsRaKXMcvgW6jiRV+6FK11LbbFrAqLrc9qlJqCVhExyQg==";
String key = "yourkey"
DesEncrypter encrypter = new DesEncrypter(key);
String decryptedStr = encrypter.decrypt(STR));
String encrypteddStr = encrypter.encrypt(decryptedStr));
OR:
String STR = "Hello world!";
String key = "yourkey"
DesEncrypter encrypter = new DesEncrypter(key);
String encrypteddStr = encrypter.encrypt(STR));