box7.c 1022 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include <stdio.h>
  2. #include "crypto_box.h"
  3. #include "randombytes.h"
  4. unsigned char alicesk[crypto_box_SECRETKEYBYTES];
  5. unsigned char alicepk[crypto_box_PUBLICKEYBYTES];
  6. unsigned char bobsk[crypto_box_SECRETKEYBYTES];
  7. unsigned char bobpk[crypto_box_PUBLICKEYBYTES];
  8. unsigned char n[crypto_box_NONCEBYTES];
  9. unsigned char m[10000];
  10. unsigned char c[10000];
  11. unsigned char m2[10000];
  12. int main()
  13. {
  14. int mlen;
  15. int i;
  16. for (mlen = 0;mlen < 1000 && mlen + crypto_box_ZEROBYTES < sizeof m;++mlen) {
  17. crypto_box_keypair(alicepk,alicesk);
  18. crypto_box_keypair(bobpk,bobsk);
  19. randombytes(n,crypto_box_NONCEBYTES);
  20. randombytes(m + crypto_box_ZEROBYTES,mlen);
  21. crypto_box(c,m,mlen + crypto_box_ZEROBYTES,n,bobpk,alicesk);
  22. if (crypto_box_open(m2,c,mlen + crypto_box_ZEROBYTES,n,alicepk,bobsk) == 0) {
  23. for (i = 0;i < mlen + crypto_box_ZEROBYTES;++i)
  24. if (m2[i] != m[i]) {
  25. printf("bad decryption\n");
  26. break;
  27. }
  28. } else {
  29. printf("ciphertext fails verification\n");
  30. }
  31. }
  32. return 0;
  33. }