📄 pemutilities.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: PEMUtilities.java
package org.bouncycastle.openssl;
import java.io.IOException;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.*;
import org.bouncycastle.crypto.PBEParametersGenerator;
import org.bouncycastle.crypto.generators.OpenSSLPBEParametersGenerator;
import org.bouncycastle.crypto.params.KeyParameter;
final class PEMUtilities
{
PEMUtilities()
{
}
static byte[] crypt(boolean encrypt, String provider, byte bytes[], char password[], String dekAlgName, byte iv[])
throws IOException
{
java.security.spec.AlgorithmParameterSpec paramSpec;
java.security.Key sKey;
String transformation;
paramSpec = new IvParameterSpec(iv);
String blockMode = "CBC";
String padding = "PKCS5Padding";
if (dekAlgName.endsWith("-CFB"))
{
blockMode = "CFB";
padding = "NoPadding";
}
if (dekAlgName.endsWith("-ECB") || "DES-EDE".equals(dekAlgName) || "DES-EDE3".equals(dekAlgName))
{
blockMode = "ECB";
paramSpec = null;
}
if (dekAlgName.endsWith("-OFB"))
{
blockMode = "OFB";
padding = "NoPadding";
}
String alg;
if (dekAlgName.startsWith("DES-EDE"))
{
alg = "DESede";
boolean des2 = !dekAlgName.startsWith("DES-EDE3");
sKey = getKey(password, alg, 24, iv, des2);
} else
if (dekAlgName.startsWith("DES-"))
{
alg = "DES";
sKey = getKey(password, alg, 8, iv);
} else
if (dekAlgName.startsWith("BF-"))
{
alg = "Blowfish";
sKey = getKey(password, alg, 16, iv);
} else
if (dekAlgName.startsWith("RC2-"))
{
alg = "RC2";
int keyBits = 128;
if (dekAlgName.startsWith("RC2-40-"))
keyBits = 40;
else
if (dekAlgName.startsWith("RC2-64-"))
keyBits = 64;
sKey = getKey(password, alg, keyBits / 8, iv);
if (paramSpec == null)
paramSpec = new RC2ParameterSpec(keyBits);
else
paramSpec = new RC2ParameterSpec(keyBits, iv);
} else
if (dekAlgName.startsWith("AES-"))
{
alg = "AES";
byte salt[] = iv;
if (salt.length > 8)
{
salt = new byte[8];
System.arraycopy(iv, 0, salt, 0, 8);
}
int keyBits;
if (dekAlgName.startsWith("AES-128-"))
keyBits = 128;
else
if (dekAlgName.startsWith("AES-192-"))
keyBits = 192;
else
if (dekAlgName.startsWith("AES-256-"))
keyBits = 256;
else
throw new IOException("unknown AES encryption with private key");
sKey = getKey(password, "AES", keyBits / 8, salt);
} else
{
throw new IOException("unknown encryption with private key");
}
transformation = (new StringBuilder()).append(alg).append("/").append(blockMode).append("/").append(padding).toString();
Cipher c;
c = Cipher.getInstance(transformation, provider);
int mode = encrypt ? 1 : 2;
if (paramSpec == null)
c.init(mode, sKey);
else
c.init(mode, sKey, paramSpec);
return c.doFinal(bytes);
Exception e;
e;
throw new IOException((new StringBuilder()).append("exception using cipher: ").append(e.toString()).toString());
}
private static SecretKey getKey(char password[], String algorithm, int keyLength, byte salt[])
throws IOException
{
return getKey(password, algorithm, keyLength, salt, false);
}
private static SecretKey getKey(char password[], String algorithm, int keyLength, byte salt[], boolean des2)
throws IOException
{
OpenSSLPBEParametersGenerator pGen = new OpenSSLPBEParametersGenerator();
pGen.init(PBEParametersGenerator.PKCS5PasswordToBytes(password), salt);
KeyParameter keyParam = (KeyParameter)pGen.generateDerivedParameters(keyLength * 8);
byte key[] = keyParam.getKey();
if (des2 && key.length >= 24)
System.arraycopy(key, 0, key, 16, 8);
return new SecretKeySpec(key, algorithm);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -