下面列出了javax.crypto.spec.GCMParameterSpec#getTLen() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private byte[] getCipherTextBySpec(GCMParameterSpec spec) throws Exception {
// init a cipher
Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, spec);
cipher.updateAAD(AAD);
byte[] cipherText = cipher.doFinal(data);
// check IVs
if (!Arrays.equals(cipher.getIV(), spec.getIV())) {
System.out.println("IV in parameters is incorrect");
return null;
}
if (spec.getTLen() != (cipherText.length - data.length) * 8) {
System.out.println("Tag length is incorrect");
return null;
}
return cipherText;
}
private byte[] getCipherTextBySpec(GCMParameterSpec spec) throws Exception {
// init a cipher
Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, spec);
cipher.updateAAD(AAD);
byte[] cipherText = cipher.doFinal(data);
// check IVs
if (!Arrays.equals(cipher.getIV(), spec.getIV())) {
System.out.println("IV in parameters is incorrect");
return null;
}
if (spec.getTLen() != (cipherText.length - data.length) * 8) {
System.out.println("Tag length is incorrect");
return null;
}
return cipherText;
}
private static void newGCMParameterSpecPass(
int tLen, byte[] src, int offset, int len) {
try {
GCMParameterSpec gcmps =
new GCMParameterSpec(tLen, src, offset, len);
if (gcmps.getTLen() != tLen) {
throw new Exception("tLen's not equal");
}
if (!Arrays.equals(gcmps.getIV(),
Arrays.copyOfRange(src, offset, offset + len))) {
System.out.println(offset + " " + len);
System.out.println(Arrays.copyOfRange(src, offset, len)[0]);
throw new Exception("IV's not equal");
}
} catch (Exception e) {
e.printStackTrace();
failed++;
}
}
private static void newGCMParameterSpecPass(
int tLen, byte[] src, int offset, int len) {
try {
GCMParameterSpec gcmps =
new GCMParameterSpec(tLen, src, offset, len);
if (gcmps.getTLen() != tLen) {
throw new Exception("tLen's not equal");
}
if (!Arrays.equals(gcmps.getIV(),
Arrays.copyOfRange(src, offset, offset + len))) {
System.out.println(offset + " " + len);
System.out.println(Arrays.copyOfRange(src, offset, len)[0]);
throw new Exception("IV's not equal");
}
} catch (Exception e) {
e.printStackTrace();
failed++;
}
}
private byte[] getCipherTextBySpec(GCMParameterSpec spec) throws Exception {
// init a cipher
Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, spec);
cipher.updateAAD(AAD);
byte[] cipherText = cipher.doFinal(data);
// check IVs
if (!Arrays.equals(cipher.getIV(), spec.getIV())) {
System.out.println("IV in parameters is incorrect");
return null;
}
if (spec.getTLen() != (cipherText.length - data.length) * 8) {
System.out.println("Tag length is incorrect");
return null;
}
return cipherText;
}
private static void newGCMParameterSpecPass(
int tLen, byte[] src, int offset, int len) {
try {
GCMParameterSpec gcmps =
new GCMParameterSpec(tLen, src, offset, len);
if (gcmps.getTLen() != tLen) {
throw new Exception("tLen's not equal");
}
if (!Arrays.equals(gcmps.getIV(),
Arrays.copyOfRange(src, offset, offset + len))) {
System.out.println(offset + " " + len);
System.out.println(Arrays.copyOfRange(src, offset, len)[0]);
throw new Exception("IV's not equal");
}
} catch (Exception e) {
e.printStackTrace();
failed++;
}
}
private static void newGCMParameterSpecPass(
int tLen, byte[] src, int offset, int len) {
try {
GCMParameterSpec gcmps =
new GCMParameterSpec(tLen, src, offset, len);
if (gcmps.getTLen() != tLen) {
throw new Exception("tLen's not equal");
}
if (!Arrays.equals(gcmps.getIV(),
Arrays.copyOfRange(src, offset, offset + len))) {
System.out.println(offset + " " + len);
System.out.println(Arrays.copyOfRange(src, offset, len)[0]);
throw new Exception("IV's not equal");
}
} catch (Exception e) {
e.printStackTrace();
failed++;
}
}
private static void newGCMParameterSpecPass(
int tLen, byte[] src, int offset, int len) {
try {
GCMParameterSpec gcmps =
new GCMParameterSpec(tLen, src, offset, len);
if (gcmps.getTLen() != tLen) {
throw new Exception("tLen's not equal");
}
if (!Arrays.equals(gcmps.getIV(),
Arrays.copyOfRange(src, offset, offset + len))) {
System.out.println(offset + " " + len);
System.out.println(Arrays.copyOfRange(src, offset, len)[0]);
throw new Exception("IV's not equal");
}
} catch (Exception e) {
e.printStackTrace();
failed++;
}
}
private byte[] getCipherTextBySpec(GCMParameterSpec spec) throws Exception {
// init a cipher
Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, spec);
cipher.updateAAD(AAD);
byte[] cipherText = cipher.doFinal(data);
// check IVs
if (!Arrays.equals(cipher.getIV(), spec.getIV())) {
System.out.println("IV in parameters is incorrect");
return null;
}
if (spec.getTLen() != (cipherText.length - data.length) * 8) {
System.out.println("Tag length is incorrect");
return null;
}
return cipherText;
}
private static void newGCMParameterSpecPass(
int tLen, byte[] src) {
try {
GCMParameterSpec gcmps = new GCMParameterSpec(tLen, src);
if (gcmps.getTLen() != tLen) {
throw new Exception("tLen's not equal");
}
if (!Arrays.equals(gcmps.getIV(), src)) {
throw new Exception("IV's not equal");
}
} catch (Exception e) {
e.printStackTrace();
failed++;
}
}
private boolean doTest() throws Exception {
GCMParameterSpec spec1 = new GCMParameterSpec(tagLength, IV);
GCMParameterSpec spec2 = new GCMParameterSpec(tagLength, IVO, offset,
IVlength);
byte[] cipherText1 = getCipherTextBySpec(spec1);
if (cipherText1 == null) {
return false;
}
byte[] cipherText2 = getCipherTextBySpec(spec2);
if (cipherText2 == null) {
return false;
}
if (!Arrays.equals(cipherText1, cipherText2)) {
System.out.println("Cipher texts are different");
return false;
}
if (spec1.getTLen() != spec2.getTLen()) {
System.out.println("Tag lengths are not equal");
return false;
}
byte[] recoveredText1 = recoverCipherText(cipherText1, spec2);
if (recoveredText1 == null) {
return false;
}
byte[] recoveredText2 = recoverCipherText(cipherText2, spec1);
if (recoveredText2 == null) {
return false;
}
if (!Arrays.equals(recoveredText1, recoveredText2)) {
System.out.println("Recovered texts are different");
return false;
}
if (!Arrays.equals(recoveredText1, data)) {
System.out.println("Recovered and original texts are not equal");
return false;
}
return true;
}
protected void engineInit(AlgorithmParameterSpec paramSpec)
throws InvalidParameterSpecException {
if (!(paramSpec instanceof GCMParameterSpec)) {
throw new InvalidParameterSpecException
("Inappropriate parameter specification");
}
GCMParameterSpec gps = (GCMParameterSpec) paramSpec;
// need to convert from bits to bytes for ASN.1 encoding
this.tLen = gps.getTLen()/8;
this.iv = gps.getIV();
}
private static void newGCMParameterSpecPass(
int tLen, byte[] src) {
try {
GCMParameterSpec gcmps = new GCMParameterSpec(tLen, src);
if (gcmps.getTLen() != tLen) {
throw new Exception("tLen's not equal");
}
if (!Arrays.equals(gcmps.getIV(), src)) {
throw new Exception("IV's not equal");
}
} catch (Exception e) {
e.printStackTrace();
failed++;
}
}
private boolean doTest() throws Exception {
GCMParameterSpec spec1 = new GCMParameterSpec(tagLength, IV);
GCMParameterSpec spec2 = new GCMParameterSpec(tagLength, IVO, offset,
IVlength);
byte[] cipherText1 = getCipherTextBySpec(spec1);
if (cipherText1 == null) {
return false;
}
byte[] cipherText2 = getCipherTextBySpec(spec2);
if (cipherText2 == null) {
return false;
}
if (!Arrays.equals(cipherText1, cipherText2)) {
System.out.println("Cipher texts are different");
return false;
}
if (spec1.getTLen() != spec2.getTLen()) {
System.out.println("Tag lengths are not equal");
return false;
}
byte[] recoveredText1 = recoverCipherText(cipherText1, spec2);
if (recoveredText1 == null) {
return false;
}
byte[] recoveredText2 = recoverCipherText(cipherText2, spec1);
if (recoveredText2 == null) {
return false;
}
if (!Arrays.equals(recoveredText1, recoveredText2)) {
System.out.println("Recovered texts are different");
return false;
}
if (!Arrays.equals(recoveredText1, data)) {
System.out.println("Recovered and original texts are not equal");
return false;
}
return true;
}
protected void engineInit(AlgorithmParameterSpec paramSpec)
throws InvalidParameterSpecException {
if (!(paramSpec instanceof GCMParameterSpec)) {
throw new InvalidParameterSpecException
("Inappropriate parameter specification");
}
GCMParameterSpec gps = (GCMParameterSpec) paramSpec;
// need to convert from bits to bytes for ASN.1 encoding
this.tLen = gps.getTLen()/8;
this.iv = gps.getIV();
}
private static void newGCMParameterSpecPass(
int tLen, byte[] src) {
try {
GCMParameterSpec gcmps = new GCMParameterSpec(tLen, src);
if (gcmps.getTLen() != tLen) {
throw new Exception("tLen's not equal");
}
if (!Arrays.equals(gcmps.getIV(), src)) {
throw new Exception("IV's not equal");
}
} catch (Exception e) {
e.printStackTrace();
failed++;
}
}
private boolean doTest() throws Exception {
GCMParameterSpec spec1 = new GCMParameterSpec(tagLength, IV);
GCMParameterSpec spec2 = new GCMParameterSpec(tagLength, IVO, offset,
IVlength);
byte[] cipherText1 = getCipherTextBySpec(spec1);
if (cipherText1 == null) {
return false;
}
byte[] cipherText2 = getCipherTextBySpec(spec2);
if (cipherText2 == null) {
return false;
}
if (!Arrays.equals(cipherText1, cipherText2)) {
System.out.println("Cipher texts are different");
return false;
}
if (spec1.getTLen() != spec2.getTLen()) {
System.out.println("Tag lengths are not equal");
return false;
}
byte[] recoveredText1 = recoverCipherText(cipherText1, spec2);
if (recoveredText1 == null) {
return false;
}
byte[] recoveredText2 = recoverCipherText(cipherText2, spec1);
if (recoveredText2 == null) {
return false;
}
if (!Arrays.equals(recoveredText1, recoveredText2)) {
System.out.println("Recovered texts are different");
return false;
}
if (!Arrays.equals(recoveredText1, data)) {
System.out.println("Recovered and original texts are not equal");
return false;
}
return true;
}
protected void engineInit(AlgorithmParameterSpec paramSpec)
throws InvalidParameterSpecException {
if (!(paramSpec instanceof GCMParameterSpec)) {
throw new InvalidParameterSpecException
("Inappropriate parameter specification");
}
GCMParameterSpec gps = (GCMParameterSpec) paramSpec;
// need to convert from bits to bytes for ASN.1 encoding
this.tLen = gps.getTLen()/8;
this.iv = gps.getIV();
}
private static void newGCMParameterSpecPass(
int tLen, byte[] src) {
try {
GCMParameterSpec gcmps = new GCMParameterSpec(tLen, src);
if (gcmps.getTLen() != tLen) {
throw new Exception("tLen's not equal");
}
if (!Arrays.equals(gcmps.getIV(), src)) {
throw new Exception("IV's not equal");
}
} catch (Exception e) {
e.printStackTrace();
failed++;
}
}
private static void newGCMParameterSpecPass(
int tLen, byte[] src) {
try {
GCMParameterSpec gcmps = new GCMParameterSpec(tLen, src);
if (gcmps.getTLen() != tLen) {
throw new Exception("tLen's not equal");
}
if (!Arrays.equals(gcmps.getIV(), src)) {
throw new Exception("IV's not equal");
}
} catch (Exception e) {
e.printStackTrace();
failed++;
}
}