- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用以下示例:
http://compscipleslab.wordpress.com/category/java/
但是我遇到了异常,当我记录异常时,它出现在我的 logcat 中,说明:InvalidKeyException
我不确定我到底做错了什么。我还没有真正修改教程中的方法(也许这就是问题所在),所以我不确定为什么会出现此错误。
public class FileChooser extends ListActivity {
private File currentDir;
private File moveToDir;
private FileArrayAdapter adapter;
public static String TAG = "DEBUG THIS:";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentDir = new File("/sdcard/");
fill(currentDir);
registerForContextMenu(getListView());
// get the attachment's filename
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(this);
String attachmentFileName = preferences.getString("fileName", "");
// save the attachment
try {
InputStream attachment = getContentResolver().openInputStream(
getIntent().getData());
File savedFile = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath(), attachmentFileName);
FileOutputStream f = new FileOutputStream(savedFile);
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = attachment.read(buffer)) > 0) {
f.write(buffer);
}
f.close();
} catch (Exception e) {
}
}
// File Manager Source to view SD Card or Internal Storage Contents
private void fill(File f) {
File[] dirs = f.listFiles();
this.setTitle("Current Dir: " + f.getName());
List<Item> dir = new ArrayList<Item>();
List<Item> fls = new ArrayList<Item>();
try {
for (File ff : dirs) {
Date lastModDate = new Date(ff.lastModified());
DateFormat formater = DateFormat.getDateTimeInstance();
String date_modify = formater.format(lastModDate);
if (ff.isDirectory()) {
File[] fbuf = ff.listFiles();
int buf = 0;
if (fbuf != null) {
buf = fbuf.length;
} else
buf = 0;
String num_item = String.valueOf(buf);
if (buf == 0)
num_item = num_item + " item";
else
num_item = num_item + " items";
// String formated = lastModDate.toString();
dir.add(new Item(ff.getName(), num_item, date_modify, ff
.getAbsolutePath(), "directory_icon"));
} else {
fls.add(new Item(ff.getName(), ff.length() + " Byte",
date_modify, ff.getAbsolutePath(), "file_icon"));
}
}
} catch (Exception e) {
}
Collections.sort(dir);
Collections.sort(fls);
dir.addAll(fls);
if (!f.getName().equalsIgnoreCase("sdcard"))
dir.add(0, new Item("..", "Parent Directory", "", f.getParent(),
"directory_up"));
adapter = new FileArrayAdapter(FileChooser.this, R.layout.file_view,
dir);
this.setListAdapter(adapter);
}
// onClick listener to move back one directory
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Item o = adapter.getItem(position);
if (o.getImage().equalsIgnoreCase("directory_icon")
|| o.getImage().equalsIgnoreCase("directory_up")) {
currentDir = new File(o.getPath());
fill(currentDir);
} else {
onFileClick(o);
}
}
// open file onClick
private void onFileClick(Item o) {
Intent intent = new Intent();
intent.putExtra("GetPath", currentDir.toString());
intent.putExtra("GetFileName", o.getName());
setResult(RESULT_OK, intent);
finish();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Copy");
menu.add(0, v.getId(), 0, "Paste");
menu.add(0, v.getId(), 0, "Delete");
menu.add(0, v.getId(), 0, "Encrypt");
menu.add(0, v.getId(), 0, "Decrypt");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Copy") {
function1(item.getItemId());
}
if (item.getTitle() == "Paste") {
function2(item.getItemId());
}
if (item.getTitle() == "Delete") {
function3(item.getItemId());
}
if (item.getTitle() == "Encrypt") {
function4(item.getItemId());
}
if (item.getTitle() == "Decrypt") {
function5(item.getItemId());
} else {
}
return false;
}
public void function1(int id) {
Toast.makeText(this, "Copy", Toast.LENGTH_SHORT).show();
try {
File sd = Environment.getExternalStorageDirectory();
currentDir = new File(sd.getAbsolutePath());
moveToDir = new File(sd.getAbsolutePath());
copyDirectoryOneLocationToAnotherLocation(currentDir, currentDir);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void function2(int id) {
Toast.makeText(this, "Paste", Toast.LENGTH_SHORT).show();
}
public void function3(int id) {
Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();
DeleteRecursive(currentDir);
}
public void function4(int id) {
Toast.makeText(this, "Encrypt", Toast.LENGTH_SHORT).show();
//encrypt(null, null, null);
// encrypt(String PlaintextFile, String Key, String CiphertextFile);
encrypt("D:\\plaintext.txt", "testkey", "D:\\ciphertext.txt");
}
public void function5(int id) {
Toast.makeText(this, "Decrypt", Toast.LENGTH_SHORT).show();
decrypt(null, null, null);
// decrypt(String PlaintextFile, String Key, String CiphertextFile);
}
private void DeleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory()) {
File[] children = fileOrDirectory.listFiles();
if (null != children) {
for (File child : children)
DeleteRecursive(child);
}
}
fileOrDirectory.delete();
}
public static void copyDirectoryOneLocationToAnotherLocation(
File sourceLocation, File targetLocation) throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists()) {
targetLocation.mkdir();
}
String[] children = sourceLocation.list();
for (int i = 0; i < sourceLocation.listFiles().length; i++) {
copyDirectoryOneLocationToAnotherLocation(new File(
sourceLocation, children[i]), new File(targetLocation,
children[i]));
}
} else {
InputStream in = new FileInputStream(Environment
.getExternalStorageDirectory().getPath());
OutputStream out = new FileOutputStream(Environment
.getExternalStorageDirectory().getPath());
Log.d("TEST", "source: " + in);
Log.d("TEST", "target: " + out);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
public static void encrypt(String PlaintextFile, String Key,
String CiphertextFile) {
try {
byte[] KeyData = Key.getBytes();
SecretKeySpec KS = new SecretKeySpec(KeyData, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, KS);
FileInputStream fis = new FileInputStream(PlaintextFile);
CipherInputStream cis = new CipherInputStream(fis, cipher);
FileOutputStream fos = new FileOutputStream(CiphertextFile);
byte[] b = new byte[1024];
int i = cis.read(b);
while (i != -1) {
fos.write(b, 0, i);
i = cis.read(b);
}
fos.flush();
fos.close();
fis.close();
cis.close();
System.out.println("Encryption Successfull !!!");
} catch (NoSuchAlgorithmException exception) {
// do something.
Thread.currentThread().getStackTrace();
} catch (NoSuchPaddingException exception) {
// do something (else?).
Thread.currentThread().getStackTrace();
} catch (InvalidKeyException exception) {
// do something.
Thread.currentThread().getStackTrace();
} catch (IOException exception) {
// do something (else?).
Thread.currentThread().getStackTrace();
}
}
public static void decrypt(String CiphertextFile, String Key,
String DecipheredFile) {
try {
byte[] KeyData = Key.getBytes();
SecretKeySpec KS = new SecretKeySpec(KeyData, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, KS);
FileInputStream fis = new FileInputStream(CiphertextFile);
FileOutputStream fos = new FileOutputStream(DecipheredFile);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
byte[] b = new byte[1024];
int i = fis.read(b);
while (i != -1) {
cos.write(b, 0, i);
i = fis.read(b);
}
fos.flush();
fos.close();
fis.close();
cos.flush();
cos.close();
System.out.println("Decryption Successfull !!!");
} catch (NoSuchAlgorithmException exception) {
// do something.
Thread.currentThread().getStackTrace();
} catch (NoSuchPaddingException exception) {
// do something (else?).
Thread.currentThread().getStackTrace();
} catch (InvalidKeyException exception) {
// do something.
Thread.currentThread().getStackTrace();
} catch (IOException exception) {
// do something (else?).
Thread.currentThread().getStackTrace();
}
}
public static void main(String[] args) {
encrypt("D:\\plaintext.txt", "testkey", "D:\\ciphertext.txt");
decrypt("D:\\ciphertext.txt", "testkey", "D:\\originaltext.txt");
}
}
01-14 17:53:07.904: I/SurfaceTextureClient(9545): [STC::queueBuffer] (this:0x5f1c6d58) fps:6.11, dur:2780.41, max:2503.10, min:13.29
01-14 17:53:07.905: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:07.905: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:07.958: V/Provider/Settings(9545): invalidate [system]: current 6 != cached 0
01-14 17:53:07.963: V/Provider/Settings(9545): from db cache, name = sound_effects_enabled , value = 0
01-14 17:53:07.966: D/skia(9545): Flag is not 10
01-14 17:53:07.976: W/System.err(9545): java.security.InvalidKeyException: Key length not 128/192/256 bits.
01-14 17:53:07.983: W/System.err(9545): at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineInit(BaseBlockCipher.java:578)
01-14 17:53:07.983: W/System.err(9545): at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineInit(BaseBlockCipher.java:625)
01-14 17:53:07.983: W/System.err(9545): at javax.crypto.Cipher.init(Cipher.java:519)
01-14 17:53:07.983: W/System.err(9545): at javax.crypto.Cipher.init(Cipher.java:479)
01-14 17:53:07.983: W/System.err(9545): at com.idg.voiscphone.FileChooser.encrypt(FileChooser.java:276)
01-14 17:53:07.984: W/System.err(9545): at com.idg.voiscphone.FileChooser.function4(FileChooser.java:209)
01-14 17:53:07.984: W/System.err(9545): at com.idg.voiscphone.FileChooser.onContextItemSelected(FileChooser.java:169)
01-14 17:53:07.984: W/System.err(9545): at android.app.Activity.onMenuItemSelected(Activity.java:2597)
01-14 17:53:07.984: W/System.err(9545): at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3663)
01-14 17:53:07.984: W/System.err(9545): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
01-14 17:53:07.985: W/System.err(9545): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
01-14 17:53:07.985: W/System.err(9545): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
01-14 17:53:07.985: W/System.err(9545): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
01-14 17:53:07.985: W/System.err(9545): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:963)
01-14 17:53:07.985: W/System.err(9545): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
01-14 17:53:07.985: W/System.err(9545): at android.widget.AbsListView.performItemClick(AbsListView.java:1128)
01-14 17:53:07.986: W/System.err(9545): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2812)
01-14 17:53:07.986: W/System.err(9545): at android.widget.AbsListView$1.run(AbsListView.java:3571)
01-14 17:53:07.986: W/System.err(9545): at android.os.Handler.handleCallback(Handler.java:725)
01-14 17:53:07.986: W/System.err(9545): at android.os.Handler.dispatchMessage(Handler.java:92)
01-14 17:53:07.986: W/System.err(9545): at android.os.Looper.loop(Looper.java:153)
01-14 17:53:07.987: W/System.err(9545): at android.app.ActivityThread.main(ActivityThread.java:5297)
01-14 17:53:07.987: W/System.err(9545): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 17:53:07.987: W/System.err(9545): at java.lang.reflect.Method.invoke(Method.java:511)
01-14 17:53:07.987: W/System.err(9545): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-14 17:53:07.988: W/System.err(9545): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-14 17:53:07.988: W/System.err(9545): at dalvik.system.NativeStart.main(Native Method)
01-14 17:53:07.989: D/OpenGLRenderer(9545): Flushing caches (mode 0)
01-14 17:53:08.013: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.013: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.028: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.028: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.052: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.053: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.054: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.076: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.076: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.076: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.078: V/InputMethodManager(9545): onWindowFocus: android.widget.ListView{4206e2c0 VFED.VCL .F...... 0,0-720,1126 #102000a android:id/list} softInputMode=16 first=false flags=#1810100
01-14 17:53:08.091: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.091: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.091: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.092: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.092: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.092: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.097: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
01-14 17:53:08.102: W/Trace(9545): Unexpected value from nativeGetEnabledTags: 0
最佳答案
您的代码存在一个问题,即您为 Cipher
和 SecretKeySpec
实例指定的算法不相同。您正在混合两种不同的算法。
与引用示例一样,两个实例保持相同:
错误行:
SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");
Cipher cipher = Cipher.getInstance("AES");
您可以考虑阅读 AES vs Blowfish here .
编辑:
如前所述,整个解决方案将严格使用 AES。因此,您设置的 key 不是有效的 AES key - AES key 是 128、192 或 256 位(从您的异常(exception)情况也可以看出)。这意味着您当前的 key 应该为 16、24 或 32 字节(尽管 testkey
为 7)。
考虑以下因素:
ThisSecurityKey1
)。可能需要考虑您的字符编码(UTF-8、UTF-16),但由于您使用的是纯文本,因此它应该可以工作。This class provides the functionality of a secret (symmetric) key generator.
此外,请尝试引用更好的 AES 加密示例 - 您选择的引用文献充满了不兼容的 Java 编码标准。
关于java - InvalidKeyException 加密-解密文件 - Java/Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21124475/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!