实施充气城堡密码算法与Android [英] Implementing Bouncy Castle Cipher Algorithms with Android

查看:150
本文介绍了实施充气城堡密码算法与Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用充气城堡供应商来实现算法,比如蛇和Twofish的,因为Sun的提供者不实现这些的。我知道,当多个供应商可以实现相同的算法,你得到的实施形式的排名最高的供应商这将是Sun提供。如果由于某种原因,你想用从一个特定的提供者(可能是因为你知道这是更快)的实施,您可以指定的getInstance两ARG版本的提供者()。在我的情况下,太阳提供者不实现我感兴趣的是,在所有的算法。

How can I use the Bouncy Castle provider to implement algorithms such as Serpent and Twofish, because Sun's providers don't implement these at all. I know when multiple providers can implement the same algorithm, you get the implementation form the highest ranking provider which is going to be the Sun provider. If for some reason you want to use the implementation from a specific provider (perhaps because you know it is faster), you can specify the provider in two-arg version of getInstance(). In my case, the Sun provider doesn't implement the algorithms I'm interested in at all.

我试图实现蛇:

    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();
        }       
    }

不过,这将引发

This however throws a

java.security.NoSuchAlgorithmException:   毒蛇/ CBC / PKCS7Padding

java.security.NoSuchAlgorithmException: Serpent/CBC/PKCS7Padding

在哪里我所说的行

密码=   Cipher.getInstance(转换,   供应商);

cipher = Cipher.getInstance(TRANSFORMATION, PROVIDER);

运行

$ adb shell
# logcat

我得到吨

not resolving ambiguous class
not verifying
multiple definitions

被输出错误。任何想法什么可以导致这种情况发生,我怎么能解决这个问题?

errors being outputted. Any idea what can be causing this to happen and how I can resolve the issue?

推荐答案

您可能需要海绵城堡 - 一个重新包装我制作充气城堡的专门针对Android的。

You probably want Spongy Castle - a repackage I made of Bouncy Castle specifically targeted for Android.

海绵城堡是一个完整的替代充气城堡加密库,船舶与Android的残废的版本。有几个小的变化,使其在Android上工作:

Spongy Castle is a full replacement for the crippled versions of the Bouncy Castle cryptographic libraries which ship with Android. There are a couple of small changes to make it work on Android:

  • 在所有软件包名称已从org.bouncycastle *到org.spongycastle * - 。所以没有类加载器冲突
  • Java安全API提供程序名称现在是 SC ,而不是 BC
  • all package names have been moved from org.bouncycastle.* to org.spongycastle.* - so no classloader conflicts
  • the Java Security API Provider name is now SC rather than BC

海绵城堡救了我的屁股。 - 从一个快乐的用户反馈:)

"Spongy Castle saved my ass." - feedback from one happy user :)

这篇关于实施充气城堡密码算法与Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆