如何使用Bouncy Castle提供程序来实现诸如Serpent和Twofish之类的算法,因为Sun的提供商根本不实现这些算法。我知道当多个提供商可以实现相同的算法时,您将获得最高等级提供商的实施,该提供商将成为Sun提供商。如果由于某种原因您想要使用特定提供程序的实现(可能因为您知道它更快),您可以在两个arg版本的getInstance()中指定提供程序。就我而言,Sun提供商根本没有实现我感兴趣的算法。
我试图实施Serpent:
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 / PKCS7Padding
在我打电话的地方
cipher = Cipher.getInstance(改造, PROVIDER);
正在运行
$ adb shell
# logcat
我得到了很多
not resolving ambiguous class
not verifying
multiple definitions
输出错误。知道是什么导致这种情况发生以及我如何解决这个问题?
答案 0 :(得分:6)
你可能想要Spongy Castle - 我为Bouncy Castle专门针对Android制作的重新包装。
Spongy Castle完全取代了Android附带的Bouncy Castle加密库的残缺版本。有一些小改动可以让它在Android上运行:
“Spongy Castle救了我的屁股。” - 来自一个快乐用户的反馈:)
答案 1 :(得分:3)
javap org.bouncycastle.jce.provider.symmetric.Serpent会产生什么?如果它没有产生一个被发现的类,则它表示在充气城堡包中缺少某些东西,或者您可能使用了不正确的类。
如果您使用的是1.4 VM,那么需要在$ JAVA_HOME / jre / lib / ext中安装jar文件,否则它将无法正常工作IIRC。
检查zipinfo的输出bcprov-jdk16-146.jar |对于Serpent类,grep'/ symmetric /'。同样,这是交叉检查包的正确安装和Serpent对称算法的存在。
我做了一个简单的测试,使用了与你自己非常相似的代码,它没有产生异常。
好的,提供程序存在于系统中,但正在发生的是内部弹性城堡实现在链接之前加载。内部实现不包含Serpent / CBC / PKCS7Padding代码,并且即使您添加了bouncycastle提供程序,它也不会替换已经注册的提供程序。
内部提供商只实现少数密码,即。 AES,ARC4,Blowfish和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?