Java 用Android实现Bouncy-Castle密码算法
我如何使用Bouncy Castle提供程序来实现诸如蛇和Twofish之类的算法,因为Sun的提供程序根本不实现这些算法。我知道当多个提供者可以实现同一个算法时,您可以从排名最高的提供者(即Sun提供者)获得实现。如果出于某种原因希望使用特定提供程序的实现(可能是因为您知道它更快),可以在getInstance()的两个arg版本中指定提供程序。在我的例子中,Sun提供程序根本没有实现我感兴趣的算法 我已尝试实现蛇:Java 用Android实现Bouncy-Castle密码算法,java,android,encryption,cryptography,bouncycastle,Java,Android,Encryption,Cryptography,Bouncycastle,我如何使用Bouncy Castle提供程序来实现诸如蛇和Twofish之类的算法,因为Sun的提供程序根本不实现这些算法。我知道当多个提供者可以实现同一个算法时,您可以从排名最高的提供者(即Sun提供者)获得实现。如果出于某种原因希望使用特定提供程序的实现(可能是因为您知道它更快),可以在getInstance()的两个arg版本中指定提供程序。在我的例子中,Sun提供程序根本没有实现我感兴趣的算法 我已尝试实现蛇: public static final String FILE_EX
public static final String FILE_EXTENSION = ".serpent";
public static final String PROVIDER = "BC"; // Bouncy Castle
public static final String BLOCK_CIPHER = "Serpent";
public static final String TRANSFORMATION = "Serpent/CBC/PKCS7Padding";
public static final String KEY_ALGORITHM = "PBKDF2WithHmacSHA1";
public static final String PRNG_ALGORITHM = "SHA1PRNG";
public static final int BLOCK_SIZE = 128; // bits
public static final int KEY_SIZE = 256; // bits
public static final int ITERATION_COUNT = 1024; // for PBE
/** Performs the encryption of a file. */
public void encrypt(String pathname, char[] password, byte[] salt) {
// Use bouncy castle as our provider
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
// Convert the file into a byte array
byte[] plaintext = fileToBytes(new File(pathname));
// Generate a 256-bit key
SecretKey key = generateSecretKey(password,salt);
// Generate a 128-bit secure random IV
byte[] iv = generateIV();
// Setup the cipher and perform the encryption
Cipher cipher = null;
byte[] ciphertext = null;
try {
cipher = Cipher.getInstance(TRANSFORMATION, PROVIDER);
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
ciphertext = cipher.doFinal(plaintext);
} catch (Exception e) {
e.printStackTrace();
}
// Append the IV to the ciphertext
byte[] temp = new byte[iv.length+ciphertext.length];
System.arraycopy(iv, 0, temp, 0, iv.length);
System.arraycopy(ciphertext, 0, temp, iv.length, ciphertext.length);
ciphertext = temp;
// Store the encrypted file in the same directory we found it
if (Environment.getExternalStorageDirectory().canWrite()) {
bytesToFile(ciphertext, pathname+FILE_EXTENSION);
File file = new File(pathname);
file.delete();
}
}
然而,这带来了一个巨大的挑战
java.security.NoSuchAlgorithmException:
蛇/CBC/PKCS7填充
在我打电话的线路上
密码=
Cipher.getInstance(转换,
供应商)
运行
$ adb shell
# logcat
我有很多钱
not resolving ambiguous class
not verifying
multiple definitions
正在输出的错误。你知道是什么导致了这种情况的发生,以及我如何解决这个问题吗 javap org.bounchycastle.jce.provider.symmetric.Serpent产生了什么?如果没有找到类,则表明bouncy castle包中缺少某些内容,或者您可能使用了不正确的类 如果您使用的是1.4VM,那么jar文件需要安装在$JAVA_HOME/jre/lib/ext中,否则它将无法正常工作 检查蛇类的zipinfo bcprov-jdk16-146.jar | grep'/symmetric/'的输出。同样,这是为了交叉检查软件包的正确安装和蛇形对称算法的存在 我做了一个简单的测试,使用了与您自己非常相似的代码,并且没有生成异常 好的,系统中存在提供程序,但实际情况是,内部bouncy castle实现在您链接的实现之前加载。内部实现不包含Serpent/CBC/PKCS7Padding代码,即使添加了bouncycastle提供程序,也不会替换已注册的提供程序 内部提供者只实现少数密码,即。AES、ARC4、河豚和DESede 我使用以下存根活动来确定这一点:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = null;
TextView tv = new TextView(this);
setContentView(tv);
for (String name : SYMMETRIC_CIPHERS) {
try {
cipher = Cipher.getInstance(name, "BC");
tv.append(name);
tv.append("\n");
} catch (Exception e) {
}
}
}
使用BouncyCastleProvider源代码中的提供程序列表:
private static final String[] SYMMETRIC_CIPHERS =
{
"AES", "ARC4", "Blowfish", "Camellia", "CAST5", "CAST6", "DESede", "Grainv1", "Grain128", "HC128", "HC256", "IDEA",
"Noekeon", "RC5", "RC6", "Rijndael", "Salsa20", "SEED", "Serpent", "Skipjack", "TEA", "Twofish", "VMPC", "VMPCKSA3", "XTEA"
};
我知道这对你没什么帮助,但你为什么不使用AES呢?你可能想要——我用Bouncy Castle重新包装了一个专门针对Android的软件包
Spongy Castle完全取代了Android附带的Bouncy Castle加密库的残废版本。为了让它在Android上工作,有几个小改动:
- 所有包名都已从org.bouncycastle.*移动到org.spongycastle.*-因此没有类加载器冲突
- Java安全API提供程序名称现在是SC,而不是BC
“海绵城堡救了我的命。”-一位快乐用户的反馈:)对我很有用。你使用的是bouncycastle的一个古老版本吗?我实际上将此代码作为Android应用程序的一部分,这会有所不同吗?我使用的是bounty castle 1.46抱歉,我对Android不太了解。谢谢,是的,我正在运行Android adb shell->logcat,我收到了大量的不“解决不明确类”和“不验证”错误,所以我认为这是Android的问题。我将编辑我的原始帖子。zipinfo的输出显示蛇$AlgParams.class、蛇$ECB.class、蛇$KeyGen.class、蛇$Mappings.class和蛇.class都存在。在Android上运行这可能是原因吗?我更新了答案-看起来Android bouncycastle提供商只有一小部分实现中的完整密码列表(这并不奇怪)。由于运行时中的类引用在添加jar文件之前解析为内部类,因此您无法重新注册提供程序。我对Spongy Castle很陌生,您能否帮助我学习如何在我的应用程序上使用Spongy Castle?